parent
af3a894c14
commit
0e0c0bea32
@ -1,33 +1,25 @@
|
||||
package me.zhengjie.config.jpa;
|
||||
package me.zhengjie.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||
|
||||
@Configuration
|
||||
public class DataSourceConfig {
|
||||
/**
|
||||
* 配置主数据源
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
|
||||
@Bean(name = "primaryDataSource")
|
||||
@Primary
|
||||
@Bean(name = "primaryJpaDataSource")
|
||||
@ConfigurationProperties("spring.datasource.druid")
|
||||
public DataSource dataSource() {
|
||||
return DruidDataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置数据源
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean(name = "secondJpaDataSource")
|
||||
@Bean(name = "secondDataSource")
|
||||
@ConfigurationProperties("spring.datasource.secondary")
|
||||
public DataSource secondDataSource() {
|
||||
return DruidDataSourceBuilder.create().build();
|
||||
@ -0,0 +1,83 @@
|
||||
package me.zhengjie.config.jpa;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
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.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;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = {
|
||||
"me.zhengjie.repository",
|
||||
"me.zhengjie.modules.company.repository",
|
||||
"me.zhengjie.modules.flow.repository",
|
||||
// "me.zhengjie.modules.goods.repository",
|
||||
"me.zhengjie.modules.mnt.repository",
|
||||
"me.zhengjie.modules.quartz.repository",
|
||||
"me.zhengjie.modules.security.repository",
|
||||
"me.zhengjie.modules.store.repository",
|
||||
"me.zhengjie.modules.system.repository",
|
||||
"me.zhengjie.modules.supplier.repository"
|
||||
})
|
||||
public class JPAPrimaryConfig {
|
||||
|
||||
@Autowired
|
||||
private JpaProperties jpaProperties;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("primaryDataSource")
|
||||
private DataSource primaryDataSource;
|
||||
|
||||
@Autowired
|
||||
private HibernateProperties hibernateProperties;
|
||||
|
||||
@Bean(name = "entityManagerFactoryPrimary")
|
||||
@Primary
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
|
||||
System.out.println(hibernateProperties);
|
||||
Map<String, Object> properties = hibernateProperties.determineHibernateProperties(
|
||||
jpaProperties.getProperties(),
|
||||
new HibernateSettings());
|
||||
return builder.dataSource(primaryDataSource).properties(properties)
|
||||
.packages(
|
||||
"me.zhengjie.domain",
|
||||
"me.zhengjie.modules.company.domain",
|
||||
"me.zhengjie.modules.flow.domain",
|
||||
// "me.zhengjie.modules.goods.domain",
|
||||
"me.zhengjie.modules.mnt.domain",
|
||||
"me.zhengjie.modules.quartz.domain",
|
||||
"me.zhengjie.modules.security.domain",
|
||||
"me.zhengjie.modules.store.domain",
|
||||
"me.zhengjie.modules.system.domain",
|
||||
"me.zhengjie.modules.supplier.domain")
|
||||
.persistenceUnit("primaryPersistenceUnit").build();
|
||||
}
|
||||
|
||||
@Primary
|
||||
@Bean(name = "entityManagerPrimary")
|
||||
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
|
||||
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
|
||||
}
|
||||
|
||||
@Primary
|
||||
@Bean(name = "transactionManagerPrimary")
|
||||
public PlatformTransactionManager transactionManagerPrimary(
|
||||
EntityManagerFactoryBuilder builder) {
|
||||
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
|
||||
}
|
||||
}
|
||||
@ -1,62 +0,0 @@
|
||||
package me.zhengjie.config.jpa;
|
||||
|
||||
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.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.annotation.Resource;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = {
|
||||
"me.zhengjie.repository", "me.zhengjie.modules.quartz.repository", "me.zhengjie.modules.mnt.repository",
|
||||
"me.zhengjie.modules.company.repository", "me.zhengjie.modules.flow.repository",
|
||||
"me.zhengjie.modules.goods.repository", "me.zhengjie.modules.supplier.repository" })
|
||||
public class JPAPrimaryConfig {
|
||||
|
||||
@Resource
|
||||
private JpaProperties jpaProperties;
|
||||
|
||||
@Resource
|
||||
private HibernateProperties hibernateProperties;
|
||||
|
||||
@Resource
|
||||
@Qualifier("primaryJpaDataSource")
|
||||
private DataSource primaryDataSource;
|
||||
|
||||
@Primary
|
||||
@Bean(name = "entityManagerPrimary")
|
||||
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
|
||||
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
|
||||
}
|
||||
|
||||
@Primary
|
||||
@Bean(name = "entityManagerFactoryPrimary")
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
|
||||
Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(),
|
||||
new HibernateSettings());
|
||||
return builder.dataSource(primaryDataSource).properties(properties)
|
||||
.packages("me.zhengjie.domain", "me.zhengjie.modules.quartz.domain", "me.zhengjie.modules.mnt.domain")
|
||||
.persistenceUnit("primaryPersistenceUnit").build();
|
||||
}
|
||||
|
||||
@Primary
|
||||
@Bean(name = "transactionManagerPrimary")
|
||||
public PlatformTransactionManager transactionManagerPrimary(
|
||||
EntityManagerFactoryBuilder builder) {
|
||||
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue