SpringBoot JPA 配置多个数据库
Posted 长不大的大灰狼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot JPA 配置多个数据库相关的知识,希望对你有一定的参考价值。
SpringBoot JPA 配置多个数据库
一、application.yml :
primary为主数据源配置,secondary为第二数据源配置
spring:
application:
name: test
jpa:
hibernate:
ddl-auto: update
show-sql: true
datasource:
primary:
jdbc-url: jdbc:mysql://127.0.0.1:3306/laboratory
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
secondary:
jdbc-url: jdbc:mysql://127.0.0.1:3306/test
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
二、创建一个SpringBoot配置类
定义两个DataSource来读取application.yml中的不同配置
@Configuration
public class DataSourceConfig
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource masterDataSource()
return DataSourceBuilder.create().build();
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource()
return DataSourceBuilder.create().build();
三、primary数据源的JPA配置
指定数据源对应的Entity实体和Repository定义的位置,用@Primary来区分主数据源。
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackages = "cn.ntshare.laboratory.domain.master"
)
public class PrimarySourceConfig
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder)
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
@Resource
private Properties jpaProperties;
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder)
LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
.dataSource(primaryDataSource)
.packages("cn.ntshare.laboratory.domain.master") //设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit")
.build();
entityManagerFactory.setJpaProperties(jpaProperties);
return entityManagerFactory;
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder)
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
四、secondary数据源的JPA配置
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
basePackages= "cn.ntshare.laboratory.domain.slave" ) //设置Repository所在位置
public class SecondarySourceConfig
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder)
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
@Resource
private Properties jpaProperties;
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder)
LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
.dataSource(secondaryDataSource)
.packages("cn.ntshare.laboratory.domain.slave") //设置实体类所在位置
.persistenceUnit("secondaryPersistenceUnit")
.build();
entityManagerFactory.setJpaProperties(jpaProperties);
return entityManagerFactory;
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder)
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
主数据源的实体和数据访问对象位于:cn.ntshare.laboratory.domain.master
次数据源的实体和数据访问对象位于:cn.ntshare.laboratory.domain.slave
参考文章:
[1] SpringBoot 连接多个数据库
[2] SpringBoot配置JPA多数据源
以上是关于SpringBoot JPA 配置多个数据库的主要内容,如果未能解决你的问题,请参考以下文章
使用 Spring Boot 自动装配具有 jpa 和非 jpa 特征的多个数据源
具有自动配置的 Spring Data JPA 应用程序的多个数据库
Spring Data Jpa多个@EnableJpaRepositories,BootstrapMode.DEFERRED不能正常工作