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

原文:http://blog.csdn.net/isea533/article/details/46815385 

 

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

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

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

多数据源的可能状况

1.主从

一般是MySql一主多从的状况,本文的例子就是主从的状况,可是只有两个数据源,因此采用直接配置不会太麻烦,可是不利于后续扩展,主要是做为一个例子来讲明,实际操做请慎重考虑。数据库

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

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

还有一个经过SpringAbstractRoutingDataSource路由接口的方式:微信

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

2.分库

当业务独立性强,数据量大的时候的,为了提升并发,可能会对表进行分库,分库后,每个数据库都须要配置一个数据源。并发

这种状况能够参考本文,可是须要注意每个数据库对应的Mapper要在不一样的包下方便区分和配置。app

另外分库的状况下也会存在主从的状况,若是你的数据库从库过多,就参考上面提供的方法,或者寻找其余方式解决。

Mapper分包

分库的状况下,不一样的数据库的Mapper必定放在不一样的包下。

主从的状况下,同一个Mapper会同时存在读写的状况,建立两个并不合适,使用同一个便可。可是这种状况下须要注意,Spring对Mapper自动生成的名字是相同的,并且类型也相同,这是就不能直接注入Mapper接口。须要经过SqlSession来解决。

Spring基础配置

applicationContext.xml

   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
<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>

这个文件,主要是引入了spring-datasource-master.xmlspring-datasource-slave.xml

spring-datasource-master.xml

   
   
   
   
  • 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
<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>

spring-datasource-slave.xml

master区别不大,主要是id名字和数据源配置有区别。

   
   
   
   
  • 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
<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>

这里须要注意<tx:method name="*" read-only="true"/>是只读的。若是不是从库,能够按主库进行配置。

在下面代码中:

   
   
   
   
  • 1
  • 2
  • 3
  • 4
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.isea533.mybatis.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/> </bean>

必须经过sqlSessionFactoryBeanName来指定不一样的sqlSessionFactory

config.properties

   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
# 数据库配置 - 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

使用Mapper

这里是针对主从的状况进行设置的,两个配置扫描的Mapper是同样的,因此无法直接注入,须要经过下面的麻烦方式注入。

   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
@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); } }

由于sqlSession能经过name区分开,因此这里从sqlSession获取Mapper

另外若是须要考虑在同一个事务中写读的时候,须要使用相同的writeMapper,这样在读的时候,才能获取事务中的最新数据。

以上是主从的状况。

在分库的状况时,因为不一样Mapper在不一样的包下,因此能够直接使用@Resource或者@Autowired注入Mapper,不须要经过sqlSession获取。

本篇文章,只是一个多数据源的参考,实际应用时,请根据本身的状况进行考虑。

后续,我会利用业余时间,在本文和上面两个相关连接的基础上,针对MySql多数据源,尝试开发能够自动切换数据源的插件,由于我对这方面的实际应用不是很熟,因此欢迎你们留言分享本身的解决方案,对这些了解的越多,就越有可能开发出通用的数据源切换插件。

3
相关文章
相关标签/搜索