日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

开源框架springboot-mybatis-wr-separation实现springboot+mybatis读写分离

發布時間:2024/3/12 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 开源框架springboot-mybatis-wr-separation实现springboot+mybatis读写分离 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近做springboot+mybatis的項目想要用到讀寫分離,查了一圈發現大家都是自己寫的,沒用通用現成的讀寫分離架構,因此就寫了一個比較簡單好用的小插件,來幫助大家簡單的實現讀寫分離的功能,項目已開源并已經上傳到maven中央倉庫,歡迎提出寶貴意見,共同改進。

springboot-mybatis-wr-separation

介紹
springboot-mybatis-wr-separation 是一個基于maven的項目,依賴springboot和mybatis,提供讀寫分離的功能的開源項目插件

軟件架構
1. 使用jdk1.7
2. 依賴spring-boot-starter-parent(1.5.4.RELEASE)/spring-boot-starter-aop/mybatis-spring-boot-starter(1.3.1)

部署說明

1. 直接進行jar包依賴即可

<dependency><groupId>com.gitee.sunchenbin</groupId><artifactId>springboot-mybatis-wr-separation</artifactId><version>0.0.4.RELEASE</version></dependency>

2. application.properties中需要做如下配置

? ? ? ? (1) mybatis的配置

mybatis.mapperLocations=classpath:sql-mapper/**/*.xmlmybatis.typeAliasesPackage=com.xxx.api.xxx.model,com.xxx.api.xxx.command

? ? ? ? (2) 讀庫和寫庫的配置? ?

spring.datasource.write.url=jdbc:postgresql://127.0.0.1/db-masterspring.datasource.write.username=rootspring.datasource.write.password=123456spring.datasource.write.driver-class-name=org.postgresql.Driverspring.datasource.read.url=jdbc:postgresql://127.0.0.1/db-slavespring.datasource.read.username=rootspring.datasource.read.password=123456spring.datasource.read.driver-class-name=org.postgresql.Driver

? ? ? ??(3) 讀寫分離切點的expression表達式(建議切manager或者service)? ?

wr.separation.pointcut.expression=execution(public * com.xxx..*.*(..))


3. 應用插件的springboot項目需要配置掃碼插件的包,即@ComponentScan需要配置對"com.gitee.sunshine.*"包路徑的掃描

使用說明

1.?本插件提供一個注解,@ReadDB(走讀庫),如果不打注解默認走寫庫

2.?注解適用范圍,必須是spring管理的bean的方法,注解不支持打在接口方法上,通常來說建議在manager層service層的方法上使用

3.?注解生命周期,為了避免讀庫寫庫來回切換,產生的事務問題,這里規定,當前線程只會使用一個庫去執行操作,比如a.test1()被調用時,首先a是被spring管理的類,并且他在我們配置文件“讀寫分離切點表達式”的范圍內,那么此時,如果test1()打了注解@ReadDB,那么從此開始直到這個線程結束了,都使用讀庫,如果沒有打注解,那么從此開始知道整個線程結束了都使用寫庫。

4.?沒有被切到的方法如果使用了數據庫操作,默認走寫庫

springboot-mybatis-wr-separation插件開源地址

  • 碼云地址:https://gitee.com/sunchenbin/springboot-mybatis-wr-separation

  • 代碼下載地址:https://git.oschina.net/sunchenbin/springboot-mybatis-wr-separation.git

  • 總結

    以上是生活随笔為你收集整理的开源框架springboot-mybatis-wr-separation实现springboot+mybatis读写分离的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。