方式一:直接用MybatisPlus封装好的注解

1.首先加入依赖:

<!-- 动态数据源 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>

2.再加入数据库配置:格式如下:

spring:
application:
name: lx-user-server
datasource:
dynamic:
primary: master
strict: false
datasource:
master:
url: jdbc:mysql://localhost:3306/test_1?characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: 123456
type: com.mysql.cj.jdbc.MysqlDataSource
slave_1:
url: jdbc:mysql://localhost:3306/lx_canal_test?useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: 123456
type: com.mysql.cj.jdbc.MysqlDataSource
slave_2:
url: jdbc:mysql://localhost:3306/test_2?useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: 123456
type: com.mysql.cj.jdbc.MysqlDataSource

springboot设置动态数据源_mysql

 

springboot设置动态数据源_mysql_02

 

springboot设置动态数据源_spring_03