spring+mybatis多数据源切换

1.applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> 

<!--配置mysql数据库参数 -->
<context:property-placeholder location="mysql.properties" />

<!--多个数据源配置 -->
<bean id="db1" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- 连接数据库参数 -->
<property name="driverClassName" value="${db1.driver}" />
<property name="url" value="${db1.url}" />
<property name="username" value="${db1.username}" />
<property name="password" value="${db1.password}" />

<!--连接池参数 -->
<property name="initialSize" value="10" />
<property name="maxActive" value="500" />
<property name="maxIdle" value="40" />
<property name="minIdle" value="10" />
</bean>

<bean id="db2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- 连接数据库参数 -->
<property name="driverClassName" value="${db2.driver}" />
<property name="url" value="${db2.url}" />
<property name="username" value="${db2.username}" />
<property name="password" value="${db2.password}" />

<!--连接池参数 -->
<property name="initialSize" value="10" />
<property name="maxActive" value="500" />
<property name="maxIdle" value="40" />
<property name="minIdle" value="10" />
</bean>

<bean id="dataSource" class="com.ckd.datasource.mybatis.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="db1" value-ref="db1" />
<entry key="db2" value-ref="db2" />
</map>
</property>
<property name="defaultTargetDataSource" ref="db2" />
</bean>

<!-- mybatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
</bean>
<!--获取sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0">
<ref bean="sqlSessionFactory"/>
</constructor-arg>
</bean> 
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

</beans>

 

2.具体实现切换的操作类:

/**
* @fileName DynamicDataSource.java
* @author chenkaideng
* @date 2015年8月27日
* @describe 动态获取数据源
*/
public class DynamicDataSource extends AbstractRoutingDataSource{
  @Override
  protected Object determineCurrentLookupKey() {
    // TODO Auto-generated method stub
    return DataSourceContextHolder.getDbType();
  }
}

 

/**
* @fileName DataSourceContextHolder.java
* @author chenkaideng
* @date 2015年8月27日
* @describe 数据源设值Holder类
*/
public class DataSourceContextHolder {
  private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); 

  public static void setDbType(String dbType) { 
    contextHolder.set(dbType); 
  } 

  public static String getDbType() { 
    return ((String) contextHolder.get()); 
  } 

  public static void clearDbType() { 
    contextHolder.remove(); 
  } 
}

 

 -》先是加载applicationContext.xml文件applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    -》然后设置数据源DataSourceContextHolder.setDbType("db1");
    -》接着从applicationContext 中获取sqlSession = (SqlSession) applicationContext.getBean("sqlSession");
    -》最后就可以拿这个sqlSession去做增删改查的操作
 
注意:不用对这个sqlSession做close和comit的操作,因为都已经由spring自己管理了,不用手动做这些操作。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值