Ohne weiteres ist der Schlüsselcode wie folgt:
1. Code: dbcontexTHolder
public class dbcontextHolder {// ThreadSafe ThreadLocalPrivate static Final ThreadLocal <string> contextHolder = new ThreadLocal <string> (); public static void setdbType (String dBtype) {contexHolder.set (dbType); ((String) contextHolder.get ());} public static void clearDbType () {contextHolder.remove ();}} 2. Code: DynamicDataSource
import org.springframework.jdbc.datasource.lookup.abstractroutingDataSource; Public Class DynamicDataSource erweitert AbstractroutingDataSource {@Overridepublic Object DeterdecurrentLookupkey () {{{{{{{{{} dbcontexTholder.get. 3. Code: Spring.xml
<!-- Data source property configuration file-><context:property-placeholder location="classpath:ibatis.properties" /><bean id="jksh"destroy-method="close"><!-- Connection Info --><property name="driverClassName" value="${driver.jksh}" /><property name="url" value="${url.jksh}" /><property name = "userername" value = "$ {userername.jksh}" /> <Eigenschaft name = "password" value = "$ {password.jksh}" /> <!-Verbindungspooling-Info-> <Eigenschaft name = "maxidle" value = "$ {maxidle.jksh}" /> <Property = "maxactive" maxactive "maxactive" maxactive "maxactive" maxactive "maxactive" maxactive "maxactive" $ "{{{}" /"mAxactive" "maxactive". name = "defaultAutocommit" value = "false" /> <Eigenschaft name = "timeBetweenevictionrunsmillis" value = "$ {timeBetweenevictionrunsmillis.jksh}" /> <Property name = "mineVictableIdletImememememememIlis" value = "{{mineVictableIntable id = "jclt" Destroy-method = "close"> <!-Verbindungsinformationen-> <Eigenschaft name = "triverClassName" value = "$ {triver.jclt}" /> <Eigenschaft name = "url" value = "$ {url.jclt}" /<Property name = "username" value = "$" {username.jclt} " value="${password.jclt}" /><!-- Connection Pooling Info --><property name="maxIdle" value="${maxIdle.jclt}" /><property name="maxActive" value="${maxActive.jclt}" /><property name="defaultAutoCommit" value="false" /><property name = "timeBetweenevictionRunsmillis" value = "$ {timeBetweenevictionRunsmillis.jclt}" /> <Eigenschaft name = "mineVictableableIdletImemillis" value = "$ {mineVictableIdletImemillis.jclt}" /> < /bean> <Ente idaNs = ""> < /bean; key-type="java.lang.String"><entry key="jksh" value-ref="jksh" /><entry key="jclt" value-ref="jclt" /></map></property><property name="defaultTargetDataSource" ref="jksh" /></bean> 4. Code: Hauptmethode
importieren javax.sql.datasource; import org.springframework.context.applicationContext; com.jclt.service.commons.dbcontextHolder; import com.jclt.service.model.User; import org.apache.ibatis.Session @param args*/public static void main (String [] args) {applicationContext appcontext = new classPathXmlApplicationContext ("client-beans.xml"); dataSource = (dataSource) appcontext.getbean ("dataSource"); = bean.getObject (); oder SqlSessionFactoryBean (); bean1.setDataSource (dataSource1); Ressourcenressource1 = Neue DateisystemeSource (res1); Bean1.setConFigLocation (Resource1); Try {SQLSessionFactory SessionFactory = BEAN.GetObject (); user = session.selectone ("com.jclt.service.dao.readjksh.findone");Das obige ist die Kombination von Spring und MyBatis, um die Multi-Daten-Quellschaltfunktion zu implementieren, die der Editor Ihnen vorgestellt hat. Ich hoffe, es wird Ihnen hilfreich sein. Wenn Sie Fragen haben, hinterlassen Sie mir bitte eine Nachricht und der Editor wird Ihnen rechtzeitig antworten. Vielen Dank für Ihre Unterstützung auf der Wulin.com -Website!