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);
  }
  
}

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

spring基于通用Dao的多数据源配置详解【ds1】的更多相关文章

  1. spring基于通用Dao的多数据源配置

    有时候在一个项目中会连接多个数据库,须要在spring中配置多个数据源,近期就遇到了这个问题,因为我的项目之前是基于通用Dao的,配置的时候问题不断.这样的方式和资源文件冲突:扫描映射文件的话,Sql ...

  2. Spring MVC配置文件的三个常用配置详解

    转自:http://www.cnblogs.com/benwu/articles/5162614.html Spring MVC项目中通常会有二个配置文件,sprng-servlet.xml和appl ...

  3. spring框架中AOP思想与各种配置详解

    Spring中提供两种AOP支持:   1.基于代理的经典AOP   2.Aspectj注解配置AOP    首先我们先了解什么是AOP,AOP(Aspect Oriented Programming ...

  4. Spring之旅第四篇-注解配置详解

    一.引言 最近因为找工作,导致很长时间没有更新,找工作的时候你会明白浪费的时间后面都是要还的,现在的每一点努力,将来也会给你回报的,但行好事,莫问前程!努力总不会有错的. 上一篇Spring的配置博客 ...

  5. 从Spring到SpringBoot构建WEB MVC核心配置详解

    目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开 ...

  6. 小慢歌之基于RHEL8/CentOS8的网络IP配置详解

    ➡ 在rhel8(含centos8)上,没有传统的network.service,在/etc/sysconfig/network-scripts/里也看不到任何脚本文件,那么该如何进行网络配置呢. ➡ ...

  7. Http请求中Content-Type讲解以及在Spring MVC注解中produce和consumes配置详解

    原文地址:  https://blog.csdn.net/shinebar/article/details/54408020 引言: 在Http请求中,我们每天都在使用Content-type来指定不 ...

  8. SpringBoot + Spring Security 基本使用及个性化登录配置详解

    Spring Security 基本介绍 这里就不对Spring Security进行过多的介绍了,具体的可以参考官方文档 我就只说下SpringSecurity核心功能: 认证(你是谁) 授权(你能 ...

  9. 基于RHEL8/CentOS8的网络IP配置详解

    ➡ 在rhel8(含centos8)上,没有传统的network.service,在/etc/sysconfig/network-scripts/里也看不到任何脚本文件,那么该如何进行网络配置呢. ➡ ...

随机推荐

  1. Samba共享目录的多用户权限设置案例

    下面根据实际工作中遇到的一个共享目录的多用户权限需求案例来说明下Samba用户权限的设置. 一.需求场景领导:李一(liyi)正式员工(zhengshiyuangong):刘二二(liuerer).于 ...

  2. php ajax登录注册

    用户登录与退出功能应用在很多地方,而在有些项目中,我们需要使用Ajax方式进行登录,登录成功后只刷新页面局部,从而提升了用户体验度.本文将使用PHP和jQuery来实现登录和退出功能. 准备数据库 本 ...

  3. 多校寒训TaoTao要吃鸡dp

    题目描述 Taotao的电脑带不动绝地求生,所以taotao只能去玩pc版的荒野行动了, 和绝地求生一样,游戏人物本身可以携带一定重量m的物品,装备背包 之后可以多携带h(h为0代表没有装备背包)重量 ...

  4. This Android SDK requires Android Developer Toolkit version 17.0.0 or above. Current version is 10.0.0.v201102162101-104271. Please update ADT to the latest version.

    win7/xp 下面安装Android虚拟机,更新SDK后,在Eclipse preference里指向android-sdk-windows时. 出现 : This Android SDK requ ...

  5. EasyUI treegrid 模糊查询、搜索

    纯JS完成 treegrid 模糊搜索功能: $("#dataTab").treegrid({ url: '/Organization/GetOrganizationList', ...

  6. From CORBA technology To Thrift technology

    技术在变,需求不变,把复杂的事情搞简单,而不是相反. 无论CORBA还是Thrift,目标只有一个:一处定义,多处使用,解决的问题只有一个:规范和简化客户端与服务器的通信的开发工作. 是不是和java ...

  7. 修复PLSQL Developer 与 Office 2010的集成导出Excel 功能

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.htm]"PerceivedType"="text&qu ...

  8. Windows环境搭建mysql服务器

    Windows环境搭建mysql服务器: 1.下载mysql-installer-community-5.7.3.0-m13.2063434697并安装  安装详细步骤>> 2.安装mys ...

  9. [工作相关] 一个虚拟机上面的SAP4HANA的简单使用维护

    1.公司组织竞品分析, 选择了SAP的 SAP4HANA作为竞品 这边协助同事搭建了SAP4HANA的测试环境: 备注 这个环境 应该是同事通过一些渠道获取到的. 里面是基于这个虚拟机进行的说明:: ...

  10. PSP(4.20——4.26)以及周记录

    1.PSP 4.20 8:45 9:25 10 30 Cordova A Y min 13:00 17:00 65 175 Cordova A Y min 4.21 9:00 17:00 125 35 ...