これ以上苦労せずに、キーコードは次のとおりです。
1。コード:dbcontextholder
public class dbcontextholder {// threadsafe threadlocalprivate static final threadlocal <string> contextholder = new threadlocal <string>(); public static void setdbtype(string dbtype){contextholder.set(dbtype);} public Static getdbtype(){) ((string)contextholder.get());} public static void cleardbtype(){contextholder.remove();}}} 2。コード:DynamicDataSource
Intement org.springframework.jdbc.datasource.lookup.abstractroutingdatasource; public class dynamicdatasource extends abstractroutingdatasource {@overridepublic object decurrentlookupkey(){return dbcontextholder.getdbtype();} 3。コード:spring.xml
<! - データソースプロパティ構成ファイル - > <コンテキスト:プロパティプレイスホルダーlocation = "classpath:ibatis.properties" /> <bean id = "jksh" destroy-method = "close"> <! - connections情報 - > <プロパティ名= "driverclassname" value = "$ {dirter.jksh}" name = "username" value = "$ {username.jksh}" /> <property name = "password" value = "$ {password.jksh}" /> <! - 接続プーリング情報 - > <プロパティname = "maxidle" value = "$ {maxidle.jksh}" /> <Propetive name = "defaultAutoCommit" value = "false" /> <プロパティname = "timeevictionrunsmillis" value = "$ {timeevictionrunsmillis.jksh}" /> <プロパティname = "minevictableidletimemillis" value = "$ {minevictableidlemillis.jksh.jksh. id = "jclt" destroy-method = "close"> <! - connection info-> <property name = "driverclassname" value = "$ {driver.jclt}" /> <プロパティ名= "url" value = "$ {url.jclt}" /> <プロパティ名= "username" value = "$ {username.jclt.jclt. value = "$ {password.jclt}" /> <! - 接続プーリング情報 - > <プロパティ名= "maxidle" value = "$ {maxidle.jclt}" /> <プロパティ名= "maxactive" value = "$ {maxactive.jclt}" />> <プロパティ= "defaultocommit" /< name = "timevevictionrunsmillis" value = "$ {timeevictionrunsmillis.jclt}" /> <プロパティname = "value =" $ {minevictableidletimemillis.jclt} " />> < /bean id = <bean id = <bean id = <bean iD key-type = "java.lang.string"> <entry key = "jksh" value-ref = "jksh" /> <entry key = "jclt" /> < /map> < /property name = "defaulttargetdatasource" ref = "jksh" /> < /bean> < /bean> 4。コード:メインメソッド
javax.sql.datasource; Import org.springframework.context.applicationcontext; Import org.springframework.context.support.classpathxmlapplicationContext; import org.springframework.core.io.surresource com.jclt.service.commons.DbContextHolder;import com.jclt.service.model.User;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;public class Text {/*** @param args*/public static void main(string [] args){applicationContext appContext = new classpathxmlapplicationcontext( "client-beans.xml"); dbcontextholder.setdbtype( "jclt"); string res = "src/main/resources/ibatis-config.xml"; datasurce AppContext.getBean( "dataSource"); sqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); resource resource = new filesystemresource(res); bean.setConfiglocation(リソース); session = sessionfactory.opensession(); user user = session.selectone( "com.jclt.service.dao.readjksh.findone"); system.out.println(user.getname());} catch(exception e){e.printstacktrace();} dbcontextholder.setdbtype Res1 = "Src/Main/Resources/Ibatis-Config.xml"; dataSource dataSource1 =(dataSource)appContext.getBean( "dataSource"); sqlSessionFactoryBean Bean1 = new SQLSessionFactoryBean(); filesystemResource(res1); bean1.setConfiglocation(resource1); try {sqlsessionFactory Factory = bean.getObject(); sqlsession session = sessionfactory.opensession(); user user = session.selectone( "com.jclt.service.dao.readjksh.findone"); system.out.println(user.getname());} catch(exception e){e.printstacktrace();}}}}}上記は、編集者が紹介したマルチデータソーススイッチング関数を実装するための春とmybatisの組み合わせです。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!