当前位置:   article > 正文

To configure two different databases in Spring Boot

To configure two different databases in Spring Boot
  1. @Service
  2. public class MyService {
  3. private final Entity1Repository entity1Repository;
  4. private final Entity2Repository entity2Repository;
  5. @Autowired
  6. public MyService(Entity1Repository entity1Repository, Entity2Repository entity2Repository) {
  7. this.entity1Repository = entity1Repository;
  8. this.entity2Repository = entity2Repository;
  9. }
  10. public void doSomething() {
  11. // Use entity1Repository and entity2Repository to interact with databases
  12. }
  13. }

To configure two different databases in Spring Boot using HikariDataSource, you can use the following approach:

  1. Define separate properties for each database in your application.properties or application.yml file.
  2. Configure multiple DataSource beans, each pointing to a different database.
  3. Optionally, create JdbcTemplate beans for easier database interaction.

Here's how you can do it:

  1. # Database 1
  2. db1.datasource.url=jdbc:mysql://localhost:3306/db1
  3. db1.datasource.username=root
  4. db1.datasource.password=password
  5. db1.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  6. # Database 2
  7. db2.datasource.url=jdbc:mysql://localhost:3306/db2
  8. db2.datasource.username=root
  9. db2.datasource.password=password
  10. db2.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  1. DataSource Configuration:
  1. import com.zaxxer.hikari.HikariDataSource;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Primary;
  6. import javax.sql.DataSource;
  7. @Configuration
  8. public class DataSourceConfig {
  9. @Primary
  10. @Bean(name = "db1DataSource")
  11. @ConfigurationProperties(prefix = "db1.datasource")
  12. public DataSource db1DataSource() {
  13. return new HikariDataSource();
  14. }
  15. @Bean(name = "db2DataSource")
  16. @ConfigurationProperties(prefix = "db2.datasource")
  17. public DataSource db2DataSource() {
  18. return new HikariDataSource();
  19. }
  20. }
  1. JdbcTemplate Beans (Optional):
  1. import org.springframework.beans.factory.annotation.Qualifier;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.jdbc.core.JdbcTemplate;
  5. import javax.sql.DataSource;
  6. @Configuration
  7. public class JdbcTemplateConfig {
  8. @Bean(name = "db1JdbcTemplate")
  9. public JdbcTemplate db1JdbcTemplate(@Qualifier("db1DataSource") DataSource dataSource) {
  10. return new JdbcTemplate(dataSource);
  11. }
  12. @Bean(name = "db2JdbcTemplate")
  13. public JdbcTemplate db2JdbcTemplate(@Qualifier("db2DataSource") DataSource dataSource) {
  14. return new JdbcTemplate(dataSource);
  15. }
  16. }

With this setup, you'll have two separate DataSources (db1DataSource and db2DataSource) configured with HikariDataSource. Optionally, you can create JdbcTemplate beans (db1JdbcTemplate and db2JdbcTemplate) to simplify database interactions with each database.

To use db1JdbcTemplate and db2JdbcTemplate in a Spring Boot application, you would typically inject them into your services or repositories where you need to interact with the databases. Here's an example of how you can use them:

Let's assume you have two entities Entity1 and Entity2 corresponding to tables in db1 and db2 databases respectively.

  1. Define the entities:
    1. @Entity
    2. @Table(name = "entity1")
    3. public class Entity1 {
    4. @Id
    5. @GeneratedValue(strategy = GenerationType.IDENTITY)
    6. private Long id;
    7. // Other fields, getters, and setters
    8. }
    9. @Entity
    10. @Table(name = "entity2")
    11. public class Entity2 {
    12. @Id
    13. @GeneratedValue(strategy = GenerationType.IDENTITY)
    14. private Long id;
    15. // Other fields, getters, and setters
    16. }

    1. @Repository
    2. public class Entity1Repository {
    3. private final JdbcTemplate jdbcTemplate;
    4. @Autowired
    5. public Entity1Repository(@Qualifier("db1JdbcTemplate") JdbcTemplate jdbcTemplate) {
    6. this.jdbcTemplate = jdbcTemplate;
    7. }
    8. // Define methods to interact with Entity1 table in db1
    9. }
    10. @Repository
    11. public class Entity2Repository {
    12. private final JdbcTemplate jdbcTemplate;
    13. @Autowired
    14. public Entity2Repository(@Qualifier("db2JdbcTemplate") JdbcTemplate jdbcTemplate) {
    15. this.jdbcTemplate = jdbcTemplate;
    16. }
    17. // Define methods to interact with Entity2 table in db2
    18. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/262906
推荐阅读
相关标签
  

闽ICP备14008679号