配置文件:application.properties中进行相关配置,我们先配两个不同的数据源
#常用数据库配置
spring.datasource.primary.url=jdbc:mysql://localhost:3306/test01
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
#非常用数据库的配置
spring.datasource.second.url=jdbc:mysql://localhost:3306/test02
spring.datasource.second.username=root
spring.datasource.second.password=root
spring.datasource.second.driver-class-name=com.mysql.jdbc.Driver
JdbcDataSource 的配置
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.<

本文介绍了如何在SpringBoot应用中使用`application.properties`配置文件管理两个不同数据源(primary和secondary),包括DataSource和JdbcTemplate的配置,以及在Controller中注入和使用这些数据源的方法。
最低0.47元/天 解锁文章
7755

被折叠的 条评论
为什么被折叠?



