|
@@ -0,0 +1,80 @@
|
|
|
+package com.ywt.outpatient.taihe.rpc.config.db;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+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;
|
|
|
+
|
|
|
+import javax.persistence.EntityManagerFactory;
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by huangguoping.
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@EnableTransactionManagement
|
|
|
+@EnableJpaRepositories(
|
|
|
+ entityManagerFactoryRef = "entityManagerFactoryCenter",
|
|
|
+ transactionManagerRef = "transactionManagerCenter",
|
|
|
+ basePackages = {"com.ywt.outpatient.taihe.rpc.domain.entity.center"}
|
|
|
+)
|
|
|
+public class Ywtcenter {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(Ywtcenter.class);
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("ywtCenterDataSource")
|
|
|
+ private DataSource ywtCenterDataSource;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private JpaProperties 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.outpatient.taihe.rpc.domain.entity.center").build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * EntityManagerFactory类似于Hibernate的SessionFactory,mybatis的SqlSessionFactory
|
|
|
+ * 总之,在执行操作之前,我们总要获取一个EntityManager,这就类似于Hibernate的Session,
|
|
|
+ * mybatis的sqlSession.
|
|
|
+ */
|
|
|
+ @Bean(name = "entityManagerFactoryCenter")
|
|
|
+ @Primary
|
|
|
+ public EntityManagerFactory entityManagerFactory(EntityManagerFactoryBuilder builder) {
|
|
|
+ log.info("创建ywt—center 的EntityManagerFactory");
|
|
|
+ return this.entityManagerFactoryBean(builder).getObject();
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 配置事务管理器
|
|
|
+ */
|
|
|
+ @Bean(name = "transactionManagerCenter")
|
|
|
+ @Primary
|
|
|
+ public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
|
|
|
+ log.info("创建db1TransactionManager");
|
|
|
+ return new JpaTransactionManager(this.entityManagerFactory(builder));
|
|
|
+ }
|
|
|
+}
|