繁体   English   中英

如何在 Spring 启动 jdbc 应用程序中配置 2 个数据库并将数据插入表中

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM