网站开发组播地址的作用,wordpress免费有趣插件,樟木头镇仿做网站,支付宝网站开发文档MyBatis多数据源配置(读写分离)
首先说明#xff0c;本文的配置使用的最直接的方式#xff0c;实际用起来可能会很麻烦。
实际应用中可能存在多种结合的情况#xff0c;你可以理解本文的含义#xff0c;不要死板的使用。
多数据源的可能情况
1.主从
通常是MySQL一主多…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 xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://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.xsdcontext:component-scan base-packagecom.isea533.mybatis.service/context:property-placeholder locationclasspath:config.properties/aop:aspectj-autoproxy/import resourcespring-datasource-master.xml/import resourcespring-datasource-slave.xml/
/beans123456789101112131415161718123456789101112131415161718
这个文件主要是引入了spring-datasource-master.xml和spring-datasource-slave.xml。
spring-datasource-master.xml
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:txhttp://www.springframework.org/schema/tx xmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://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.xsdbean iddataSourceMaster classcom.alibaba.druid.pool.DruidDataSource init-methodinit destroy-methodcloseproperty namedriverClassName value${master.jdbc.driverClass}/property nameurl value${master.jdbc.url}/property nameusername value${master.jdbc.user}/property namepassword value${master.jdbc.password}/property namefilters valuestat/property namemaxActive value20/property nameinitialSize value1/property namemaxWait value60000/property nameminIdle value1/property nametimeBetweenEvictionRunsMillis value60000/property nameminEvictableIdleTimeMillis value300000/property namevalidationQuery valueSELECT x/property nametestWhileIdle valuetrue/property nametestOnBorrow valuefalse/property nametestOnReturn valuefalse//beanbean idsqlSessionFactory1 classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSourceMaster/property namemapperLocationsarrayvalueclasspath:mapper/*.xml/value/array/property/beanbean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.isea533.mybatis.mapper/property namesqlSessionFactoryBeanName valuesqlSessionFactory1//beanbean idsqlSessionMaster classorg.mybatis.spring.SqlSessionTemplate scopeprototypeconstructor-arg index0 refsqlSessionFactory1//beanaop:configaop:pointcut idappService expressionexecution(* com.isea533.mybatis.service..*Service*.*(..))/aop:advisor advice-reftxAdvice1 pointcut-refappService//aop:configtx:advice idtxAdvice1 transaction-managertransactionManager1tx:attributestx:method nameselect* read-onlytrue/tx:method namefind* read-onlytrue/tx:method nameget* read-onlytrue/tx:method name*//tx:attributes/tx:advicebean idtransactionManager1 classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSourceMaster//bean
/beans1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727312345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
spring-datasource-slave.xml
和master区别不大主要是id名字和数据源配置有区别。
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://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.xsdbean iddataSourceSlave classcom.alibaba.druid.pool.DruidDataSource init-methodinit destroy-methodcloseproperty namedriverClassName value${slave.jdbc.driverClass}/property nameurl value${slave.jdbc.url}/property nameusername value${slave.jdbc.user}/property namepassword value${slave.jdbc.password}/property namefilters valuestat/property namemaxActive value20/property nameinitialSize value1/property namemaxWait value60000/property nameminIdle value1/property nametimeBetweenEvictionRunsMillis value60000/property nameminEvictableIdleTimeMillis value300000/property namevalidationQuery valueSELECT x/property nametestWhileIdle valuetrue/property nametestOnBorrow valuefalse/property nametestOnReturn valuefalse//beanbean idsqlSessionFactory2 classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSourceSlave/property namemapperLocationsarrayvalueclasspath:mapper/*.xml/value/array/property/beanbean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.isea533.mybatis.mapper/property namesqlSessionFactoryBeanName valuesqlSessionFactory2//beanbean idsqlSessionSlave classorg.mybatis.spring.SqlSessionTemplate scopeprototypeconstructor-arg index0 refsqlSessionFactory2//beanaop:configaop:pointcut idappService expressionexecution(* com.isea533.mybatis.service..*Service*.*(..))/aop:advisor advice-reftxAdvice2 pointcut-refappService//aop:configtx:advice idtxAdvice2 transaction-managertransactionManager2tx:attributestx:method name* read-onlytrue//tx:attributes/tx:advicebean idtransactionManager2 classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSourceSlave//bean
/beans12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970711234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
这里需要注意tx:method name* read-onlytrue/是只读的。如果不是从库可以按主库进行配置。
在下面代码中
bean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.isea533.mybatis.mapper/property namesqlSessionFactoryBeanName valuesqlSessionFactory2/
/bean12341234
必须通过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 jj12345678910111234567891011
使用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 ListCountry selectPage(int pageNum, int pageSize) {PageHelper.startPage(pageNum, pageSize);return readMapper.select(null);}
}12345678910111213141516171819202122231234567891011121314151617181920212223
因为sqlSession能通过name区分开所以这里从sqlSession获取Mapper。
另外如果需要考虑在同一个事务中写读的时候需要使用相同的writeMapper这样在读的时候才能获取事务中的最新数据。
以上是主从的情况。
在分库的情况时由于不同Mapper在不同的包下所以可以直接使用Resource或者Autowired注入Mapper不需要通过sqlSession获取。
本篇文章只是一个多数据源的参考实际应用时请根据自己的情况进行考虑。
后续我会利用业余时间在本文和上面两个相关链接的基础上针对MySql多数据源尝试开发可以自动切换数据源的插件因为我对这方面的实际应用不是很熟所以欢迎大家留言分享自己的解决方案对这些了解的越多就越有可能开发出通用的数据源切换插件。