Mybatis-Plus配置多數(shù)據(jù)源
Mybatis-Plus配置多數(shù)據(jù)源
1、引入依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
2、配置多數(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=utf8&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=utf8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
3、創(chuàng)建用戶service
public interface UserService extends IService<User> {
}
@DS("master") //指定所操作的數(shù)據(jù)源
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
5、創(chuàng)建商品service
public interface ProductService extends IService<Product> {
}
@DS("slave_1")//指定所操作的數(shù)據(jù)源
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}
本文來自博客園,作者:wfxx,轉(zhuǎn)載請注明原文鏈接:http://www.rzrgm.cn/wufaxiang/p/17306708.html

浙公網(wǎng)安備 33010602011771號