Spring multi-data source configuration based on universal Dao
Sometimes a project connects to multiple databases and needs to configure multiple data sources in spring. This problem has recently been encountered because my project was previously based on the general Dao, the problem persists during configuration. This method conflicts with the resource file. If the ing file is scanned, the bean name of SqlSessionFactory must be sqlSessionFactory and cannot read sqlSessioNFactory2 or other names. The final solution is as follows:
1. Add the following classes to the project: MultipleDataSource. java
package com.etoak.util;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;public class MultipleDataSource extends AbstractRoutingDataSource {private static final ThreadLocal
dataSourceKey = new InheritableThreadLocal
(); public static void setDataSourceKey(String dataSource) { dataSourceKey.set(dataSource); }@Overrideprotected Object determineCurrentLookupKey() {// TODO Auto-generated method stubreturn dataSourceKey.get();}}
The spring configuration file is as follows:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans 		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 		http://www.springframework.org/schema/context 		http://www.springframework.org/schema/context/spring-context-3.2.xsd 		http://www.springframework.org/schema/mvc 		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><context:component-scan base-package="com"></context:component-scan><mvc:annotation-driven></mvc:annotation-driven><context:property-placeholder location="classpath:db.properties"></context:property-placeholder><bean id=ds1 class=org.springframework.jdbc.datasource.DriverManagerDataSource p:password="${mysql.password}" p:username="${mysql.username}" p:url="${mysql.url}" p:driverclassname="${mysql.driver}"></bean><bean id=ds2 class=org.springframework.jdbc.datasource.DriverManagerDataSource p:password="${mysql2.password}" p:username="${mysql2.username}" p:url="${mysql2.url}" p:driverclassname="${mysql2.driver}"></bean><bean id=multipleDataSource class=com.etoak.util.MultipleDataSource> <property name="defaultTargetDataSource" ref="ds1"></property> <property name="targetDataSources"><MAP> <entry value-ref="ds1" key="ds1"></entry> <entry value-ref="ds2" key="ds2"></entry></MAP> </property> </bean><bean id=sqlSessionFactory1 class=org.mybatis.spring.SqlSessionFactoryBean p:datasource-ref="multipleDataSource" p:mapperlocations="classpath:com/etoak/dao/*-mapper.xml"></bean><bean class=org.mybatis.spring.mapper.MapperScannerConfigurer><property name="basePackage" value="com.etoak.dao"></property> <property name="markerInterface" value="com.etoak.dao.BaseDao"></property></bean></beans>
The test class is as follows:
package com.etoak.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import com.etoak.dao.ProductDaoIf;import com.etoak.util.MultipleDataSource;public class Test {public static void main(String[] args) {ApplicationContext ac = new FileSystemXmlApplicationContext("WebContent/WEB-INF/etoak-servlet.xml");ProductDaoIf proDao = (ProductDaoIf)ac.getBean(ProductDaoIf.class);MultipleDataSource.setDataSourceKey("ds1");int count1 = proDao.selectProductCount();MultipleDataSource.setDataSourceKey("ds2");int count2 = proDao.selectProductCount();System.out.println(count1);System.out.println(count2);}}