MyBatis多数据源配置(读写分离)

MyBatis多数据源配置(读写分离)

首先说明,本文的配置使用的最直接的方式,实际用起来可能会很麻烦。

实际应用中可能存在多种结合的情况,你可以理解本文的含义,不要死板的使用。

多数据源的可能情况

1.主从

通常是MySQL一主多从的情况,本文的例子就是主从的情况,但是只有两个数据源,所以采用直接配置不会太麻烦,但是不利于后续扩展,主要是作为一个例子来说明,实际操作请慎重考虑。

针对这种情况,一个更好的解决方法可以参考(本人没有实际尝试过):

http://blog.csdn.net/lixiucheng005/article/details/17391857

还有一个通过SpringAbstractRoutingDataSource路由接口的方式:

http://blog.csdn.net/xtj332/article/details/43953699

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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

这个文件,主要是引入了spring-datasource-master.xmlspring-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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://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
  • 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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://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
  • 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
  • 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
  • 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
  • 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多数据源,尝试开发可以自动切换数据源的插件,因为我对这方面的实际应用不是很熟,所以欢迎大家留言分享自己的解决方案,对这些了解的越多,就越有可能开发出通用的数据源切换插件。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/387707.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

UI简单工作

UI用户界面 需求——效果图——风格设计——高保证效果——html 网页的宽度屏幕的宽度-纵向滚动条的宽度 企业网站一般是1280 根据百度流量研究所 目前我们的网页注主要是1024和1200 这样的宽度符合大体市场 首屏高度。 首屏的概念来源于出版领域 报纸折叠后贩卖&…

MySQL分库分表总结

MySQL分库分表总结&#xff1a; 单库单表 &#xff1a; 单库单表是最常见的数据库设计&#xff0c;例如&#xff0c;有一张用户(user)表放在数据库db中&#xff0c;所有的用户都可以在db库中的user表中查到。 单库多表 &#xff1a; 随着用户数量的增加&#xff0c;user表的数…

3章 RxJava操作符

本篇文章已授权微信公众号 YYGeeker 独家发布转载请标明出处 CSDN学院课程地址 RxJava2从入门到精通-初级篇:edu.csdn.net/course/deta…RxJava2从入门到精通-中级篇:edu.csdn.net/course/deta…RxJava2从入门到精通-进阶篇:edu.csdn.net/course/deta…RxJava2从入门到精通-源码…

virtualbox 使用

实现文件拖拽功能 1、设备 -- 安装增强功能 -- /bin/sh VboxLinuxaddition.run -- reboot 2、设备 -- 拖放 -- 双向 3、虚拟机 -- 设置 -- 存储 -- 控制器&#xff1a;SATA -- 勾选 使用主机输入输出&#xff08;I\O 缓存&#xff09; 4、虚拟机硬盘 -- 勾选固态驱动器 转载于…

linux安装mysql 5.6.33

.到MySQL官网下载mysql编译好的二进制安装包&#xff0c;在下载页面Select Platform:选项选择linux-generic&#xff0c;然后把页面拉到底部&#xff0c;64位系统下载Linux - Generic (glibc 2.5) (x86, 64-bit)&#xff0c;下载后文件名&#xff1a;mysql-5.6.33-linux-glibc2…

Go 函数特性和网络爬虫示例

爬取页面 这篇通过网络爬虫的示例&#xff0c;来了解 Go 语言的递归、多返回值、延迟函数调用、匿名函数等方面的函数特性。首先是爬虫的基础示例&#xff0c;下面两个例子展示通过 net/http 包来爬取页面的内容。 获取一个 URL 下面的程序展示从互联网获取信息&#xff0c;获…

Qt的安装和使用中的常见问题(详细版)

对于太长不看的朋友&#xff0c;可参考Qt的安装和使用中的常见问题&#xff08;简略版&#xff09;。 目录 1、引入2、Qt简介3、Qt版本 3.1 查看安装的Qt版本3.2 查看当前项目使用的Qt版本3.3 查看当前项目使用的QtCreator版本3.4 Linux命令行下查看和使用不同版本的Qt4、Qt模块…

python与C#的互相调用

python与C#的互相调用一、C#调用python新建一个项目&#xff0c;添加引用&#xff1a;IronPython.dll&#xff0c;Microsoft.Scripting.dll&#xff08;在IronPython的安装目录中&#xff09;。创建一个文本文件命名为hello.py,把该文件添加的当前的项目中,并设置为总是输出。#…

