spring基于通用Dao的多数据源配置详解【ds1】

 

 

spring基于通用Dao的多数据源配置详解

 

有时候在一个项目中会连接多个数据库,需要在spring中配置多个数据源,最近就遇到了这个问题,由于我的项目之前是基于通用Dao的,配置的时候问题不断,这种方式和资源文件冲突;扫描映射文件的话,SqlSessionFactory的bean名字必须是sqlSessionFactory 他读不到sqlSessioNFactory2或者其他名字,最终解决方法如下:

1.在项目中加入如下类MultipleDataSource.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 determineCurrentLookupKey() {
     // TODO Auto-generated method stub
     return dataSourceKey.get();
   }
  
}

spring配置文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  
   < context:component-scan base-package = "com" />
    
   < mvc:annotation-driven />
    
  
   < context:property-placeholder location = "classpath:db.properties" />
   < bean id = "ds1" class = "org.springframework.jdbc.datasource.DriverManagerDataSource"
     p:driverClassName = "${mysql.driver}"
     p:url = "${mysql.url}"
     p:username = "${mysql.username}"
     p:password = "${mysql.password}" />
   < bean id = "ds2" class = "org.springframework.jdbc.datasource.DriverManagerDataSource"
     p:driverClassName = "${mysql2.driver}"
     p:url = "${mysql2.url}"
     p:username = "${mysql2.username}"
     p:password = "${mysql2.password}" />
    
    
   < bean id = "multipleDataSource" class = "com.etoak.util.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" class = "org.mybatis.spring.SqlSessionFactoryBean"
     p:dataSource-ref = "multipleDataSource"
     p:mapperLocations = "classpath:com/etoak/dao/*-mapper.xml" />
    
   < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >
     < property name = "basePackage" value = "com.etoak.dao" />
         < property name = "markerInterface" value = "com.etoak.dao.BaseDao" />
   </ bean >  
    
</ beans >

测试类如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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);
   }
  
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

转载于:https://www.cnblogs.com/libin6505/p/10552911.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值