Spring Boot配置和使用两个数据源
我是Spring和Spring Boot的新手。如何配置和使用两个数据源?
例如,这是我对第一个数据源的需求:
application.properties
#first dbspring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver
#second db ...
应用类别
@SpringBootApplicationpublic class SampleApplication
{
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}
如何修改application.properties
以添加另一个数据源?如何将其自动布线以供其他存储库使用?
回答:
#first dbspring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver
#second db ...
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
以上是 Spring Boot配置和使用两个数据源 的全部内容, 来源链接: utcz.com/qa/423906.html