各行业大数据可视化界面参考

转载于:https://www.cnblogs.com/wangsongbai/p/10178096.html

mysql远程连接 Host * is not allowed to connect to this MySQL server

localhost改成% 进入mysql的BIN目录 代码如下 复制代码 mysql -u root -p mysql>use mysql; mysql>update user set host ’%where user ’root’; mysql>flush privileges; 具体分析 1、在本机登入mysql后&#xff0c;更改“mysql”数据库里的“user”表里的“h…

今日听闻这几款手机软件比较火爆 果然名不虚传!

如今的时代&#xff0c;智能手机已经成为我们生活中不可缺少的一部分&#xff0c;大家之所以这么爱玩手机&#xff0c;其实并不是手机本身有多么吸引人&#xff0c;而是安装在手机上的各种各样的APP&#xff0c;比如各种社交软件、音频软件、购物软件以及地图软件等等。下面我们…

setdefault()方法

setdefault()方法 描述 字典 setdefault() 方法和 get()方法类似,返回指定键的值&#xff0c;如果键不在字典中&#xff0c;将会添加键并将值设置为一个指定值&#xff0c;默认为None。 get() 和 setdefault() 区别&#xff1a; setdefault() 返回的键如果不在字典中&#xff0…

Hive2.1.1、Hadoop2.7.3 部署

本文以远程模式安装Hive2.1.1将hive的元数据放置在MySQL数据库中。 1 安装mysql数据库 sudo apt-get install mysql-server11 重启mysql服务使得配置文件生效 sudo service mysql restart11 创建hive专用账户 CREATE USER hive% IDENTIFIED BY 123456;11 给hive账户授予所有权限…

Django 的简单ajax

需要通过ajax实现局部刷新 js代码 $(#guo-sou-ajax).click(function(){ #获取id为guo-sou-ajax点击后的信号console.log($(this).attr("data-action")) $.ajax({ #调用ajaxurl: $(this).attr("data-action"), #url保存在标签里面的data-actio…

postman提取返回值

Postman是做接口测试的&#xff0c;但是很多接口并不是直接就能测&#xff0c;有的需要一些预处理。比如说身份认证&#xff0c;需要传递一个token。如果做网页测试&#xff0c;一般打开登陆界面的时候就会生成一个token&#xff0c;如果返回值是json格式&#xff0c;用Postman…

docker下用keepalived+Haproxy实现高可用负载均衡集群

启动keepalived后宿主机无法ping通用keepalived&#xff0c;报错&#xff1a; [rootlocalhost ~]# ping 172.18.0.15 PING 172.18.0.15 (172.18.0.15) 56(84) bytes of data. From 172.18.0.1 icmp_seq1 Destination Host Unreachable From 172.18.0.1 icmp_seq2 Destination H…

hadoop hive 2.1.1 将Hive启动为服务

我们之前使用的Shell方式与Hive交互只是Hive交互方式中的一种&#xff0c;还有一种就是将Hive启动为服务&#xff0c;然后运行在一个节点上&#xff0c;那么剩下的节点就可以使用客户端来连接它&#xff0c;从而也可以使用Hive的数据分析服务。 前台模式 可以使用下面的命令来将…

大数据学习要知道的十大发展趋势,以及学习大数据的几点建议

2016年&#xff0c;近40%的公司正在实施和扩展大数据技术应用&#xff0c;另有30%的公司计划在未来12个月内采用大数据技术&#xff0c;62.5%的公司现在至少有一个大数据项目投入生产&#xff0c;只有5.4%的公司没有大数据应用计划&#xff0c;或者是没有正在进行的大数据项目&…

pickle 模块

import pickle # class Elephant:def __init__(self, name, weight, height):self.name nameself.weight weightself.height heightdef tiaoxi(self):print(f"{self.name}大象特别喜欢调戏人")# e Elephant("宝宝", "185T", "175"…

Hiv:SQuirrel连接hive配置

熟悉了Sqlserver的sqlserver management studio、Oracle的PL/SQL可视化数据库查询分析工具&#xff0c;在刚开始使用hive、phoenix等类sql组件时&#xff0c;一直在苦苦搜寻是否也有类似的工具&#xff0c;不负所望&#xff0c;SQuirrel Sql client 可视化数据库工具基本可满足…