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
|
xsi:schemaLocation="http://www.springframework.org/schema/beans < 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】的更多相关文章
- spring基于通用Dao的多数据源配置
有时候在一个项目中会连接多个数据库,须要在spring中配置多个数据源,近期就遇到了这个问题,因为我的项目之前是基于通用Dao的,配置的时候问题不断.这样的方式和资源文件冲突:扫描映射文件的话,Sql ...
- Spring MVC配置文件的三个常用配置详解
转自:http://www.cnblogs.com/benwu/articles/5162614.html Spring MVC项目中通常会有二个配置文件,sprng-servlet.xml和appl ...
- spring框架中AOP思想与各种配置详解
Spring中提供两种AOP支持: 1.基于代理的经典AOP 2.Aspectj注解配置AOP 首先我们先了解什么是AOP,AOP(Aspect Oriented Programming ...
- Spring之旅第四篇-注解配置详解
一.引言 最近因为找工作,导致很长时间没有更新,找工作的时候你会明白浪费的时间后面都是要还的,现在的每一点努力,将来也会给你回报的,但行好事,莫问前程!努力总不会有错的. 上一篇Spring的配置博客 ...
- 从Spring到SpringBoot构建WEB MVC核心配置详解
目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开 ...
- 小慢歌之基于RHEL8/CentOS8的网络IP配置详解
➡ 在rhel8(含centos8)上,没有传统的network.service,在/etc/sysconfig/network-scripts/里也看不到任何脚本文件,那么该如何进行网络配置呢. ➡ ...
- Http请求中Content-Type讲解以及在Spring MVC注解中produce和consumes配置详解
原文地址: https://blog.csdn.net/shinebar/article/details/54408020 引言: 在Http请求中,我们每天都在使用Content-type来指定不 ...
- SpringBoot + Spring Security 基本使用及个性化登录配置详解
Spring Security 基本介绍 这里就不对Spring Security进行过多的介绍了,具体的可以参考官方文档 我就只说下SpringSecurity核心功能: 认证(你是谁) 授权(你能 ...
- 基于RHEL8/CentOS8的网络IP配置详解
➡ 在rhel8(含centos8)上,没有传统的network.service,在/etc/sysconfig/network-scripts/里也看不到任何脚本文件,那么该如何进行网络配置呢. ➡ ...
随机推荐
- Centos下MooseFS(MFS)分布式存储共享环境部署记录
分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网络与节点相连,分布式文件系统的实际基于客户机/服务器模式.目前 ...
- MySQL高可用架构-MHA环境部署记录
一.MHA介绍 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司) ...
- ZJOI2008 生日聚会Party
对于任意连续区间的限制,可以转化为以i结尾的所有区间的限制.这个转换在昨天的后缀自动机题也有用到,因此将其命名为区后变换.稍加分析后,我们记录以i结尾任意区间最大差即可进行DP转移.这个转换同时也创造 ...
- SCRUM 12.23
距离第二轮迭结束只有几天了. 我们全体组员现在的工作方向都在应用测试上. 明天的任务分配如下 成员 已完成任务 新任务 彭林江 落实API 自动爬虫测试 王卓 提升爬虫程序性能 正确性测试 郝倩 提升 ...
- Scrum Meeting NO.10
Scrum Meeting No.10 1.会议内容 2.任务清单 徐越 序号 近期的任务 进行中 已完成 1 "我"回答过的问题 -- 界面 √ 2 "问题" ...
- <<浪潮之巅>>阅读笔记二
好的文章总是慢慢吸引着你去阅读,这本书的作者是吴军博士,让我很钦佩的是他还是一个很著名的程序员.其实我感觉理科生在写作方面的能力是很欠缺的,我们经常做到了有观点,但是做不到和别人表达清楚你的观点想法, ...
- C#-ToString格式化
Int.ToString(format): 格式字符串采用以下形式:Axx,其中 A 为格式说明符,指定格式化类型,xx 为精度说明符,控制格式化输出的有效位数或小数位数,具体如下: 格式说明符 说明 ...
- org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent succ
数据库 没有开启 连接失败 org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause ...
- Maven-Build Lifecycle(构建生命周期)
https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html https://www.w3cschoo ...
- jquery 語法
基本形式: $(selector).action() 文檔加載函數: $(document).Ready{ function(){ //將所有的函數寫到文檔加載函數里,可以防止頁面未加載完全,就執行j ...