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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[MyBatisPlus]模拟多数据源环境及测试

發(fā)布時(shí)間:2023/12/4 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [MyBatisPlus]模拟多数据源环境及测试 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

模擬多數(shù)據(jù)源環(huán)境

多數(shù)據(jù)源

適用于多種場景:純粹多庫、 讀寫分離、 一主多從、 混合模式等

目前我們就來模擬一個純粹多庫的一個場景,其他場景類似

場景說明:

我們創(chuàng)建兩個庫,分別為:mybatis_plus(以前的庫不動)與mybatis_plus_1(新建),將mybatis_plus庫的product表移動到mybatis_plus_1庫,這樣每個庫一張表,通過一個測試用例分別獲取用戶數(shù)據(jù)與商品數(shù)據(jù),如果獲取到說明多庫模擬成功

創(chuàng)建數(shù)據(jù)庫及表

CREATE DATABASE `mybatis_plus_1` /*!40100 DEFAULT CHARACTER SET utf8mb4 */; use `mybatis_plus_1`; 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 '樂觀鎖版本號', PRIMARY KEY (id) );

添加測試數(shù)據(jù)

INSERT INTO product (id, NAME, price) VALUES (1, '外星人筆記本', 100);

刪除mybatis_plus庫product表

use mybatis_plus; DROP TABLE IF EXISTS product;


引入依賴

<!-- mybatis plus 啟動器--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><!-- lombok用于簡化實(shí)體類開發(fā)--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!-- mysql驅(qū)動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.5.0</version></dependency>

配置多數(shù)據(jù)源

spring:# 配置數(shù)據(jù)源信息datasource:dynamic:# 設(shè)置默認(rèn)的數(shù)據(jù)源或者數(shù)據(jù)源組,默認(rèn)值為masterprimary: master #設(shè)置默認(rèn)的數(shù)據(jù)源或者數(shù)據(jù)源組,默認(rèn)值即為master,如果讀者只是單數(shù)據(jù)源只需要注釋掉slave相關(guān)配置即可,這里為了方便演示master與slave保持相同# 嚴(yán)格匹配數(shù)據(jù)源,默認(rèn)為false,true未匹配到指定數(shù)據(jù)源時(shí)拋出異常,false使用默認(rèn)數(shù)據(jù)源strict: falsedatasource:master:url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=Hongkong&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false # serverTimezone=Hongkong 需要填上時(shí)區(qū)username: rootpassword: passworddriverClassName: com.mysql.cj.jdbc.Driverslave_1:url: jdbc:mysql://localhost:3306/mybatis_plus_1?serverTimezone=Hongkong&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false # serverTimezone=Hongkong 需要填上時(shí)區(qū)username: rootpassword: passworddriverClassName: com.mysql.cj.jdbc.Driver

創(chuàng)建用戶service

package com.xxxx.mybatisplus02.service;import com.baomidou.mybatisplus.extension.service.IService; import com.xxxx.mybatisplus02.pojo.User;public interface UserService extends IService<User> {} package com.xxxx.mybatisplus02.service.Impl;import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.xxxx.mybatisplus02.mapper.UserMapper; import com.xxxx.mybatisplus02.pojo.User; import com.xxxx.mybatisplus02.service.UserService; import org.springframework.stereotype.Service;@DS("master") @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}

創(chuàng)建商品service

package com.xxxx.mybatisplus02.service;import com.baomidou.mybatisplus.extension.service.IService; import com.xxxx.mybatisplus02.pojo.Product;public interface ProductService extends IService<Product> {} package com.xxxx.mybatisplus02.service.Impl;import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.xxxx.mybatisplus02.mapper.ProductMapper; import com.xxxx.mybatisplus02.pojo.Product; import com.xxxx.mybatisplus02.service.ProductService; import org.springframework.stereotype.Service;@DS("slave_1") @Service public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService { }

測試

package com.xxxx.mybatisplus02;import com.xxxx.mybatisplus02.service.ProductService; import com.xxxx.mybatisplus02.service.UserService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class Mybatisplus02ApplicationTests {@Autowiredprivate UserService userService;@Autowiredprivate ProductService productService;@Testpublic void test(){System.out.println(userService.getById(1));System.out.println(productService.getById(1));}}

結(jié)果:

  • 都能順利獲取對象,則測試成功
  • 如果我們實(shí)現(xiàn)讀寫分離,將寫操作方法加上主庫數(shù)據(jù)源,讀操作方法加上從庫數(shù)據(jù)源,自動切換,是不是就能實(shí)現(xiàn)讀寫分離?
  • @DS 可以注解在方法上或類上,同時(shí)存在就近原則 方法上注解 優(yōu)先于 類上注解。

    總結(jié)

    以上是生活随笔為你收集整理的[MyBatisPlus]模拟多数据源环境及测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。