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开发项目时,随着业务量的扩大,我们通常会进行数据库拆分或是引入其他数据库,从而我们需要配置多个数据源. ...
随机推荐
- ExecutorService 和 NSOperationQueue
ExecutorService,简化了Android中的并发处理,NSOperationQueue简化了iOS中的并发处理.它们都管理线程池,作用十分相近,下面简单说明一下. 1.ExecutorSe ...
- android 初探
2014年7月27日 15:02:57 附: android 官方培训课程中文版 //官方简单的入门教程, 每个大类中只介绍了几个知识点, 可以快速搭建一个hello world android 开发 ...
- sphinx 增量索引与主索引使用测试
2013年10月28日 15:01:16 首先对新增的商品建立增量索引,搜索时只使用增量索引: array (size=1) 0 => array (size=6) 'gid' => st ...
- -fomit-frame-pointer 编译选项在gcc 4.8.2版本中的汇编代码研究
#include void fun(void) { printf("fun"); } int main(int argc, char *argv[]){ fun(); return ...
- Java for LeetCode 042 Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【JAVA、C++】LeetCode 015 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 信与信封问题(codevs 1222)
题目描述 Description John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出.但是,第二天John的儿子Small John将这n封信都拿出了信封.不幸的是,Small Joh ...
- C++里的静态成员函数不能用const的原因
From http://blog.csdn.net/freeboy1015/article/details/7634950 static在c++中的第五种含义:用static修饰不访问非静态数 ...
- Quartus signal tapii 的使用
此功能原来已经试验过,没有笔记.这次复习巩固下. 使用PLL 的程序. 1.新建signaltap ii 文件 注意以下几个地方,会用到 添加采样时钟 . 添加采样信号: 完成之后,编译下载 运行 两 ...
- JAVA基础学习之throws和throw的区别、Java中的四种权限、多线程的使用等(2)
1.throws和throw的区别 throws使用在函数外,是编译时的异常,throw使用在函数内,是运行时的异常 使用方法 public int method(int[] arr) throws ...