- 导入依赖
<!--多数据源-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.4.2</version>
</dependency>
- yml配置
datasource:
type: com.alibaba.druid.pool.DruidDataSource
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master,如果读者只是单数据源只需要注释掉slave相关配置即可
datasource:
master:
url: jdbc:mysql://数据库地址?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
username: ****
password: ****
driverClassName: com.mysql.jdbc.Driver
slave:
url: jdbc:mysql://数据库地址?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
username: ****
password: ****
driverClassName: com.mysql.jdbc.Driver
- 启动类添加注解
- 业务层 添加注解
完成~