最近在做项目的时候遇到需要配置多个数据源,就想写篇博客当做笔记,做个总结。
首先是配置文件。
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="validationQuery" value="select 1"></property>
<property name="testWhileIdle" value="true"></property>
<property name="timeBetweenEvictionRunsMillis" value="3600000"></property>
<property name="minEvictableIdleTimeMillis" value="18000000"></property>
<property name="testOnBorrow" value="true"></property>
</bean>
<bean id="dataSourceTwo" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.drivertwo}"/>
<property name="url" value="${jdbc.urltwo}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="dataSourceType" class="utry.DataProcessingSystem.utils.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="dataSource" key="dataSourceOne"></entry>
<entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource"></property>
</bean>
<bean class="utry.DataProcessingSystem.interceptor.DialectUtil">
<property name="dialect" value="${jdbc.dialect}"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceType" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池-->
<property name="dataSource" ref="dataSourceType"/>
<!--configLocation属性指定mybatis的核心配置文件-->
<property name="configLocation" value="classpath:myBatis-config.xml" />
<!-- 扫描model -->
<property name="typeAliasesPackage" value="***********.bo"/>
<!-- 所有配置的mapper文件 -->
<property name="mapperLocations" value="classpath:mybatis/*.xml" />
<property name="plugins">
<array>
<bean class="utry.DataProcessingSystem.interceptor.PageInterceptor">
</bean>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="***********.dao" />
</bean>
</beans>
基本的就这样就可以满足多数据源了(不要求配置事物的情况下)
JAVA:
//DynamicDataSource 控制切换数据源
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getCustomerType();
}
}
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}
public static String getCustomerType() {
return contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}
import org.springframework.stereotype.Component;
@Component
public class DataSourceInterceptor {
public static final String SOURCE_ONE = "dataSourceOne";
public static final String SOURCE_TWO = "dataSourceTwo";
}
具体业务中切换的时候,以下面Junit例子为参照。读取配置文件方式请自行百度,由于涉及项目的一些命名,我就不发了。
@Resource
private TestDataSourceDao testDataDao;
@Test
public void test(){
DataSourceContextHolder.setCustomerType(DataSourceInterceptor.SOURCE_TWO);//切换到第二个数据源
List<TestDataSource> list = testDataDao.findAll();
for (TestDataSource testDataSource : list) {
System.out.println(testDataSource.getId()+"-->"+testDataSource.getName());
}
}