To configure two different databases in Spring Boot

@Service
public class MyService {
    private final Entity1Repository entity1Repository;
    private final Entity2Repository entity2Repository;

    @Autowired
    public MyService(Entity1Repository entity1Repository, Entity2Repository entity2Repository) {
        this.entity1Repository = entity1Repository;
        this.entity2Repository = entity2Repository;
    }

    public void doSomething() {
        // Use entity1Repository and entity2Repository to interact with databases
    }
}

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:

# Database 1
db1.datasource.url=jdbc:mysql://localhost:3306/db1
db1.datasource.username=root
db1.datasource.password=password
db1.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# Database 2
db2.datasource.url=jdbc:mysql://localhost:3306/db2
db2.datasource.username=root
db2.datasource.password=password
db2.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  1. DataSource Configuration:
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(name = "db1DataSource")
    @ConfigurationProperties(prefix = "db1.datasource")
    public DataSource db1DataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "db2DataSource")
    @ConfigurationProperties(prefix = "db2.datasource")
    public DataSource db2DataSource() {
        return new HikariDataSource();
    }
}
  1. JdbcTemplate Beans (Optional):
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;

@Configuration
public class JdbcTemplateConfig {

    @Bean(name = "db1JdbcTemplate")
    public JdbcTemplate db1JdbcTemplate(@Qualifier("db1DataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "db2JdbcTemplate")
    public JdbcTemplate db2JdbcTemplate(@Qualifier("db2DataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

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:
  2. @Entity
    @Table(name = "entity1")
    public class Entity1 {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        // Other fields, getters, and setters
    }
    
    @Entity
    @Table(name = "entity2")
    public class Entity2 {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        // Other fields, getters, and setters
    }
    

  3. @Repository
    public class Entity1Repository {
        private final JdbcTemplate jdbcTemplate;
    
        @Autowired
        public Entity1Repository(@Qualifier("db1JdbcTemplate") JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        // Define methods to interact with Entity1 table in db1
    }
    
    @Repository
    public class Entity2Repository {
        private final JdbcTemplate jdbcTemplate;
    
        @Autowired
        public Entity2Repository(@Qualifier("db2JdbcTemplate") JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        // Define methods to interact with Entity2 table in db2
    }
    

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 09:42:10       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 09:42:10       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 09:42:10       87 阅读
  4. Python语言-面向对象

    2024-03-18 09:42:10       96 阅读

热门阅读

  1. odoo中传递上下文

    2024-03-18 09:42:10       45 阅读
  2. React高阶组件详解

    2024-03-18 09:42:10       47 阅读
  3. Flutter 当涉及Listview的复杂滑动布局良好布局方式

    2024-03-18 09:42:10       38 阅读
  4. Python实现连连看

    2024-03-18 09:42:10       42 阅读
  5. 如何优化查询ORM

    2024-03-18 09:42:10       43 阅读
  6. IDEA SpringBoot + Gradle无法运行测试问题

    2024-03-18 09:42:10       40 阅读
  7. Spring Data访问Elasticsearch----Elasticsearch对象映射

    2024-03-18 09:42:10       46 阅读
  8. Spring Boot(七十):利用Jasypt对数据库连接进行加密

    2024-03-18 09:42:10       37 阅读
  9. 如何在MATLAB中处理图像和视频?

    2024-03-18 09:42:10       41 阅读
  10. tcpudp面试题

    2024-03-18 09:42:10       36 阅读