springboot2.x jpa 多数据源的配置相比1.x有一点不同,下面提供一种2.x的jpa 多数据源的配置
1.配置类
PrimaryConfig
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.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.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Map;
/**
* @author 75412
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryPrimary",
transactionManagerRef="transactionManagerPrimary",
basePackages= { "com.gbcom.facesystem.migratingdata.dao.ics" }) //设置Repository所在位置
public class PrimaryConfig {
@Autowired
private HibernateProperties hibernateProperties;
@Autowired
private JpaProperties jpaProperties;
private Map<String, Object> getVendorProperties() {
return hibernateProperties.determineHibernateProperties(
jpaProperties.getProperties(), new HibernateSettings()
);
}
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary") // 配置数据源获取的涞源
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.properties(getVendorProperties())
.packages("com.gbcom.facesystem.migratingdata.dao.ics")
.persistenceUnit("primaryPersistenceUnit")
.build();
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager propertyTransactionManager(
@Qualifier("entityManagerFactoryPrimary") EntityManagerFactory propertyEntityManagerFactory) {
return new JpaTransactionManager(propertyEntityManagerFactory);
}
}
SecondaryConfig
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.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.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Map;
/**
* @author 75412
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
basePackages= { "com.gbcom.facesystem.migratingdata.dao.idic"}) //设置Repository所在位置
public class SecondaryConfig {
@Autowired
private HibernateProperties hibernateProperties;
@Autowired
private JpaProperties jpaProperties;
private Map<String, Object> getVendorProperties() {
Map<String, Object> map = hibernateProperties.determineHibernateProperties(
jpaProperties.getProperties(), new HibernateSettings());return map;
}
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource targetDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder, @Qualifier("secondDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.properties(getVendorProperties())
.packages("com.gbcom.facesystem.migratingdata.dao.idic")
.persistenceUnit("targetPersistenceUnit")
.build();
}
@Bean(name = "transactionManagerSecondary")
public PlatformTransactionManager propertyTransactionManager(
@Qualifier("entityManagerFactorySecondary") EntityManagerFactory propertyEntityManagerFactory) {
return new JpaTransactionManager(propertyEntityManagerFactory);
}
}
有几个地方需要说明一下
配置文件文件application.yml
spring:
application:
name: xxxxxxxxx
datasource:
primary:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://ip:port/mysqlBase?useUnicode=true&characterEncoding=utf-8&userSSL=false&serverTimezone=GMT%2B8
username: xxxxxx
password: xxxxxx
secondary:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://ip:port/mysqlBase?useUnicode=true&characterEncoding=utf-8&userSSL=false&serverTimezone=GMT%2B8
username: xxxxxx
password: xxxxxx
一定要去jdbc-url,否则可能报错
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/5328.html