如果数据库类型相同,可以配置spring.jpa相关属性,primaryDataSource或者secondaryDataSource其中之一配置@Primary,用于触发HibernateJpaConfiguration(JpaBaseConfiguration)#EntityManagerFactoryBuilder方法。这样相关spring.jpa属性自动赋值给EntityManagerFactoryBuilder中相关对象。
如果数据库类型不同,如mysql、oracle,primaryDataSource或者secondaryDataSource上@Primary注释掉,调整其他注释部分,注意相关注解加上@Qualifier。
spring.primary.datasource.jdbc-url=jdbc:mysql://***/database-name?useSSL=false
spring.primary.datasource.username=
spring.primary.datasource.password=
spring.primary.datasource.driver-class-name=
spring.secondary.datasource.jdbc-url=jdbc:mysql://***/database-name?useSSL=false
spring.secondary.datasource.username=
spring.secondary.datasource.password=
spring.secondary.datasource.driver-class-name=
spring.jpa.database-platform=
spring.jpa.properties.hibernate.hbm2ddl.auto=none
spring.jpa.open-in-view=false
#spring.primary.jpa.properties.hibernate.hbm2ddl.auto=validate
#spring.secondary.jpa.properties.hibernate.hbm2ddl.auto=none
import org.springframework.beans.factory.annotation.Qualifier;
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;
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.primary.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Primary//用于触发HibernateJpaConfiguration(JpaBaseConfiguration)#EntityManagerFactoryBuilder
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix="spring.secondary.datasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
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;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackages = {"com.***.repository"}) //设置Repository所在位置
public class PrimaryConfig {
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
// @Autowired
// @Qualifier("primaryEntityManagerFactoryBuilder")
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
// @Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean bean = builder
.dataSource(primaryDataSource)
.properties(getVendorProperties())
.packages("com.***.domain") //设置实体类所在位置
.persistenceUnit("persistenceUnitPrimary")
.build();
return bean;
}
@Autowired
// @Qualifier("primaryJpaProperties")
private JpaProperties jpaProperties;
private Map<String, Object> getVendorProperties() {
return jpaProperties.getHibernateProperties(new HibernateSettings());
}
// @Bean
// @ConfigurationProperties("spring.primary.jpa")
// public JpaProperties primaryJpaProperties() {
// return new JpaProperties();
// }
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
// @Bean(name = "transactionManagerPrimary")
// public PlatformTransactionManager transactionManagerPrimary(@Qualifier("primaryEntityManagerFactoryBuilder") EntityManagerFactoryBuilder builder) {
// return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
// }
// @Bean("primaryEntityManagerFactoryBuilder")
// public EntityManagerFactoryBuilder entityManagerFactoryBuilder(
// @Qualifier("primaryJpaVendorAdapter") JpaVendorAdapter jpaVendorAdapter,
// ObjectProvider<PersistenceUnitManager> persistenceUnitManager) {
// EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder(
// jpaVendorAdapter, this.jpaProperties.getProperties(),
// persistenceUnitManager.getIfAvailable());
builder.setCallback(getVendorCallback());
// return builder;
// }
//
// @Bean("primaryJpaVendorAdapter")
// public JpaVendorAdapter jpaVendorAdapterPrimary() {
// AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
// adapter.setShowSql(this.jpaProperties.isShowSql());
// adapter.setDatabase(this.jpaProperties.determineDatabase(this.primaryDataSource));
// adapter.setDatabasePlatform(this.jpaProperties.getDatabasePlatform());
// adapter.setGenerateDdl(this.jpaProperties.isGenerateDdl());
// return adapter;
// }
}
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.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.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactorySecondary",
transactionManagerRef = "transactionManagerSecondary",
basePackages = {"com.***.searchrepository"}) //设置Repository所在位置
public class SecondaryConfig {
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@Bean(name = "entityManagerSecondary")
// @Autowired
// @Qualifier("secondaryEntityManagerFactoryBuilder")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary( EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean bean = builder
.dataSource(secondaryDataSource)
.properties(getVendorProperties())
.packages("com.***.seconddomain") //设置实体类所在位置
.persistenceUnit("persistenceUnitSecondary")
.build();
return bean;
}
@Autowired
// @Qualifier("secondaryJpaProperties")
private JpaProperties jpaProperties;
private Map getVendorProperties() {
return jpaProperties.getHibernateProperties(new HibernateSettings());
}
// @Bean
// @ConfigurationProperties("spring.secondary.jpa")
// public JpaProperties secondaryJpaProperties() {
// return new JpaProperties();
// }
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary( EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
// @Bean(name = "transactionManagerSecondary")
// PlatformTransactionManager transactionManagerSecondary(@Qualifier("secondaryEntityManagerFactoryBuilder") EntityManagerFactoryBuilder builder) {
// return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
// }
// @Bean("secondaryEntityManagerFactoryBuilder")
// public EntityManagerFactoryBuilder entityManagerFactoryBuilder(
// JpaVendorAdapter jpaVendorAdapter,
// ObjectProvider<PersistenceUnitManager> persistenceUnitManager) {
// EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder(
// jpaVendorAdapter, this.jpaProperties.getProperties(),
// persistenceUnitManager.getIfAvailable());
builder.setCallback(getVendorCallback());
// return builder;
// }
//
// @Bean("secondaryJpaVendorAdapter")
// public JpaVendorAdapter jpaVendorAdapterSecondary() {
// AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
// adapter.setShowSql(this.jpaProperties.isShowSql());
// adapter.setDatabase(this.jpaProperties.determineDatabase(this.secondaryDataSource));
// adapter.setDatabasePlatform(this.jpaProperties.getDatabasePlatform());
// adapter.setGenerateDdl(this.jpaProperties.isGenerateDdl());
// return adapter;
// }
}