(1)首先要把Spring Boot自带的DataSourceAutoConfiguration禁用,因为它会读取application.properties文件中的Spring.datasource.*属性并自动配置单数据源。
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
(2)在application.properties中添加数据源连接,生成环境中请使用连接池。
## test1 database
spring.datasource.test1.url=jdbc:mysql://localhost:3307/multipledatasource1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
spring.datasource.test1.username=root
spring.datasource.test1.password=root
spring.datasource.test1.driver-class-name=com.mysql.cj.jdbc.Driver
## test2 database
spring.datasource.test2.url=jdbc:mysql://localhost:3307/multipledatasource2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
spring.datasource.test2.username=root
spring.datasource.test2.password=root
spring.datasource.test2.driver-class-
spring多数据源
最新推荐文章于 2024-04-25 09:31:40 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)