Spring与Mybatis相结合实现多数据源切换功能
废话不多说,关键代码如下所示:
1.代码:DbContextHolder
publicclassDbContextHolder{
//线程安全的ThreadLocal
privatestaticfinalThreadLocal<String>contextHolder=newThreadLocal<String>();
publicstaticvoidsetDbType(StringdbType){
contextHolder.set(dbType);
}
publicstaticStringgetDbType(){
return((String)contextHolder.get());
}
publicstaticvoidclearDbType(){
contextHolder.remove();
}
}
2.代码:DynamicDataSource
importorg.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
publicclassDynamicDataSourceextendsAbstractRoutingDataSource{
@Override
publicObjectdetermineCurrentLookupKey(){
returnDbContextHolder.getDbType();
}
}
3.代码:spring.xml
<!--数据源属性配置文件-->
<context:property-placeholderlocation="classpath:ibatis.properties"/>
<beanid="jksh"class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!--ConnectionInfo-->
<propertyname="driverClassName"value="${driver.jksh}"/>
<propertyname="url"value="${url.jksh}"/>
<propertyname="username"value="${username.jksh}"/>
<propertyname="password"value="${password.jksh}"/>
<!--ConnectionPoolingInfo-->
<propertyname="maxIdle"value="${maxIdle.jksh}"/>
<propertyname="maxActive"value="${maxActive.jksh}"/>
<propertyname="defaultAutoCommit"value="false"/>
<propertyname="timeBetweenEvictionRunsMillis"
value="${timeBetweenEvictionRunsMillis.jksh}"/>
<propertyname="minEvictableIdleTimeMillis"value="${minEvictableIdleTimeMillis.jksh}"/>
</bean>
<beanid="jclt"class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!--ConnectionInfo-->
<propertyname="driverClassName"value="${driver.jclt}"/>
<propertyname="url"value="${url.jclt}"/>
<propertyname="username"value="${username.jclt}"/>
<propertyname="password"value="${password.jclt}"/>
<!--ConnectionPoolingInfo-->
<propertyname="maxIdle"value="${maxIdle.jclt}"/>
<propertyname="maxActive"value="${maxActive.jclt}"/>
<propertyname="defaultAutoCommit"value="false"/>
<propertyname="timeBetweenEvictionRunsMillis"value="${timeBetweenEvictionRunsMillis.jclt}"/>
<propertyname="minEvictableIdleTimeMillis"value="${minEvictableIdleTimeMillis.jclt}"/>
</bean>
<beanid="dataSource"class="com.jclt.service.commons.DynamicDataSource">
<propertyname="targetDataSources">
<mapkey-type="java.lang.String">
<entrykey="jksh"value-ref="jksh"/>
<entrykey="jclt"value-ref="jclt"/>
</map>
</property>
<propertyname="defaultTargetDataSource"ref="jksh"/>
</bean>
4.代码:main方法
importjavax.sql.DataSource;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.springframework.core.io.FileSystemResource;
importorg.springframework.core.io.Resource;
importcom.jclt.service.commons.DbContextHolder;
importcom.jclt.service.model.User;
importorg.apache.ibatis.session.SqlSession;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.mybatis.spring.SqlSessionFactoryBean;
publicclassText{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
ApplicationContextappContext=newClassPathXmlApplicationContext("client-beans.xml");
DbContextHolder.setDbType("jclt");
Stringres="src/main/resources/ibatis-config.xml";
DataSourcedatasource=(DataSource)appContext.getBean("dataSource");
SqlSessionFactoryBeanbean=newSqlSessionFactoryBean();
bean.setDataSource(datasource);
Resourceresource=newFileSystemResource(res);
bean.setConfigLocation(resource);
try{
SqlSessionFactorysessionfactory=bean.getObject();
SqlSessionsession=sessionfactory.openSession();
Useruser=session.selectOne("com.jclt.service.Dao.readJKSH.findOne");
System.out.println(user.getName());
}catch(Exceptione){
e.printStackTrace();
}
DbContextHolder.setDbType("jksh");
Stringres1="src/main/resources/ibatis-config.xml";
DataSourcedatasource1=(DataSource)appContext.getBean("dataSource");
SqlSessionFactoryBeanbean1=newSqlSessionFactoryBean();
bean1.setDataSource(datasource1);
Resourceresource1=newFileSystemResource(res1);
bean1.setConfigLocation(resource1);
try{
SqlSessionFactorysessionfactory=bean.getObject();
SqlSessionsession=sessionfactory.openSession();
Useruser=session.selectOne("com.jclt.service.Dao.readJKSH.findOne");
System.out.println(user.getName());
}catch(Exceptione){
e.printStackTrace();
}
}
}
以上所述是小编给大家介绍的Spring与Mybatis相结合实现多数据源切换功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!