Spring-Boot JPA双数据源配置

application.properties中数据源配置:


# 认证中心数据库配置
spring.datasource.auth-center.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.auth-center.url=jdbc:mysql://mysql70:4006/wifi_plt_apps?characterEncoding=UTF8&connectTimeout=0
spring.datasource.auth-center.username=rw
spring.datasource.auth-center.password=123456
spring.datasource.auth-center.name=radius3
spring.datasource.auth-center.max-active=100
spring.datasource.auth-center.max-idle=90
spring.datasource.auth-center.min-idle=10
spring.datasource.auth-center.initial-size=10
spring.datasource.auth-center.test-on-borrow=false
spring.datasource.auth-center.test-while-idle=true
spring.datasource.auth-center.test-on-return=false
spring.datasource.auth-center.remove-abandoned=true
spring.datasource.auth-center.log-abandoned=true
spring.datasource.auth-center.remove-abandoned-timeout=120
spring.datasource.auth-center.time-between-eviction-runs-millis=60000
spring.datasource.auth-center.num-tests-per-eviction-run=1

# 数据中心MySQL配置
spring.datasource.data-center.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.data-center.url=jdbc:mysql://mysql-idc:3306/bigdata?characterEncoding=UTF8&connectTimeout=0
spring.datasource.data-center.username=all_read
spring.datasource.data-center.password=2wsx@WSX
spring.datasource.data-center.name=bigdata
spring.datasource.data-center.max-active=10
spring.datasource.data-center.max-idle=8
spring.datasource.data-center.min-idle=2
spring.datasource.data-center.initial-size=2
spring.datasource.data-center.test-on-borrow=false
spring.datasource.data-center.test-while-idle=true
spring.datasource.data-center.test-on-return=false
spring.datasource.data-center.remove-abandoned=true
spring.datasource.data-center.log-abandoned=true
spring.datasource.data-center.remove-abandoned-timeout=120
spring.datasource.data-center.time-between-eviction-runs-millis=60000
spring.datasource.data-center.num-tests-per-eviction-run=1

DataSourceConfig类:

package com.pasenger.jpa.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * MySQL数据源配置
 * Created by Pasenger on 2017/3/20.
 */

@Configuration
public class DataSourceConfig {

    @Bean(name = "authCenterDataSource")
    @Qualifier("authCenterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.auth-center")
    public DataSource primaryDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "dataCenterDataSource")
    @Qualifier("dataCenterDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.data-center")
    public DataSource dataCenterDataSource(){
        return DataSourceBuilder.create().build();
    }

}

JPA1类:

package com.pasenger.jpa.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

/**
 * 认证中心JPA配置
 * Created by Pasenger on 2017/3/20.
 */

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages = {"cn.gcks.wifi.repository.authcenter", "cn.gcks.wifi.entity.authcenter"}   //设置Repository所在位置
)
public class AuthCenterJPAConfig {

    @Autowired
    @Qualifier("authCenterDataSource")
    private DataSource authCenterDataSource;



    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(authCenterDataSource)
                .properties(getVendorProperties(authCenterDataSource))
                .packages("cn.gcks.wifi.repository.authcenter", "cn.gcks.wifi.entity.authcenter") //设置实体类所在位置
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }
}

JPA2类:

package com.pasenger.jpa.config;

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

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

/**
 * 数据中心JPA配置
 * Created by Pasenger on 2017/3/20.
 */

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactorySecondary",
        transactionManagerRef="transactionManagerSecondary",
        basePackages = {"cn.gcks.wifi.repository.datacenter", "cn.gcks.wifi.entity.datacenter"}   //设置Repository所在位置
)
public class DataCenterJPAConfig {

    @Autowired
    @Qualifier("dataCenterDataSource")
    private DataSource dataCenterDataSource;

    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(dataCenterDataSource)
                .properties(getVendorProperties(dataCenterDataSource))
                .packages("cn.gcks.wifi.repository.datacenter", "cn.gcks.wifi.entity.datacenter") //设置实体类所在位置
                .persistenceUnit("secondaryPersistenceUnit")
                .build();
    }

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }

}

说明:

    JPAConfig中packages为实体类和Respository所在的包名。

    设置正确后使用起来的但数据源JPA没区别。

 

转载于:https://my.oschina.net/pasenger/blog/865331

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值