1.依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency>
2.配置
(1)primary: db1 默认数据源是db1
(2)启动类需要修改注解添加 exclude = {DruidDataSourceAutoConfigure.class}
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
(3)如果使用db2数据源的话需要在 Mapper 或者 service 上加注解@DS(“db2”)
3.测试