zoukankan      html  css  js  c++  java
  • springmvc双数据源

    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;
    	}
    
    }
    

      

  • 相关阅读:
    PAT (Advanced Level) Practice 1100 Mars Numbers (20分)
    PAT (Advanced Level) Practice 1107 Social Clusters (30分) (并查集)
    PAT (Advanced Level) Practice 1105 Spiral Matrix (25分)
    PAT (Advanced Level) Practice 1104 Sum of Number Segments (20分)
    PAT (Advanced Level) Practice 1111 Online Map (30分) (两次迪杰斯特拉混合)
    PAT (Advanced Level) Practice 1110 Complete Binary Tree (25分) (完全二叉树的判断+分享致命婴幼儿错误)
    PAT (Advanced Level) Practice 1109 Group Photo (25分)
    PAT (Advanced Level) Practice 1108 Finding Average (20分)
    P6225 [eJOI2019]异或橙子 树状数组 异或 位运算
    P4124 [CQOI2016]手机号码 数位DP
  • 原文地址:https://www.cnblogs.com/acme6/p/14260850.html
Copyright © 2011-2022 走看看