Sometimes multiple databases are connected in a project, and multiple data sources need to be configured in spring. I have encountered this problem recently. Since my project was based on general Dao before, there are constant problems during configuration, and this method conflicts with resource files. If you scan the mapping file, the bean name of the SqlSessionFactory must be sqlSessionFactory. It cannot read sqlSessioNFactory2 or other names. The final solution is as follows:
1. Add the following class MultipleDataSource.java to the project
package com.etoak.util; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class MultipleDataSource extends AbstractRoutingDataSource { private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>(); public static void setDataSourceKey(String dataSource) { dataSourceKey.set(dataSource); } @Override protected Object determinedCurrentLookupKey() { // TODO Auto-generated method stub return dataSourceKey.get(); } } The spring configuration file is as follows:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com"/> <mvc:annotation-driven/> <context:property-placeholder location="classpath:db.properties"/> <bean id="ds1" p:driverClassName="${mysql.driver}" p:url="${mysql.url}" p:username="${mysql.username}" p:password="${mysql.password}"/> <bean id="ds2" p:driverClassName="${mysql2.driver}" p:url="${mysql2.url}" p:username="${mysql2.username}" p:password="${mysql2.password}"/> <bean id="multipleDataSource"> <property name="defaultTargetDataSource" ref="ds1"/> <property name="targetDataSources"> <map> <entry key="ds1" value-ref="ds1"/> <entry key="ds2" value-ref="ds2"/> </map> </property> </bean> <bean id="sqlSessionFactory1" p:dataSource-ref="multipleDataSource" p:mapperLocations="classpath:com/etoak/dao/*-mapper.xml"/> <bean> <bean> <property name="basePackage" value="com.etoak.dao"/> <property name="markerInterface" value="com.etoak.dao.BaseDao" /> </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); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.