如何使用多数据源,同时使用jpa和jdbctemplate

spring:
 datasource:
    first:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://xx.xx.xx.xx:xx/xx?characterEncoding=utf8
    driver-class-name: com.mysql.jdbc.Driver
    username: xx
    password: xx
    #配置监控统计拦截的filters,去掉监控界面sql将无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    #最大活跃数
    maxActive: 20
    #初始化连接数
    initialSize: 1
    #最大连接等待超过时间
    maxWait: 60000
    #打开PSCache,并且指定每个连接PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    #通过connectionProperties属性来打开mergeSql功能;慢SQL记录
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 1 from dual
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
  sec:
     type: com.alibaba.druid.pool.DruidDataSource
     url: jdbc:mysql://xx.xx.xx.xx:3306/xx?characterEncoding=utf8
     driver-class-name: com.mysql.jdbc.Driver
     username: xx
     password: xx
     #配置监控统计拦截的filters,去掉监控界面sql将无法统计,'wall'用于防火墙
     filters: stat,wall,log4j
     #最大活跃数
     maxActive: 20
     #初始化连接数
     initialSize: 1
     #最大连接等待超过时间
     maxWait: 60000
     #打开PSCache,并且指定每个连接PSCache的大小
     poolPreparedStatements: true
     maxPoolPreparedStatementPerConnectionSize: 20
     #通过connectionProperties属性来打开mergeSql功能;慢SQL记录
     connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
     minIdle: 1
     timeBetweenEvictionRunsMillis: 60000
     minEvictableIdleTimeMillis: 300000
     validationQuery: select 1 from dual
     testWhileIdle: true
     testOnBorrow: false
     testOnReturn: false
配置文件

 

@ServletComponentScan
@Configuration
public class DatasourceConfiguration {

    private Logger logger = LoggerFactory.getLogger(DatasourceConfiguration.class);

    @Value("${spring.datasource.first.url}")
    private String firstdevUrl;
    @Value("${spring.datasource.first.username}")
    private String firstUsername;
    @Value("${spring.datasource.first.password}")
    private String firstPassword;

    @Value("${spring.datasource.sec.url}")
    private String secUrl;
    @Value("${spring.datasource.sec.username}")
    private String secUsername;
    @Value("${spring.datasource.sec.password}")
    private String secPassword;



    @Value("com.mysql.jdbc.Driver")
    private String driverClassName;

    @Value("5")
    private int initialSize;

    @Value("5")
    private int minIdle;

    @Value("20")
    private int maxActive;

    @Value("60000")
    private int maxWait;

    /**
     * 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
     */
    @Value("60000")
    private int timeBetweenEvictionRunsMillis;
    /**
     * 配置一个连接在池中最小生存的时间,单位是毫秒
     */
    @Value("300000")
    private int minEvictableIdleTimeMillis;

    @Value("SELECT 1 FROM DUAL")
    private String validationQuery;

    @Value("true")
    private boolean testWhileIdle;

    @Value("false")
    private boolean testOnBorrow;

    @Value("false")
    private boolean testOnReturn;

    /**
     * 打开PSCache,并且指定每个连接上PSCache的大小
     */
    @Value("true")
    private boolean poolPreparedStatements;

    @Value("20")
    private int maxPoolPreparedStatementPerConnectionSize;
    /**
     * 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
     */
    @Value("stat,wall,log4j")
    private String filters;
    /**
     * 通过connectProperties属性来打开mergeSql功能;慢SQL记录
     */
    @Value("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500")
    private String connectionProperties;


    @Bean(name = "secDatasource")
    @Qualifier("secDatasource")
    @ConfigurationProperties(prefix = "spring.datasource.sec")
    public DataSource secDataSource() {
        return getDruidDataSource(secUsername, secPassword, secUrl);
    }

    
    @Bean(name = "firstDatasource")
    @Qualifier("firstDatasource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.first")
    public DataSource firstDataSource() {
        return getDruidDataSource(firstUsername, firstPassword, firstUrl);
    }

   

