|
@@ -1,70 +1,76 @@
|
|
|
-package com.ywt.glasses.config.jpa;
|
|
|
+package com.ywt.order.common.config.db;
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
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.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.persistence.EntityManagerFactory;
|
|
|
import javax.sql.DataSource;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
- * ywt_center数据库配置
|
|
|
- * @author dyh
|
|
|
+ * Created by huangguoping.
|
|
|
*/
|
|
|
@Configuration
|
|
|
@EnableTransactionManagement
|
|
|
@EnableJpaRepositories(
|
|
|
- entityManagerFactoryRef = "entityManagerFactoryBase",
|
|
|
- transactionManagerRef = "transactionManagerBase",
|
|
|
- basePackages = {"com.ywt.mg.domain.entity.center"}
|
|
|
+ entityManagerFactoryRef = "entityManagerFactoryCenter",
|
|
|
+ transactionManagerRef = "transactionManagerCenter",
|
|
|
+ basePackages = {"com.ywt.glasses.domain.entity.center"}
|
|
|
)
|
|
|
-
|
|
|
+@Slf4j
|
|
|
public class YwtCenterConfig {
|
|
|
@Autowired
|
|
|
@Qualifier("ywtCenterDataSource")
|
|
|
private DataSource ywtCenterDataSource;
|
|
|
|
|
|
- @Bean(name = "entityManagerBase")
|
|
|
- public EntityManager entityManager(EntityManagerFactoryBuilder builder,@Qualifier("ywtCenterJpaProperties") JpaProperties jpaProperties) {
|
|
|
- return entityManagerFactory(builder,jpaProperties).getObject().createEntityManager();
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(name = "entityManagerFactoryBase")
|
|
|
- public LocalContainerEntityManagerFactoryBean entityManagerFactory (EntityManagerFactoryBuilder builder, @Qualifier("ywtCenterJpaProperties") JpaProperties jpaProperties) {
|
|
|
- return builder
|
|
|
- .dataSource(ywtCenterDataSource)
|
|
|
- .properties(jpaProperties.getProperties())
|
|
|
- .properties(new HibernateProperties()
|
|
|
- .determineHibernateProperties(
|
|
|
- jpaProperties.getProperties(), new HibernateSettings()))
|
|
|
- .packages("com.ywt.mg.domain.entity.center") //设置实体类所在位置
|
|
|
- .persistenceUnit("askcenterPersistenceUnit")
|
|
|
- .build();
|
|
|
- }
|
|
|
-
|
|
|
@Autowired
|
|
|
private JpaProperties jpaProperties;
|
|
|
|
|
|
- @Bean(name = "ywtCenterJpaProperties")
|
|
|
- @ConfigurationProperties(prefix = "spring.jpa.ywtcenter")
|
|
|
- public JpaProperties jpaProperties() {
|
|
|
- return new JpaProperties();
|
|
|
+ @Autowired
|
|
|
+ private HibernateProperties hibernateProperties;
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 通过LocalContainerEntityManagerFactoryBean来获取EntityManagerFactory实例
|
|
|
+ */
|
|
|
+ @Bean(name = "entityManagerFactoryBeanCenter")
|
|
|
+ public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
|
|
|
+ EntityManagerFactoryBuilder builder) {
|
|
|
+ Map<String, Object> properties = hibernateProperties.determineHibernateProperties(
|
|
|
+ jpaProperties.getProperties(), new HibernateSettings());
|
|
|
+ return builder.dataSource(ywtCenterDataSource).properties(properties)
|
|
|
+ .packages("com.ywt.glasses.domain.entity.center").build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * EntityManagerFactory类似于Hibernate的SessionFactory,mybatis的SqlSessionFactory
|
|
|
+ * 总之,在执行操作之前,我们总要获取一个EntityManager,这就类似于Hibernate的Session,
|
|
|
+ * mybatis的sqlSession.
|
|
|
+ */
|
|
|
+ @Bean(name = "entityManagerFactoryCenter")
|
|
|
+ public EntityManagerFactory entityManagerFactory(EntityManagerFactoryBuilder builder) {
|
|
|
+ log.info("创建ywt—center 的EntityManagerFactory");
|
|
|
+ return this.entityManagerFactoryBean(builder).getObject();
|
|
|
}
|
|
|
|
|
|
- @Bean(name = "transactionManagerBase")
|
|
|
+ /*
|
|
|
+ * 配置事务管理器
|
|
|
+ */
|
|
|
+ @Bean(name = "transactionManagerCenter")
|
|
|
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
|
|
|
- return new JpaTransactionManager(entityManagerFactory(builder, jpaProperties()).getObject());
|
|
|
+ log.info("创建Center的TransactionManager");
|
|
|
+ return new JpaTransactionManager(this.entityManagerFactory(builder));
|
|
|
}
|
|
|
}
|