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();
}
}