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

      

  • 相关阅读:
    BASH让标准输出和错误输出颜色不同
    为Linux的文件管理器创建“在此打开终端”菜单
    在Linux终端中快速生成、解码二维码
    让BASH用得更舒服:提示符颜色、时间、显示返回值、终端标题显示当前目录与正在执行的命令
    Linux关联文件扩展名和打开程序
    Linux发行版教你如何选 给入门者的选择通法
    B/S架构与C/S架构的比较
    一个PB12.5安装的问题
    介绍JavaEE平台
    类与对象小结
  • 原文地址:https://www.cnblogs.com/acme6/p/14260850.html
Copyright © 2020-2023  润新知