MyBatis多数据源配置(读写分离)
原文:http://blog.csdn.net/isea533/article/details/46815385
MyBatis多数据源配置(读写分离)
首先说明,本文的配置使用的最直接的方式,实际用起来可能会很麻烦。
实际应用中可能存在多种结合的情况,你可以理解本文的含义,不要死板的使用。
多数据源的可能情况
1.主从
通常是MySql一主多从的情况,本文的例子就是主从的情况,但是只有两个数据源,所以采用直接配置不会太麻烦,但是不利于后续扩展,主要是作为一个例子来说明,实际操作请慎重考虑。
针对这种情况,一个更好的解决方法可以参考(本人没有实际尝试过):
还有一个通过SpringAbstractRoutingDataSource
路由接口的方式:
2.分库
当业务独立性强,数据量大的时候的,为了提高并发,可能会对表进行分库,分库后,每一个数据库都需要配置一个数据源。
这种情况可以参考本文,但是需要注意每一个数据库对应的Mapper要在不同的包下方便区分和配置。
另外分库的情况下也会存在主从的情况,如果你的数据库从库过多,就参考上面提供的方法,或者寻找其他方式解决。
Mapper分包
分库的情况下,不同的数据库的Mapper一定放在不同的包下。
主从的情况下,同一个Mapper会同时存在读写的情况,创建两个并不合适,使用同一个即可。但是这种情况下需要注意,Spring对Mapper自动生成的名字是相同的,而且类型也相同,这是就不能直接注入Mapper
接口。需要通过SqlSession
来解决。
Spring基础配置
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.isea533.mybatis.service"/> <context:property-placeholder location="classpath:config.properties"/> <aop:aspectj-autoproxy/> <import resource="spring-datasource-master.xml"/> <import resource="spring-datasource-slave.xml"/> </beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
这个文件,主要是引入了spring-datasource-master.xml
和spring-datasource-slave.xml
。
spring-datasource-master.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${master.jdbc.driverClass}"/> <property name="url" value="${master.jdbc.url}"/> <property name="username" value="${master.jdbc.user}"/> <property name="password" value="${master.jdbc.password}"/> <property name="filters" value="stat"/> <property name="maxActive" value="20"/> <property name="initialSize" value="1"/> <property name="maxWait" value="60000"/> <property name="minIdle" value="1"/> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="SELECT 'x'"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> </bean> <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSourceMaster"/> <property name="mapperLocations"> <array> <value>classpath:mapper/*.xml</value> </array> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.isea533.mybatis.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"/> </bean> <bean id="sqlSessionMaster" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory1"/> </bean> <aop:config> <aop:pointcut id="appService" expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/> <aop:advisor advice-ref="txAdvice1" pointcut-ref="appService"/> </aop:config> <tx:advice id="txAdvice1" transaction-manager="transactionManager1"> <tx:attributes> <tx:method name="select*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSourceMaster"/> </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
- 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
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
spring-datasource-slave.xml
和master
区别不大,主要是id
名字和数据源配置有区别。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSourceSlave" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${slave.jdbc.driverClass}"/> <property name="url" value="${slave.jdbc.url}"/> <property name="username" value="${slave.jdbc.user}"/> <property name="password" value="${slave.jdbc.password}"/> <property name="filters" value="stat"/> <property name="maxActive" value="20"/> <property name="initialSize" value="1"/> <property name="maxWait" value="60000"/> <property name="minIdle" value="1"/> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="SELECT 'x'"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> </bean> <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSourceSlave"/> <property name="mapperLocations"> <array> <value>classpath:mapper/*.xml</value> </array> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.isea533.mybatis.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/> </bean> <bean id="sqlSessionSlave" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory2"/> </bean> <aop:config> <aop:pointcut id="appService" expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/> <aop:advisor advice-ref="txAdvice2" pointcut-ref="appService"/> </aop:config> <tx:advice id="txAdvice2" transaction-manager="transactionManager2"> <tx:attributes> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSourceSlave"/> </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
- 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
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
这里需要注意<tx:method name="*" read-only="true"/>
是只读的。如果不是从库,可以按主库进行配置。
在下面代码中:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.isea533.mybatis.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/> </bean>
- 1
- 2
- 3
- 4
必须通过sqlSessionFactoryBeanName
来指定不同的sqlSessionFactory
。
config.properties
# 数据库配置 - Master master.jdbc.driverClass = com.mysql.jdbc.Driver master.jdbc.url = jdbc:mysql://192.168.1.11:3306/test master.jdbc.user = root master.jdbc.password = jj # - Slave slave.jdbc.driverClass = com.mysql.jdbc.Driver slave.jdbc.url = jdbc:mysql://192.168.1.22:3306/test slave.jdbc.user = root slave.jdbc.password = jj
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
使用Mapper
这里是针对主从的情况进行设置的,两个配置扫描的Mapper
是一样的,所以没法直接注入,需要通过下面的麻烦方式注入。
@Service public class DemoService { private CountryMapper writeMapper; private CountryMapper readMapper; @Resource(name = "sqlSessionMaster") public void setWriteMapper(SqlSession sqlSession) { this.writeMapper = sqlSession.getMapper(CountryMapper.class); } @Resource(name = "sqlSessionSlave") public void setReadMapper(SqlSession sqlSession) { this.readMapper = sqlSession.getMapper(CountryMapper.class); } public int save(Country country){ return writeMapper.insert(country); } public List<Country> selectPage(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); return readMapper.select(null); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
因为sqlSession
能通过name
区分开,所以这里从sqlSession
获取Mapper
。
另外如果需要考虑在同一个事务中写读的时候,需要使用相同的writeMapper
,这样在读的时候,才能获取事务中的最新数据。
以上是主从的情况。
在分库的情况时,由于不同Mapper在不同的包下,所以可以直接使用@Resource
或者@Autowired
注入Mapper
,不需要通过sqlSession
获取。
本篇文章,只是一个多数据源的参考,实际应用时,请根据自己的情况进行考虑。
后续,我会利用业余时间,在本文和上面两个相关链接的基础上,针对MySql多数据源,尝试开发可以自动切换数据源的插件,因为我对这方面的实际应用不是很熟,所以欢迎大家留言分享自己的解决方案,对这些了解的越多,就越有可能开发出通用的数据源切换插件。
- 顶
- 3
MyBatis多数据源配置(读写分离)的更多相关文章
- spring-boot 速成(9) druid+mybatis 多数据源及读写分离的处理
按上节继续学习,稍微复杂的业务系统,一般会将数据库按业务拆开,比如产品系统的数据库放在product db中,订单系统的数据库放在order db中...,然后,如果量大了,可能每个库还要考虑做读.写 ...
- 使用Spring配置动态数据源实现读写分离
最近搭建的一个项目需要实现数据源的读写分离,在这里将代码进行分享,以供参考.关键词:DataSource .AbstractRoutingDataSource.AOP 首先是配置数据源 <!-- ...
- 阿里P7教你如何使用 Spring 配置动态数据源实现读写分离
最近搭建的一个项目需要实现数据源的读写分离,在这里将代码进行分享,以供参考. 关键词:DataSource .AbstractRoutingDataSource.AOP 首先是配置数据源 <!- ...
- spring MVC、mybatis配置读写分离
spring MVC.mybatis配置读写分离 1.环境: 3台数据库机器,一个master,二台slave,分别为slave1,slave2 2.要实现的目标: ①使数据写入到master ②读数 ...
- spring+mybatis+mysql5.7实现读写分离,主从复制
申明:请尽量与我本博文所有的软件版本保持一致,避免不必要的错误. 所用软件版本列表:MySQL 5.7spring5mybaties3.4.6 首先搭建一个完整的spring5+springMVC5+ ...
- Mysql主从配置+读写分离
Mysql主从配置+读写分离 MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具.因此,我们首先要在系统中源码编译安装cmake工具. ...
- Spring Boot 2.X(五):MyBatis 多数据源配置
前言 MyBatis 多数据源配置,最近在项目建设中,需要在原有系统上扩展一个新的业务模块,特意将数据库分库,以便减少复杂度.本文直接以简单的代码示例,如何对 MyBatis 多数据源配置. 准备 创 ...
- Spring+mybatis 实现aop数据库读写分离,多数据库源配置
在数据库层面大都采用读写分离技术,就是一个Master数据库,多个Slave数据库.Master库负责数据更新和实时数据查询,Slave库当然负责非实时数据查询.因为在实际的应用中,数据库都是读多写少 ...
- mybatis用spring的动态数据源实现读写分离
一.环境: 三个mysql数据库.一个master,两个slaver.master写数据,slaver读数据. 二.原理: 借助Spring的 AbstractRoutingDataSource 这个 ...
随机推荐
- Coding the Matrix (0):映射、复数和域
1. 非常好的 Python 教程 <深入 Python 3.0> 以及 IBM 开发社区的博客探索 Python. 2. 子集: s 是 S 的子集 >>>S = {2 ...
- WEB前端开发规范
WEB前端开发规范 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档.本文档如有不对或者不合适的地方请及时提出, 经讨论决定后方可更改. 基本 ...
- C# 使用XML序列化对象(二)
在C# 使用XML序列化对象(一)中描述了使用XML序列化对象的最简单的实现. 现在我们来看看稍微复杂一点的情况: 现有两个类:A和B,B是A的派生类,如下所示: public class A { p ...
- android常用的弹出提示框
我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等 ...
- MOTT的学习(一)
MQTT的消息体 flag 此标志设置时,客户端或服务器尝试重新publish,PUBREL,subcribe或unsubscribe消息.这适用于消息, 其中的QoS的值大于零(0),并且需要确认. ...
- 搭建OpenWrt开发环境(包括编译过程)
OpenWrt是一个高度模块化.高度自动化的嵌入式linux发行版,其编译和安装过程比普通的linux发行版而言,要简单太多了.如果您是新手,您那恐惧的心大可放到肚子里,呵呵.对于新手来说最麻烦的恐怕 ...
- Maven-在eclipse中安装Maven插件
装IDE Plugins的方法有很多. 其一:在线安装 通过Help-->Install New Software的方式,输入HTTP地址来安装,简单易操作,但是也优缺点,就是下载速度慢,或者有 ...
- 【UVA 11462】 Age Sort(基数排序)
题 题意 给你最多2000000个数据,大小是1到99的数,让你排序输出. 分析 快排也可以过.不过这题本意是要基数排序(桶排序),就是读入年龄age, a[age]++,然后输出时,从1到99岁(看 ...
- 【POJ 1061】青蛙的约会
题 Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...
- 【poj1015】 Jury Compromise
http://poj.org/problem?id=1015 (题目链接) 题意 随机挑选n个人作为陪审团的候选人,然后再从这n个人中选m 人组成陪审团.选m人的办法是:控方和辩方会根据对候选人的喜欢 ...