MyBatis-Plus多數(shù)據(jù)源配置與讀寫分離全過(guò)程
一、引言
在實(shí)際的項(xiàng)目開發(fā)中,我們常常會(huì)遇到需要操作多個(gè)數(shù)據(jù)庫(kù)的情況,比如純粹多庫(kù)、讀寫分離、一主多從、混合模式等。
本文將詳細(xì)介紹如何使用 MyBatis-Plus 實(shí)現(xiàn)純粹多庫(kù)的場(chǎng)景,并探討讀寫分離的實(shí)現(xiàn)思路。
二、環(huán)境準(zhǔn)備
- 開發(fā)工具:IntelliJ IDEA
- 數(shù)據(jù)庫(kù):MySQL
- 框架:Spring Boot、MyBatis-Plus
- 依賴管理:Maven
三、純粹多庫(kù)場(chǎng)景模擬
1. 創(chuàng)建數(shù)據(jù)庫(kù)及表
首先,我們要?jiǎng)?chuàng)建兩個(gè)數(shù)據(jù)庫(kù),分別是 mybatis_plus(原庫(kù)保持不變)和 mybatis_plus_1(新建),并將 mybatis_plus 庫(kù)的 product 表移到 mybatis_plus_1 庫(kù)。
-- 創(chuàng)建數(shù)據(jù)庫(kù) mybatis_plus_1
CREATE DATABASE `mybatis_plus_1` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
USE `mybatis_plus_1`;
-- 創(chuàng)建 product 表
CREATE TABLE product
(
id BIGINT(20) NOT NULL COMMENT '主鍵 ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名稱 ',
price INT(11) DEFAULT 0 COMMENT '價(jià)格 ',
version INT(11) DEFAULT 0 COMMENT '樂(lè)觀鎖版本號(hào) ',
PRIMARY KEY (id)
);
-- 添加測(cè)試數(shù)據(jù)
INSERT INTO product (id, NAME, price) VALUES (1, '外星人筆記本 ', 100);
-- 刪除 mybatis_plus 庫(kù)中的 product 表
-- 請(qǐng)先連接到 mybatis_plus 數(shù)據(jù)庫(kù)
USE mybatis_plus;
DROP TABLE IF EXISTS product;2. 引入依賴
在 pom.xml 中添加 dynamic-datasource-spring-boot-starter 依賴:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>3. 配置多數(shù)據(jù)源
在 application.yml 中配置多數(shù)據(jù)源:
spring:
# 配置數(shù)據(jù)源信息
datasource:
dynamic:
# 設(shè)置默認(rèn)的數(shù)據(jù)源或者數(shù)據(jù)源組 ,默認(rèn)值即為 master
primary: master
# 嚴(yán)格匹配數(shù)據(jù)源 ,默認(rèn) false.true 未匹配到指定數(shù)據(jù)源時(shí)拋異常 ,false 使用默認(rèn)數(shù)據(jù)源
strict: false
datasource:
master:
url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
slave_1:
url: jdbc:mysql://localhost:3306/mybatis_plus_1?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 1234564. 創(chuàng)建用戶 Service
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
// 指定所操作的數(shù)據(jù)源
@DS("master")
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}5. 創(chuàng)建商品 Service
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@DS("slave_1")
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}6. 測(cè)試
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class DynamicDataSourceTest {
@Autowired
private UserService userService;
@Autowired
private ProductService productService;
@Test
public void testDynamicDataSource() {
System.out.println(userService.getById(1L));
System.out.println(productService.getById(1L));
}
}如果測(cè)試時(shí)能順利獲取到用戶對(duì)象和商品對(duì)象,那就表明多庫(kù)模擬成功。
四、讀寫分離思路
如果要實(shí)現(xiàn)讀寫分離,可把寫操作方法加上主庫(kù)數(shù)據(jù)源,讀操作方法加上從庫(kù)數(shù)據(jù)源,這樣就能自動(dòng)切換。
示例代碼:
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
// 寫操作使用主庫(kù)
@DS("master")
@Override
public boolean save(User user) {
return super.save(user);
}
// 讀操作使用從庫(kù)
@DS("slave_1")
@Override
public User getById(Serializable id) {
return super.getById(id);
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot+mybatis-plus實(shí)現(xiàn)多數(shù)據(jù)源配置的詳細(xì)步驟
- Mybatis-plus配置多數(shù)據(jù)源,連接多數(shù)據(jù)庫(kù)方式
- MyBatis-Plus多數(shù)據(jù)源的示例代碼
- Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問(wèn)題
- MyBatis-Plus與Druid結(jié)合Dynamic-datasource實(shí)現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫(kù)的示例
- SpringBoot整合Mybatis-Plus+Druid實(shí)現(xiàn)多數(shù)據(jù)源配置功能
相關(guān)文章
Idea工具中創(chuàng)建 SpringBoot工程及入門詳解
這篇文章主要介紹了Idea工具中創(chuàng)建 SpringBoot工程及入門分析詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
使用java實(shí)現(xiàn)百萬(wàn)級(jí)別數(shù)據(jù)導(dǎo)出excel的三種方式
這篇文章主要介紹了使用java實(shí)現(xiàn)百萬(wàn)級(jí)別數(shù)據(jù)導(dǎo)出excel的三種方式,有些業(yè)務(wù)系統(tǒng)可能動(dòng)輒涉及到百萬(wàn)上千萬(wàn)的數(shù)據(jù),用正常的方法效率就變得很低,今天我們來(lái)看看這幾種實(shí)現(xiàn)思路2023-03-03
MybatisPlus 連表查詢、邏輯刪除功能實(shí)現(xiàn)(多租戶)
這篇文章主要介紹了MybatisPlus 連表查詢、邏輯刪除功能實(shí)現(xiàn)(多租戶),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-12-12
Java開發(fā)中的23種設(shè)計(jì)模式詳解(推薦)
本篇文章主要介紹了Java開發(fā)中的23種設(shè)計(jì)模式詳解,現(xiàn)在分享給大家,也給大家做個(gè)參考。感興趣的小伙伴們可以參考一下。 設(shè)計(jì)模式(Design Patterns)2016-11-11
Java?遠(yuǎn)程調(diào)用失敗重試的操作方法
這篇文章主要介紹了Java?遠(yuǎn)程調(diào)用失敗重試的操作方法,今天給大家介紹了一下?Spring??的?@Retryable?注解使用,并通過(guò)幾個(gè) demo 來(lái)帶大家編寫了自己重試攔截器以及回滾方法,需要的朋友可以參考下2022-09-09
Java限流方法常見(jiàn)實(shí)現(xiàn)方案(單機(jī)限流和分布式限流)
Java限流用于保護(hù)系統(tǒng)資源,分為單機(jī)(Guava/滑動(dòng)窗口)和分布式(Redis+Lua)方案,核心算法包括固定窗口、令牌桶、漏桶等,推薦使用Sentinel等成熟框架實(shí)現(xiàn)動(dòng)態(tài)流量控制,本文介紹Java限流方法常見(jiàn)實(shí)現(xiàn)方案(單機(jī)限流和分布式限流),感興趣的朋友一起看看吧2025-08-08

