mybatis-plus多数据源使用
前言
本文记录多数据源的使用。
一、多数据源使用
1.导入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
2.配置yml
#多数据源配置
datasource:
#主数据库
master:
url: jdbc:mysql://127.0.0.1:56001/20220930yhjg?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
yhjg_seeyon:
url: jdbc:mysql://127.0.0.1:3306/yhjg_seeyon?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
xxl_job:
url: jdbc:mysql://127.0.0.1:3306/xxl_job?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
3.使用 @DS 切换数据源
**@DS 可以注解在方法上或类上,同时存在就近原则 方法上注解 优先于 类上注解**