    private DruidDataSource getDruidDataSource(String username, String password, String url) {
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(url);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
            logger.error("druid configuration initialization filter : {0}", e);
        }
        datasource.setConnectionProperties(connectionProperties);
        return datasource;
    }

}
配置数据源
package com.ipinyou.mip.configuration;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;


/**
 * 这样的方式不需要添加注解:@ServletComponentScan
 * Created by Administrator on 2018/2/28.
 */
@Configuration
public class DruidConfiguration {

    @Bean
    public ServletRegistrationBean statViewServlet(){
        //创建Servlet,注册实体
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //设置ip白名单
        servletRegistrationBean.addInitParameter("allow","xx.xx.xx.xx");
        //设置ip黑名单,如果deny和allow共同存在时,deny优先于allow
        servletRegistrationBean.addInitParameter("deny","xx.xx.xx.xx");
        //设置控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername","xxx");
        servletRegistrationBean.addInitParameter("loginPassword","xxx");
        //是否可以重置数据
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean statFilter(){
        //创建过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //设置过滤器过滤路径
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤形式
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}
设置数据源的过滤器
package com.ipinyou.mip.configuration;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary",
        transactionManagerRef = "transactionManagerPrimary",
        basePackages = {"xx.xx.xxx","xx.xx.xx"})//设置dao(repo)所在位置
public class JpaConfiguration {

    @Autowired
    @Qualifier("firstDatasource")
    private DataSource firstDataSource;
    
    @Primary
    @Bean(name = "entityManagerFirst")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    /**
     * 设置实体类所在位置
     */
    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(mipdevDataSource)
                .packages("xx.xx.xx.xx")
                .properties(getVendorProperties())
                .persistenceUnit("firstManageFactory")
                .properties(getVendorProperties())
                .build();
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }
}
firstJpaConfiguration
package com.ipinyou.mip.configuration;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.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;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySec",
        transactionManagerRef = "transactionManagerSec",
        basePackages = {"xx.xx.xx"})//设置dao(repo)所在位置
public class JpaSecConfiguration {

    @Autowired
    @Qualifier("secDatasource")
    private DataSource secDatasource;
    
    @Bean(name = "entityManagerAmp")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryAmp(builder).getObject().createEntityManager();
    }

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    /**
     * 设置实体类所在位置
     */
    @Bean(name = "entityManagerFactorySec")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryAmp(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(secDatasource)
                .packages("xx.xx.xx")
                .properties(getVendorProperties())
                .persistenceUnit("secManageFactory")
                .properties(getVendorProperties())
                .build();
    }

    @Bean(name = "transactionManagerSec")
    public PlatformTransactionManager transactionManagerAmp(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySec(builder).getObject());
    }
}
secJpaConfiguration
package com.ipinyou.mip.configuration;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
@Import(DatasourceConfiguration.class)
public class JdbcTemplateConfiguration {
    @Bean(name = "secJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(
            @Qualifier("secDatasource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


    @Bean(name = "firstJdbcTemplate")
    public JdbcTemplate threeJdbcTemplate(
            @Qualifier("firstDatasource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
JdbcTemplateConfiguration
/**
 * 提供了一系列数据库查询方法
 *
 * @author guodong.zhang
 */
public class CommonDao<E, PK extends Serializable> {

    @PersistenceContext(unitName = "secManageFactory")
    protected EntityManager em;
   /**
     * 相当于jdbctemplate的queryForlist 返回List<Map<String, Object>>
     *
     * @param sql
     * @return
     */
    public List<Map<String, Object>> queryForList(String sql) {
        Query nativeQuery = em.createNativeQuery(sql);
        nativeQuery.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String,Object>> resultList = nativeQuery.getResultList();
        return resultList;
    }
}
具体使用

 

转载于:https://www.cnblogs.com/zqr99/p/9208356.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值