Mybatis-Plus的多數(shù)據(jù)源你了解嗎
多數(shù)據(jù)源
創(chuàng)建數(shù)據(jù)庫(kù)
CREATE DATABASE mybatis_plus_1;
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 '樂觀鎖版本號(hào)',
PRIMARY KEY (id)
);
刪除表
USE mybatis_plus; DROP TABLE IF EXISTS product;
插入數(shù)據(jù)
INSERT INTO product (id, NAME, price) VALUES (1, '外星人', 100);
新建個(gè)工程。步驟和之前的一樣
導(dǎo)入依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
修改application.yml
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?useSSL=false&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: "011012"
save_1:
url: jdbc:mysql://localhost:3306/mybatis_plus_1?useSSL=false&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: "011012"
創(chuàng)建文件包pojo
package com.example.pojo;
import lombok.Data;
@Data
public class Product {
private Integer id;
private String name;
private Integer price;
private Integer version;
}
package com.example.pojo;
import com.baomidou.mybatisplus.annotation.*;
import lombok.*;
//所有無(wú)參構(gòu)造和get方法和set方法還有哈希庫(kù)方法注解(lombok)
//有參構(gòu)造方法注解(@AllArgsConstructor)
@Data
@TableName("t_user")
public class User {
//將該屬性對(duì)應(yīng)的字段指定為主鍵
@TableId(value = "uid",type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
private Integer sex;
}
創(chuàng)建文件包mapper(都是接口)
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.Product;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductMapper extends BaseMapper<Product> {
}
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
創(chuàng)建文件包Service
package com.example.Sevice;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.pojo.Product;
public interface ProductService extends IService<Product> {
}
package com.example.Sevice;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.pojo.User;
public interface UserService extends IService<User> {
}
在這個(gè)包下創(chuàng)建文件包impl
package com.example.Sevice.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import org.springframework.stereotype.Service;
@Service
@DS("master")
public class UserService extends ServiceImpl<UserMapper, User> implements com.example.Sevice.UserService {
}
package com.example.Sevice.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.Sevice.ProductService;
import com.example.mapper.ProductMapper;
import com.example.pojo.Product;
import lombok.Data;
import org.springframework.stereotype.Service;
@Service
@DS("save_1")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}
寫測(cè)試文件
@Autowired
private UserService userService;
@Autowired
private ProductService productService;
@Test
public void test1(){
System.out.println(userService.getById(1));
System.out.println(productService.getById(1));
}

總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯(cuò)問題
- Mybatis-plus配置多數(shù)據(jù)源,連接多數(shù)據(jù)庫(kù)方式
- MyBatis-Plus多數(shù)據(jù)源的示例代碼
- SpringBoot集成Mybatis實(shí)現(xiàn)對(duì)多數(shù)據(jù)源訪問原理
- Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問題
- 詳解SpringBoot Mybatis如何對(duì)接多數(shù)據(jù)源
- Mybatis操作多數(shù)據(jù)源的實(shí)現(xiàn)
- 一文搞懂MyBatis多數(shù)據(jù)源Starter實(shí)現(xiàn)
- Mybatis-plus多數(shù)據(jù)源配置的兩種方式總結(jié)
- MyBatis-Plus 集成動(dòng)態(tài)多數(shù)據(jù)源的實(shí)現(xiàn)示例
- mybatis-flex實(shí)現(xiàn)多數(shù)據(jù)源操作
相關(guān)文章
SpringCloud服務(wù)接口調(diào)用OpenFeign及使用詳解
這篇文章主要介紹了SpringCloud服務(wù)接口調(diào)用——OpenFeign,在學(xué)習(xí)Ribbon時(shí),服務(wù)間調(diào)用使用的是RestTemplate+Ribbon實(shí)現(xiàn),而Feign在此基礎(chǔ)上繼續(xù)進(jìn)行了封裝,使服務(wù)間調(diào)用變得更加方便,需要的朋友可以參考下2023-04-04
java利用phantomjs進(jìn)行截圖實(shí)例教程
PlantomJs是一個(gè)基于javascript的webkit內(nèi)核無(wú)頭瀏覽器 也就是沒有顯示界面的瀏覽器,你可以在基于 webkit 瀏覽器做的事情,它都能做到。下面這篇文章主要給大家介紹了關(guān)于java利用phantomjs進(jìn)行截圖的相關(guān)資料,需要的朋友可以參考下2018-10-10
java實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Zuul基礎(chǔ)
這篇文章主要介紹了SpringCloud?Zuul微服務(wù)網(wǎng)關(guān),負(fù)載均衡,熔斷和限流,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
解析springboot集成AOP實(shí)現(xiàn)日志輸出的方法
如果這需要在每一個(gè)controller層去寫的話代碼過于重復(fù),于是就使用AOP定義切面 對(duì)其接口調(diào)用前后進(jìn)行攔截日志輸出。接下來通過本文給大家介紹springboot集成AOP實(shí)現(xiàn)日志輸出,需要的朋友可以參考下2021-11-11
解決springSecurity 使用默認(rèn)登陸界面登錄后無(wú)法跳轉(zhuǎn)問題
這篇文章主要介紹了解決springSecurity 使用默認(rèn)登陸界面登錄后無(wú)法跳轉(zhuǎn)問題,項(xiàng)目環(huán)境springboot下使用springSecurity 版本2.7.8,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-12-12
Java org.w3c.dom.Document 類方法引用報(bào)錯(cuò)
這篇文章主要介紹了Java org.w3c.dom.Document 類方法引用報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

