启动类增加
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
pom.xml增加
<dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.0.0</version> </dependency>
application.yml
spring: datasource: dynamic: primary: db1 # 配置默认数据库 datasource: db1: # 数据源1配置 url: jdbc:mysql://ip1:port2/dbname1?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: jtiptv password: LtXtJc_iptv2018 driver-class-name: com.mysql.jdbc.Driver db2: # 数据源2配置 url: jdbc:mysql://ip2:port2/dbname2?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.jdbc.Driver durid: initial-size: 1 max-active: 20 min-idle: 1 max-wait: 60000
对应service或mapper增加注解
@DS("db1")