参考文章:基于Spring Data JPA支持多数据源
Spring Data JPA 之 多数据源配置
spring boot下基于spring data jpa配置mysql+达梦多数据源(以不同包路径方式,mysql为主数据源)
spring:
datasource:
primary:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url:
username:
password:
secondary:
driver-class-name: oracle.jdbc.OracleDriver
jdbc-url:
username:
password:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-json</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-extra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
</dependencies>
需要将不同数据源放在不同包下分别扫描
这里用的主数据源的包名:primary , 辅数据源包名:Secondary
直接照搬无需修改
package com.ruiling.zhaigua.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.util.Map;
/**
* @author xingyu.wu
* @date 2023/9/27 15:18
* @email [email protected]
*/
@Configuration
public class DataSourceConfig {
// 不少教程是把jpaProperties、hibernateProperties、getVendorProperties放到具体的JPA配置中,代码重复冗余。
// 可以把这两个属性、一个方法在这里自动注入来初始化,然后在对应的JPA配置中自动注入即可
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
// 自动注入时的对象名称
@Bean(name = "vendorProperties")
public Map<String, Object> getVendorProperties() {
return hibernateProperties
.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
}
// 主数据源,加上 @Primary。
@Primary
// 自动注入时的对象名称
@Bean(name = "primaryDataSource")
// 主数据源的配置项前缀
@ConfigurationProperties("spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
// 自动注入时的对象名称
@Bean(name = "secondaryDataSource")
// 辅助数据源的配置项前缀
@ConfigurationProperties("spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
需要修改 repository 路径 与 实体类路径
package com.ruiling.zhaigua.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
/**
* @author xingyu.wu
* @date 2023/9/27 11:26
* @email [email protected]
*/
@SuppressWarnings("ConstantConditions")
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories( // 利⽤ EnableJpaRepositories 配置哪些包下⾯的 Repositories,采⽤哪个 EntityManagerFactory 和哪个 TransactionManagement
basePackages = {"com.ruiling.zhaigua.primary.repository"},// 改这里!!master 数据源的 repository 的包路径
entityManagerFactoryRef = "entityManagerFactoryPrimary",// 改变 master 数据源的 EntityManagerFactory 的默认值,改为 masterEntityManagerFactory
transactionManagerRef = "transactionManagerPrimary" // 改变 master 数据源的 TransactionManagement 的默认值,masterTransactionManager
)
public class PrimaryConfig {
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
// 自动注入在配置类中提供的初始化方法
@Autowired
@Qualifier("vendorProperties")
private Map<String, Object> vendorProperties;
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(
EntityManagerFactoryBuilder builder) {
// final HashMap hibernateProperties = new HashMap();
// hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
// 指定模型类的包名
return builder
.dataSource(primaryDataSource)
.properties(vendorProperties)//配置方言
.packages("com.ruiling.zhaigua.primary.model")//和这里
.persistenceUnit("primaryPersistenceUnit")
.build();
}
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "transactionManagerPrimary")
PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
修改同上
package com.ruiling.zhaigua.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
/**
* @author xingyu.wu
* @date 2023/9/27 11:31
* @email [email protected]
*/
@SuppressWarnings("ConstantConditions")
@Configuration
@EnableTransactionManagement
// 指定Secondary数据源Repository的包名
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
basePackages= {"com.ruiling.zhaigua.secondary.repository"}) //改这里!!
public class SecondaryConfig {
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
// 自动注入在配置类中提供的初始化方法
@Autowired
@Qualifier("vendorProperties")
private Map<String, Object> vendorProperties;
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (
EntityManagerFactoryBuilder builder) {
// final HashMap hibernateProperties = new HashMap();
// hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
// 指定模型类的包名
return builder
.dataSource(secondaryDataSource)
.properties(vendorProperties)
.packages("com.ruiling.zhaigua.secondary.model")//和这里
.persistenceUnit("secondaryPersistenceUnit")
.build();
}
@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
解决办法:
最后发现Application启动类中加了@EnableJpaRepositories 注解后会默认扫描entityManagerFactory,注释掉解决完成。
@EnableAsync
@SpringBootApplication
//@EnableJpaRepositories //将这里注释掉
@Import(cn.hutool.extra.spring.SpringUtil.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
参考:https://blog.csdn.net/weixin_42465269/article/details/117783565
spring boot下基于spring data jpa配置mysql+达梦多数据源(以不同包路径方式,mysql为主数据源)
最后