package com.zxelec.common.config; import java.sql.SQLException; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Value; 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.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.alibaba.druid.pool.DruidDataSource; @Configuration @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactory", transactionManagerRef="transactionManager", basePackages= { "com.zxelec.cpbs.jpa" }) @EnableTransactionManagement public class DateSourceConfig { private Logger logger = LogManager.getLogger(DateSourceConfig.class); @Value("${pg.database.driver}") private String driverClassName; @Value("${pg.database.url}") private String url; @Value("${pg.database.username}") private String username; @Value("${pg.database.password}") private String password; @Value("${pg.initialSize}") private Integer initialSize; @Value("${pg.maxActive}") private Integer maxActive; @Value("${pg.minIdle}") private Integer minIdle; @Value("${pg.maxWait}") private Integer maxWait; @Value("${pg.showSql:false}") private Boolean pgShowSql; @Value("${pg.persistence.unit.name}") private String pgPersistenceUnitName; @Bean(destroyMethod = "close", initMethod = "init") @Primary public DataSource druidDataSource() { logger.info("===================卡口业务 DataSource==================="); DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setInitialSize(initialSize); dataSource.setMaxActive(maxActive); dataSource.setMinIdle(minIdle); dataSource.setMaxWait(maxWait); try { dataSource.setFilters("stat"); dataSource.setValidationQuery("select X"); } catch (SQLException e) { logger.error("初始化数据库连接池异常e.getMessage:{},e:{}",e.getMessage(),e); } return dataSource; } @Bean @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactory() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setPackagesToScan("com.zxelec.cpbs.entity"); factory.setDataSource(druidDataSource()); factory.setPersistenceUnitName(pgPersistenceUnitName); jpaVendorAdapter.setShowSql(pgShowSql); factory.setJpaVendorAdapter(jpaVendorAdapter); return factory; } @Bean @Primary public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory); return txManager; } }
package com.zxelec.common.config; import java.sql.SQLException; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Value; 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.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import com.alibaba.druid.pool.DruidDataSource; @Configuration @EnableJpaRepositories( entityManagerFactoryRef="structrueEntityManagerFactory", transactionManagerRef="structrueTransactionManager", basePackages= { "com.zxelec.cpbs.structruejpa" }) public class StructrueDateSourceConfig { private Logger logger = LogManager.getLogger(StructrueDateSourceConfig.class); @Value("${structrue.pg.database.driver}") private String driverClassName; @Value("${structrue.pg.database.url}") private String url; @Value("${structrue.pg.database.username}") private String username; @Value("${structrue.pg.database.password}") private String password; @Value("${structrue.pg.initialSize}") private Integer initialSize; @Value("${structrue.pg.maxActive}") private Integer maxActive; @Value("${structrue.pg.minIdle}") private Integer minIdle; @Value("${structrue.pg.maxWait}") private Integer maxWait; @Value("${structrue.pg.showSql:false}") private Boolean pgShowSql; @Value("${structrue.pg.persistence.unit.name}") private String pgPersistenceUnitName; @Bean(name="structrueDruidDataSource",destroyMethod = "close", initMethod = "init") public DataSource structrueDruidDataSource() { logger.info("===================卡口业务结构化数据源 DataSource==================="); DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setInitialSize(initialSize); dataSource.setMaxActive(maxActive); dataSource.setMinIdle(minIdle); dataSource.setMaxWait(maxWait); try { dataSource.setFilters("stat"); dataSource.setValidationQuery("select X"); } catch (SQLException e) { logger.error("初始化数据库连接池异常e.getMessage:{},e:{}",e.getMessage(),e); } return dataSource; } @Bean(name = "structrueEntityManagerFactory") public LocalContainerEntityManagerFactoryBean structrueEntityManagerFactory() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setPackagesToScan("com.zxelec.cpbs.structrueentity"); factory.setDataSource(structrueDruidDataSource()); factory.setPersistenceUnitName(pgPersistenceUnitName); jpaVendorAdapter.setShowSql(pgShowSql); factory.setJpaVendorAdapter(jpaVendorAdapter); return factory; } @Bean(name = "structrueTransactionManager") public PlatformTransactionManager structrueTransactionManager(EntityManagerFactory structrueEntityManagerFactory) { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(structrueEntityManagerFactory); return txManager; } }