[英]How to configure 2 databases in Spring boot jdbc application and insert data into table
I am new to spring boot and trying to configure and access two databases using spring jdbc, can someone help me here I have one local database (mysql) and another on aws rds (mysql), I have configured application.yml with two datasources. 但是每當我運行應用程序時,它只連接到 applicaion.yml 中配置的最后一個數據庫,我想從一個數據庫連接到兩個數據庫,我需要獲取信息和處理並將日志放入 aws rds,有人可以幫我嗎?
This is my Dbconfig file
@Bean(name = "rdsDatasource")
@ConfigurationProperties("spring.datasource.rds")
public DataSource rdsDatasource() {
return DataSourceBuilder.create().build();
}
@Bean
public NamedParameterJdbcOperations namedParameterJdbcOperations(@Qualifier("rdsDatasource") DataSource rdsDbDataSource) {
return new NamedParameterJdbcTemplate(rdsDbDataSource);
}
application.yml
datasource:
local:
url: jdbc:mysql://localhost:3306/test
username: test
password: test
port: 3306
rds:
url: jdbc:mysql://aws-rds/test
username: test
password: test
port: 3306.
謝謝,如果需要更多信息,請告訴我。
您已將 DataSource 配置為使用 rds 數據庫的屬性 - 這就是@ConfigurationProperties("spring.datasource.rds")
所做的。
每個DataSource object 只能連接一個數據庫。 您應該使用@ConfigurationProperties("spring.datasource.local")
創建另一個 DataSource object 並將其命名為@Bean("localDatasource")
之類的其他名稱 - 然后您將擁有兩個不同的 DataSource 對象,它們連接到您的兩個數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.