spring基于通用Dao的多数据源配置
有时候在一个项目中会连接多个数据库,须要在spring中配置多个数据源,近期就遇到了这个问题,因为我的项目之前是基于通用Dao的,配置的时候问题不断。这样的方式和资源文件冲突;扫描映射文件的话,SqlSessionFactory的bean名字必须是sqlSessionFactory 他读不到sqlSessioNFactory2或者其它名字,终于解决方法例如以下:
1.在项目中增加例如以下类MultipleDataSource.java
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配置文件例如以下:
<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/context/spring-context-3.2.xsd
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" 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>
測试类例如以下:
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的多数据源配置的更多相关文章
- spring基于通用Dao的多数据源配置详解【ds1】
spring基于通用Dao的多数据源配置详解 有时候在一个项目中会连接多个数据库,需要在spring中配置多个数据源,最近就遇到了这个问题,由于我的项目之前是基于通用Dao的,配置的时候问题不断,这种 ...
- Spring Boot 2.x Redis多数据源配置(jedis,lettuce)
Spring Boot 2.x Redis多数据源配置(jedis,lettuce) 96 不敢预言的预言家 0.1 2018.11.13 14:22* 字数 65 阅读 727评论 0喜欢 2 多数 ...
- 基于注解实现SpringBoot多数据源配置
1.功能介绍 在实际的开发中,同一个项目中使用多个数据源是很常见的场景.最近在学习的过程中使用注解的方式实现了一个Springboot项目多数据源的功能.具体实现方式如下. 2.在applicatio ...
- spring boot + druid + mybatis + atomikos 多数据源配置 并支持分布式事务
文章目录 一.综述 1.1 项目说明 1.2 项目结构 二.配置多数据源并支持分布式事务 2.1 导入基本依赖 2.2 在yml中配置多数据源信息 2.3 进行多数据源的配置 三.整合结果测试 3.1 ...
- Spring学习笔记:jdbcTemplate和数据源配置
一.使用Spring框架jdbcTemplate实现数据库的增删改查 1.数据库 /* SQLyog Ultimate v8.32 MySQL - 5.7.19-log : Database - in ...
- Spring Boot + Mybatis多数据源和动态数据源配置
文章转自 https://blog.csdn.net/neosmith/article/details/61202084 网上的文章基本上都是只有多数据源或只有动态数据源,而最近的项目需要同时使用两种 ...
- Spring BeanPostProcessor与动态加载数据源配置
前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverClass都通过运行时指定,而非由xml静态配置定死 ...
- spring+mybatis通用dao层、service层的实现
个人理解: 1.mybatis-spring.jar 提供了SqlSessionTemplate类该类可以对数据库进行CRUD操作(底层其实还是SqlSession) 2.我们可以集成SqlSessi ...
- Spring Boot数据访问之多数据源配置及数据源动态切换
如果一个数据库数据量过大,考虑到分库分表和读写分离需要动态的切换到相应的数据库进行相关操作,这样就会有多个数据源.对于一个数据源的配置在Spring Boot数据访问之数据源自动配置 - 池塘里洗澡的 ...
随机推荐
- Android -----listView的重要属性
android:transcriptMode="alwaysScroll" android:cacheColorHint="#00000000" android ...
- C#内容格式刷html 转成txt
/// <summary> /// 内容格式刷 /// </summary> /// <param name="strHtml">要格式的文本& ...
- ionic中遇到的一些问题和坑
接触ionic有一段时间了,一路上踩了不少坑.大部分都记录下来了,分享给大家,可以少走很多弯路 1,ng-init不能在body里面初始化,可以在一个段落里面初始化<div ng-init> ...
- Windows 10 IIS所有的html返回空白
这是一个神奇的现象.因为使用IIS已经有N多年了,喜欢使用它是因为它随手可得.自从装上windows10以来,直至今天才用它来调试客户端程序.想在上面放一个静态的json数据,省的还要去建立一个Web ...
- dotnetnuke 头像调用 头像缩放
public static string GetProfileImage(int userId, int width, int height) { ...
- (转)分布式文件存储FastDFS(四)配置fastdfs-apache-module
http://blog.csdn.net/xingjiarong/article/details/50560605 在前边我们已经配置好了FastDFS的环境,但是此时的FastDFS还不能通过htt ...
- centos设置ssh安全只允许用户从指定的IP登陆
1.编辑文件 /etc/ssh/sshd_config vi /etc/ssh/sshd_config 2.root用户只允许在如下ip登录 AllowUsers root@203.212.4.117 ...
- vue跨域问题解决(生产环境)
vue跨域问题解决(使用webpack打包的) 配置代理:(config下index.js文件) module.exports = { dev: { env: require('./dev.env') ...
- P2080 增进感情
题目背景 小明和小红的感情,是慢慢发展起来的. 题目描述 他们对对方分别有一个好感值.定义两人的亲密程度为两人的好感值之和. 如果他们的亲密程度达到V,则他们将走到一起.他们以后的生活将取决于两人的好 ...
- Codevs P1017 乘积最大
P1017 乘积最大 题目描述 Description 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的 ...