spring?boot+mybatis-plus配置讀寫分離的操作
更新時間:2024年01月25日 14:48:31 作者:程序員阿明
這篇文章主要介紹了spring?boot+mybatis-plus配置讀寫分離的操作,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
對于mysql主從機制的讀寫分離,對于寫操作寫入主庫,讀取操作讀取從庫
一、安裝依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.3.6</version> </dependency>
二、配置文件配置數(shù)據(jù)源
spring: datasource: dynamic: #默認數(shù)據(jù)源 primary: master datasource: master: username: root password: xxxxx driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/xx?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8 slave: username: root password: xxx driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/xx?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
三、在service,如果是讀取操作的話,則需要加上注解@DS(“slave”)即可,可看如下示例
@Service public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService { //讀取走從庫 @Override @DS("slave") public List<TUser> getUserList(){ return baseMapper.selectList(null); } //寫操作默認走主庫 @Override @Transactional public String adUser(TUser user){ baseMapper.insert(user); return "OK"; } }
到此這篇關(guān)于spring boot+mybatis-plus配置讀寫分離的文章就介紹到這了,更多相關(guān)spring boot mybatis-plus讀寫分離內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java如何解析html中的內(nèi)容并存到數(shù)據(jù)庫詳解
最近用到了Java解析Html的一個庫Jsoup,所以下面這篇文章主要給大家介紹了關(guān)于Java如何解析html中的內(nèi)容并存到數(shù)據(jù)庫的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-03-03springboot前后臺數(shù)據(jù)交互的示例代碼
這篇文章主要介紹了springboot前后臺數(shù)據(jù)交互的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10詳解SpringMVC組件之HandlerMapping(一)
這篇文章主要介紹了詳解SpringMVC組件之HandlerMapping(一),HandlerMapping組件是Spring?MVC核心組件,用來根據(jù)請求的request查找對應(yīng)的Handler,在Spring?MVC中,有各式各樣的Web請求,每個請求都需要一個對應(yīng)的Handler來處理,需要的朋友可以參考下2023-08-08SpringBoot使用Thymeleaf自定義標簽的實例代碼
這篇文章主要介紹了SpringBoot使用Thymeleaf自定義標簽的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09