Spring Boot 整合多数据源配置实现
在 Spring Boot 中整合多数据源可以通过配置多个 DataSource
bean 来实现。下面是一个基本的实现步骤:
步骤 1:添加依赖
首先,你需要在 pom.xml
文件中添加 Spring Boot 和数据库相关的依赖。例如,如果你使用的是 MySQL 和 H2 数据库,你可以添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
步骤 2:配置数据源
在 application.properties
或 application.yml
中配置不同的数据源。例如:
# Primary DataSource (e.g., MySQL)
spring.datasource.primary.url=jdbc:mysql://localhost:3306/primarydb
spring.datasource.primary.username=root
spring.datasource.primary.password=yourpassword
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
# Secondary DataSource (e.g., H2)
spring.datasource.secondary.url=jdbc:h2:mem:secondarydb
spring.datasource.secondary.driver-class-name=org.h2.Driver
spring.datasource.secondary.username=sa
spring.datasource.secondary.password=password
步骤 3:定义配置类
为每个数据源定义一个配置类,并标记相应的配置。使用 @Primary
注解来表示哪个数据源是主要的。
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = "com.example.primary.repository",
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager"
)
public class PrimaryDataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("primaryDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.primary.model")
.persistenceUnit("primary")
.build();
}
@Primary
@Bean(name = "primaryTransactionManager")
public PlatformTransactionManager primaryTransactionManager(
@Qualifier("primaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
为第二个数据源做类似配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = "com.example.secondary.repository",
entityManagerFactoryRef = "secondaryEntityManagerFactory",
transactionManagerRef = "secondaryTransactionManager"
)
public class SecondaryDataSourceConfig {
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("secondaryDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.secondary.model")
.persistenceUnit("secondary")
.build();
}
@Bean(name = "secondaryTransactionManager")
public PlatformTransactionManager secondaryTransactionManager(
@Qualifier("secondaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
步骤 4:创建实体和仓库
在 com.example.primary.model
和 com.example.secondary.model
包中为每个数据源创建实体类。在 com.example.primary.repository
和 com.example.secondary.repository
包中为每个数据源创建 JPA 仓库接口。
结论
配置完成后,各个数据源将会在自己的独立上下文中运行,并且可以通过注入相应的 Repository 来进行数据操作。通过这种方式,你可以在 Spring Boot 应用程序中轻松管理多个数据源。