1 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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- <context:component-scan base-package="org.cdsoft.*"></context:component-scan> -->
<context:component-scan base-package="sys.cdsoft.*"></context:component-scan>
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath*:db.properties</value>
</array>
</property>
</bean>
<!-- 配置数据库信息 -->
<bean id="dataSourceMysql" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="6"></property>
<!-- 防止mysql等待时间超时报错 -->
<property name="testOnBorrow" value="false"></property>
<property name="testOnReturn" value="false"></property>
<property name="testWhileIdle" value="true"></property>
<property name="timeBetweenEvictionRunsMillis" value="100000"></property>
<property name="validationQuery" value="select 1"></property>
</bean>
<!-- 配置数据库信息2 -->
<bean id="dataSourceMysql2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url2}"></property>
<property name="username" value="${username2}"></property>
<property name="password" value="${password2}"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="6"></property>
<!-- 防止mysql等待时间超时报错 -->
<property name="testOnBorrow" value="false"></property>
<property name="testOnReturn" value="false"></property>
<property name="testWhileIdle" value="true"></property>
<property name="timeBetweenEvictionRunsMillis" value="100000"></property>
<property name="validationQuery" value="select 1"></property>
</bean>
<bean id="dataSource" class="sys.cdsoft.util.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="dataSourceMysql" value-ref="dataSourceMysql"></entry>
<entry key="dataSourceMysql2" value-ref="dataSourceMysql2"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSourceMysql" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="sys.cdsoft.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath*:sys/cdsoft/*/*.xml"></property>
<property name="configLocation" value="classpath:mybatis.xml"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
</beans>
2. Java 继承 AbstractRoutingDataSource类
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getDataSource();
}
}
设定 数据源工具类
public class DynamicDataSourceHolder {
private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal();
public static String getDataSource() {
return (String) THREAD_DATA_SOURCE.get();
}
public static void setDataSource(String dataSource) {
THREAD_DATA_SOURCE.set(dataSource);
}
public static void clearDataSource() {
THREAD_DATA_SOURCE.remove();
}
}
代码中具体实战
@RequestMapping("selectall")
public JSONObject selectAll(Bankdetails bankdetails) {
DynamicDataSourceHolder.setDataSource("dataSourceMysql2");
System.out.println("前端传输数据为:"+gson.toJson(bankdetails));
CustomUtil util = new CustomUtil();
JSONObject jsonObject=new JSONObject();
if (StringUtil.isNotEmpty(bankdetails.getCreate_time())) {
String times[]=TimeUtil.reTimes(bankdetails.getCreate_time(), " - ");
bankdetails.setStart_time(times[0]);
bankdetails.setEnd_time(times[1]);
}