springBoot 多数据源配置

常规数据源

# 应用端口
server:
  port: 5555
spring:
  datasource:
    username: root
    password: ffjy1101
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
  application:

 

多数据源:
1.修改yml

# 应用端口
server:
  port: 5555
spring:
  datasource:
    dynamic:
      primary: test1 #默认使用test1数据源
      strict: false
      datasource:
        test1:
          username: root
          password: ffjy1101
          url: jdbc:mysql://localhost:3306/test1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
          driver-class-name: com.mysql.cj.jdbc.Driver
        test2:
          username: root
          password: ffjy1101
          url: jdbc:mysql://localhost:3306/test2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
          driver-class-name: com.mysql.cj.jdbc.Driver


2.添加pom.xml

<dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
           <version>3.5.0</version>
        </dependency> 

 

3.配置

//默认使用test1数据源,使用其他数据源需在类或方法上添加注解
@DS("test2")

 

posted @ 2024-04-18 23:36  好mingzi给猪了  阅读(20)  评论(0编辑  收藏  举报