Spring multi-data source configuration based on universal Dao

Source: Internet
Author: User

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 &#13;&#10;&#9;&#9;http://www.springframework.org/schema/beans/spring-beans-3.2.xsd&#13;&#10;&#9;&#9;http://www.springframework.org/schema/context&#13;&#10;&#9;&#9;http://www.springframework.org/schema/context/spring-context-3.2.xsd&#13;&#10;&#9;&#9;http://www.springframework.org/schema/mvc&#13;&#10;&#9;&#9;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);}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.