PS:使用AbstractRoutingDataSource路由数据源实现动态数据库的调用
1、 连接哪个数据源的环境变量
package com.hysoft.common;
public class JdbcContextHolder {
private static final ThreadLocal<String> CONTEXTHOLDER = new ThreadLocal<String>();
public static void setJdbcType(String jdbcType) {
CONTEXTHOLDER.set(jdbcType);
}
/**
* 写读数据库
*/
public static void setWriteDataSource() {
setJdbcType("writeDataSource");
}
/**
* 设置读数据库
*/
public static void setReadyDataSource() {
clearJdbcType();
}
public static String getJdbcType() {
return (String) CONTEXTHOLDER.get();
}
public static void clearJdbcType() {
CONTEXTHOLDER.remove();
}
}
2、建立动态数据源类,这个类必须继承AbstractRoutingDataSource
package com.hysoft.common;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return JdbcContextHolder.getJdbcType();
}
}
3、配置数据库初始化xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 数据库连接 -->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
</bean> -->
<!-- ready only DataSource -->
<bean id="readyDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.ready.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.ready.url}" />
<property name="user" value="${jdbc.ready.username}" />
<property name="password" value="${jdbc.ready.password}" />
<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
</bean>
<!-- write only DataSource -->
<bean id="writeDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.write.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.write.url}" />
<property name="user" value="${jdbc.write.username}" />
<property name="password" value="${jdbc.write.password}" />
<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
</bean>
<!-- 动态路由数据库连接 ,默认是读数据库 -->
<bean id="mySqlDataSource" class="com.hysoft.common.DynamicDataSource">
<property name="targetDataSources">
<map>
<entry key="writeDataSource" value-ref="writeDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="readyDataSource"/>
</bean>
<!-- sessionFactory 将spring和mybatis整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="mySqlDataSource" />
<!-- 自动扫描xml目录,省掉手工配置 -->
<property name="mapperLocations" value="classpath:mapping/*/*.xml" />
<property name="plugins">
<list>
<ref bean="pageInterceptor" />
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hysoft.*.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- AOP事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="mySqlDataSource" />
</bean>
<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
4、在serverice层指出使用的数据源
public Map<String, Object> getAllUserByPage(Map<String, Object> paramMap, String rows, String page){
//主库 读 (默认,可以不写)
JdbcContextHolder.setReadyDataSource();
//辅库 写
// JdbcContextHolder.setWriteDataSource();
Map<String, Object> resultMap = new HashMap<String, Object>();
PageParameter mypage = getPageMap(paramMap, Integer.valueOf(page), Integer.valueOf(rows));
List<UserInfoDomain> rtnList = userInfoMapper.getAllUserByPage(paramMap);
resultMap.put("total", mypage.getTotalCount());
resultMap.put("rows", rtnList);
return resultMap;
}
/**
* 新增数据
* @param view
*/
public void saveUser(UserInfoDomain domain){
JdbcContextHolder.setWriteDataSource();
this.userInfoMapper.insert(domain);
}
/**
* 编辑数据
* @param view
*/
public void updateUser(UserInfoDomain domain){
JdbcContextHolder.setWriteDataSource();
this.userInfoMapper.updateByPrimaryKeySelective(domain);
}
- 基于 EntityFramework 的数据库主从读写分离服务插件
基于 EntityFramework 的数据库主从读写分离服务插件 1. 版本信息和源码 1.1 版本信息 v1.01 beta(2015-04-07),基于 EF 6.1 开发,支持 EF 6.1 ...
- 利用oneproxy部署mysql数据库的读写分离
实验系统:CentOS 6.6_x86_64 实验前提:防火墙和selinux都关闭 实验说明:本实验共有4台主机,IP分配如拓扑 实验软件:mariadb-10.0.20 oneproxy-rhel ...
- MySQL搭建主从数据库 实现读写分离
首先声明,实际生产中,网站为了提高用户体验,性能等,将数据库实现读写分离是有必要的,我们让主数据库去写入数据,然后当用户查询的时候,然后在从数据库读取数据,故能减轻数据库的压力,实现良好的用户体验! ...
- Mycat - 实现数据库的读写分离与高可用
前言 开心一刻 上语文课,不小心睡着了,坐在边上的同桌突然叫醒了我,并小声说道:“读课文第三段”.我立马起身大声读了起来.正在黑板写字的老师吓了一跳,老师郁闷的看着我,问道:“同学有什么问题吗?”,我 ...
- 基于 EntityFramework 的数据库主从读写分离架构 - 目录
基于 EntityFramework 的数据库主从读写分离架构 回到目录,完整代码请查看(https://github.com/cjw0511/NDF.Infrastructure)中的目 ...
- 基于 EntityFramework 的数据库主从读写分离架构(1) - 原理概述和基本功能实现
回到目录,完整代码请查看(https://github.com/cjw0511/NDF.Infrastructure)中的目录: src\ NDF.Data.EntityFramew ...
- 如何轻松实现MySQL数据库的读写分离和负载均衡?
配置好了 Mysql 的主从复制结构后,我们希望实现读写分离,把读操作分散到从服务器中,并且对多个从服务器能实现负载均衡.读写分离和负载均衡是 Mysql 集群的基础需求,MaxScale 就可以帮着 ...
- springboot+mybatis实现数据库的读写分离
介绍 随着业务的发展,除了拆分业务模块外,数据库的读写分离也是常见的优化手段.方案使用了AbstractRoutingDataSource和mybatis plugin来动态的选择数据源选择这个方案的 ...
- 利用mysql-proxy进行mysql数据库的读写分离
实验系统:CentOS 6.6_x86_64 实验前提:防火墙和selinux都关闭 实验说明:本实验共有4台主机,IP分配如拓扑 实验软件:mariadb-10.0.20 mysql-proxy-0 ...
随机推荐
- 响应式布局框架 Pure-CSS 5.0 示例中文版-下
10. 表格 Tables 在 table 标签增加 .pure-table 类 <table class="pure-table"> <thead> &l ...
- Objective-C函数重载规则
是按照函数标签是否重复来判断是否为一个重载函数的.
- Objective-c的@property 详解
转自:http://www.cnblogs.com/andyque/archive/2011/08/03/2125728.html 之前很多网友对我翻译的教程中的Property的使用感到有些迷惑不解 ...
- iPhone How-to:如何调整UIView的Z-Order
转自:http://bj007.blog.51cto.com/1701577/541572 在界面设计中,最终用户看到的呈现通常是由不同层的视图组成的,通过控制视图的层次就可以实现不同的效果和功能.而 ...
- android线程控制UI更新(Handler 、post()、postDelayed()、postAtTime)
依照以下的理解就是handler与ui线程有一定的关联能够由于更新界面仅仅能在主线程中全部更新界面的地方能够在接受消息的handleMessage那里还有更新界面能够在handler.port(new ...
- 使用tc对linux中某ip段限速
TC 无需安装,Linux 内核自带例:将IP地址段192.168.1.0/24 上传下载限速为 5M将以下内容添加到/etc/ppp/ip-up文件exit 0上面. down=5Mbituploa ...
- Spring学习10-SpringMVC入门
二.SpringMVC请求处理流程 其中Front controller :前端控制器 Controller:后端控制器 三.Spring核心组件及请求处理流程
- 419. Roman to Integer【medium】
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- linux下编译 静态库、动态库,动态库依赖静态库
xx.o : xx.h xx.cpp -lstdc++ -o xx.o -c xx.cpp -I ./ libxx.a : xx.o ar -crv libxx.a xx.o libTest.so : ...
- Python高级编程之生成器(Generator)与coroutine(一):Generator
转载请注明出处:点我 这是一系列的文章,会从基础开始一步步的介绍Python中的Generator以及coroutine(协程)(主要是介绍coroutine),并且详细的讲述了Python中coro ...