spring-boot-mybatis-rw讀寫(xiě)分離插件
spring-boot-mybatis-rw
基于mybatis,springboot開(kāi)箱即用的讀寫(xiě)分離插件
Quick Start
介紹
此插件由以下2部分組成
datasource:讀寫(xiě)數(shù)據(jù)源的代理,支持一寫(xiě)多讀,用戶只需實(shí)現(xiàn) org.spring.boot.mybatis.rw.starter.datasource.AbstractReadRoutingDataSource這個(gè)類(lèi),實(shí)現(xiàn)自己讀數(shù)據(jù)源的負(fù)載均衡算法
pulgin:mybatis插件實(shí)現(xiàn)讀寫(xiě)路由
spring-boot 配置
spring.mybatis.rw.readDataSources[0].url=jdbc:MySql://localhost:3306/test?characterEncoding=UTF-8 spring.mybatis.rw.readDataSources[0].driverClassName=com.mysql.jdbc.Driver spring.mybatis.rw.readDataSources[0].username=root spring.mybatis.rw.readDataSources[0].password=123456 spring.mybatis.rw.readDataSources[1].url=jdbc:MySql://localhost:3306/test?characterEncoding=UTF-8 spring.mybatis.rw.readDataSources[1].driverClassName=com.mysql.jdbc.Driver spring.mybatis.rw.readDataSources[1].username=root spring.mybatis.rw.readDataSources[1].password=123456 spring.mybatis.rw.writeDataSource.url=jdbc:MySql://localhost:3306/chenlei?characterEncoding=UTF-8 spring.mybatis.rw.writeDataSource.driverClassName=com.mysql.jdbc.Driver spring.mybatis.rw.writeDataSource.username=root spring.mybatis.rw.writeDataSource.password=123456
XML配置
datasource:
<!--簡(jiǎn)單的一個(gè)master和多個(gè)slaver 讀寫(xiě)分離的數(shù)據(jù)源 --> <bean id="dataSource" class="org.spring.boot.mybatis.rw.starter.datasource.impl.RoundRobinRWRoutingDataSourceProxy"> <property name="writeDataSource" ref="writeDS"/> <property name="readDataSoures"> <list> <ref bean="readDS"/> <ref bean="readDS"/> <ref bean="readDS"/> </list> </property> </bean>
總結(jié)
只需將數(shù)據(jù)源和事務(wù)工廠注入到sqlSessionFactory中,其他配置不變,便實(shí)現(xiàn)讀寫(xiě)分離,對(duì)代碼0入侵,配置簡(jiǎn)單,非常方便老項(xiàng)目的遷移。 詳細(xì)配置
評(píng)論
圖片
表情
