行业知识
To configure two different databases in Spring Boot
@Servicepublic 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 }} index.php/tags-41973.html" >To configure two different databases in Spring Boot using HikariDataSource, you can use the following approach:
()- Define separate properties for each database in your application.properties or application.yml file.
- Configure multiple DataSource beans, each pointing to a different database.
- Optionally, create JdbcTemplate beans for easier database interaction.
Here's how you can do it:
# Database 1db1.datasource.url=jdbc:mysql://localhost:3306/db1db1.datasource.username=rootdb1.datasource.password=passworddb1.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# Database 2db2.datasource.url=jdbc:mysql://localhost:3306/db2db2.datasource.username=rootdb2.datasource.password=passworddb2.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- 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;@Configurationpublic 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(); }} - 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;@Configurationpublic 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.
- Define the entities:
-
@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} -
@Repositorypublic 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}@Repositorypublic 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}