原文链接:http://blog.csdn.net/u010310183/article/details/52884909
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:default.properties" />
<!-- 配置数据源 -->
<bean name="datasource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- <property name="driverClassName" value="${jdbc.driver}" /> --><!-- dirverClassName 可以不加 -->
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置数据源1 -->
<bean name="datasource1" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- <property name="driverClassName" value="${jdbc.driver}" /> --><!-- dirverClassName 可以不加 -->
<property name="url" value="${jdbc.url1}"></property>
<property name="username" value="${jdbc.username1}" />
<property name="password" value="${jdbc.password1}" />
</bean>
<bean id="multipleDataSource" class="com.cn.util.MultipleDataSource">
<property name="defaultTargetDataSource" ref="datasource"/>
<property name="targetDataSources">
<map>
<entry key="datasource" value-ref="datasource"/>
<entry key="datasource1" value-ref="datasource1"/>
</map>
</property>
</bean>
<bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter">
<property name="statementExecutableSqlLogEnable" value="false" />
</bean>
<!-- transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="multipleDataSource" />
</bean>
<!-- enable component scanning and autowire (beware that this does not enable mapper scanning!) -->
<context:component-scan base-package="com.cn.service" />
<!-- enable transaction demarcation with annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="multipleDataSource" />
<property name="configLocation" value="classpath:mybatis-config-default.xml"></property>
<property name="typeAliasesPackage" value="com.cn.model"/>
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.dao" />
</bean>
<!-- 配置线程池 -->
<bean id ="taskExecutor" class ="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
<!-- 线程池维护线程的最少数量 -->
<property name ="corePoolSize" value ="5" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name ="keepAliveSeconds" value ="30000" />
<!-- 线程池维护线程的最大数量 -->
<property name ="maxPoolSize" value ="1000" />
<!-- 线程池所使用的缓冲队列 -->
<property name ="queueCapacity" value ="200" />
</bean>
</beans>
增加MultipleDataSource类(保证spring能扫描到)
package com.service.manager.util;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class MultipleDataSource extends AbstractRoutingDataSource {
public static final String datasource = "datasource";
public static final String datasource1 = "datasource1";
private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
public static void setDataSourceKey(String dataSource) {
dataSourceKey.set(dataSource);
}
@Override
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return dataSourceKey.get();
}
}
增加配置文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&autoreconnect=true&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=
jdbc.driver1=com.mysql.jdbc.Driver
jdbc.url1=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&autoreconnect=true&zeroDateTimeBehavior=convertToNull
jdbc.username1=root
jdbc.password1=
Controller切换数据源:
MultipleDataSource.setDataSourceKey(MultipleDataSource.datasource);