SSH配置动态数据源
用到一个项目,需要整合2个不同的数据库!
现将代码贴下,以备后用:
1、创建静态映射类,该类映射动态数据源
public class DataSourceMap {
public static final String Analyse="Analyse";
public static final String DLmarket= "DLmarket";
}
2、创建数据库连接配置容器类
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setCustomerType(String customerType){
contextHolder.set(customerType);
}
public static String getCustomerType() {
return contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}
3、创建动态数据源切换类
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
String customerType="";
if(DataSourceContextHolder.getCustomerType()!=null){
customerType = DataSourceContextHolder.getCustomerType().toString();
}
return customerType;
}
}
该类继承AbstractRoutingDataSource,并重写determineCurrentLookupKey方法
4、在spring中配置多数据源
<bean id="analyseDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
xxx...
</bean> <bean id="dlmarketDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url_dlmarket}" />
<property name="username" value="${jdbc_username_dlmarket}" />
<property name="password" value="${jdbc_password_dlmarket}" />
xxx...
<bean> <!-- 多数据源的映射关系 -->
<bean id="dataSource" class="com.current.util.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<!-- key的值必须要和静态键值对照类中的值相同 -->
<entry value-ref="analyseDataSource" key="Analyse"></entry>
<entry value-ref="dlmarketDataSource" key="DLmarket"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="analyseDataSource"></property>
</bean>
其他的SessionFactory 事务管理器配置都不需要修改。
5、在Action中切换数据源
public void getList(){
DataSourceContextHolder.setCustomerType(DataSourceMap.DLmarket);
int id=1;
List<Chaining> chainList = chainService.getList(id);
System.out.println(chainList.get(0).getChaining());
}
SSH配置动态数据源的更多相关文章
- 如何通过Spring Boot配置动态数据源访问多个数据库
之前写过一篇博客<Spring+Mybatis+Mysql搭建分布式数据库访问框架>描述如何通过Spring+Mybatis配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中 ...
- SpringBoot整合MyBatisPlus配置动态数据源
目录 SpringBoot整合MyBatisPlus配置动态数据源 SpringBoot整合MyBatisPlus配置动态数据源 推文:2018开源中国最受欢迎的中国软件MyBatis-Plus My ...
- SSM配置动态数据源
多数据源配置主要涉及自定义类(DataSource注解类.DataSourceAspect切面类,动态数据源接口实现类.以及数据源字符串线程保存类),pom.xml文件.applicationCont ...
- Spring配置动态数据源-读写分离和多数据源
在现在互联网系统中,随着用户量的增长,单数据源通常无法满足系统的负载要求.因此为了解决用户量增长带来的压力,在数据库层面会采用读写分离技术和数据库拆分等技术.读写分离就是就是一个Master数据库,多 ...
- 使用Spring配置动态数据源实现读写分离
最近搭建的一个项目需要实现数据源的读写分离,在这里将代码进行分享,以供参考.关键词:DataSource .AbstractRoutingDataSource.AOP 首先是配置数据源 <!-- ...
- SpringBoot集成Mybatis配置动态数据源
很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...
- 使用 Spring 配置动态数据源实现读写分离
关键词:DataSource .AbstractRoutingDataSource.AOP 首先是配置数据源 <!--读数据源配置--><bean id="readData ...
- 阿里P7教你如何使用 Spring 配置动态数据源实现读写分离
最近搭建的一个项目需要实现数据源的读写分离,在这里将代码进行分享,以供参考. 关键词:DataSource .AbstractRoutingDataSource.AOP 首先是配置数据源 <!- ...
- Spring-Boot 多数据源配置+动态数据源切换+多数据源事物配置实现主从数据库存储分离
一.基础介绍 多数据源字面意思,比如说二个数据库,甚至不同类型的数据库.在用SpringBoot开发项目时,随着业务量的扩大,我们通常会进行数据库拆分或是引入其他数据库,从而我们需要配置多个数据源. ...
随机推荐
- Recover Rotated Sorted Array
Given a rotated sorted array, recover it to sorted array in-place. Clarification What is rotated arr ...
- MPlayer-ww 增加边看边剪切功能
解压到 D:\MPlayer-ww 运行 copy_font.bat 安装字体 LED_font.ttf 双击 MPlayer_ww_openWith.reg 添加右键播放功能 outformat.i ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- DP:Islands and Bridges(POJ 2288)
2015-09-21 造桥基建工程 题目大意,就是有n座岛和k座桥,要你找一条哈密顿圈(找完所有的岛,并且每个岛只经过一次),当经过一座岛就加上岛的价值,如果两岛联通,则加上两座岛的价值之积,如果三座 ...
- IOS多线程(NSThread)
1.创建方法 使用NSThread创建线程主要有两个个方法,分别如下 NSThread* myThread = [[NSThread alloc] initWithTarget:self sele ...
- codeforces 479C Exams 解题报告
题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...
- WebService站点服务的地址
天气的地址 http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx
- Android开发方向
运行Android平台的硬件只是手机.平台电脑等便携式设备,这些设备的计算能力.数据存储能力都是有限的, 不太可能在Android平台上部署大型企业级应用,因此Android应用可能以纯粹客户端应用的 ...
- Centos 6.4 32位 gcc 升级(已验证)
具体需要升级成什么版本自行下载https://gcc.gnu.org/ 本文升级为4.8.5 1.下载编译所需依赖库 cd gcc-4.8.5 ./contrib/download_prerequis ...
- Centos升级内核 --已验证
Docekr 对内核由要求,3.10以上 Centos 6.5内核达不到要求 [linux@localhost Desktop]$ sudo service docker statusdocker d ...