文档章节

Spring-Boot JPA双数据源配置

Pasenger
 Pasenger
发布于 2017/03/23 12:09
字数 504
阅读 107
收藏 1

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没区别。

 

© 著作权归作者所有

Pasenger
粉丝 5
博文 34
码字总数 20821
作品 0
朝阳
高级程序员
私信 提问
spring-boot和JPA多数据源整合

今天弄一下spring-boot和jpa的多数据源整合 Jpa(Java Persistence API)Java持久化API,它是一套ORM规范 只是一套规范 Spring Boot中使用的Jpa实际上是Spring Data Jpa,Spring Data是Sprin...

咸鱼-李y
06/01
107
0
深入 Spring Boot :实现对 Fat Jar jsp 的支持

原文出处:Hengyunabc spring boot 对于jsp支持的限制 对于jsp的支持,Spring Boot官方只支持了war的打包方式,不支持fat jar。参考官方文档: https://docs.spring.io/spring-boot/docs/cu...

Hengyunabc
2018/06/04
0
0
Spring Boot 学习系列(08)—自定义servlet、filter及listener

此文已由作者易国强授权网易云社区发布。 欢迎访问网易云社区,了解更多网易技术产品运营经验。 传统的filter及listener配置 在传统的Java web项目中,servlet、filter和listener的配置很简单...

网易云
2018/11/01
0
0
Spring Boot整合模板引擎jsp

jsp也算是一种模板引擎吧。整合jsp前,先说一下运行SpringBoot项目的几种方式 1. 运行SpringBoot项目的几种方式 1.1 使用内嵌Tomcat运行项目 在IDE中右键运行启动类,也就是直接直接运行App...

yysue
2018/06/15
333
5
spring3 ,spring3 mvc 配置文件整合问题,哪种配置更好?

正常的整合是要在web.xml里面配置spring监听器,spring mvc的dispath servlet,代码如下: Java代码 org.springframework.web.context.ContextLoaderListener contextConfigLocation /WEB-I......

_凤求凰_
2014/01/07
2.4K
3

没有更多内容

加载失败,请刷新页面

加载更多

读书笔记:深入理解ES6 (五)

第五章 解构:使数据访问更便捷 第1节 为什么使用解构功能?   在ES5中,开发者们从对象、数组中获取特定数据并赋值给变量,编写了很多看起来同质化的代码。例如: 1 let options = {2 ...

张森ZS
18分钟前
17
0
CentOS7 yum方式安装MySQL5.7

在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。 1 下载并安装MySQL官方的 Yum Repository [root@localho...

roockee
26分钟前
13
0
Allegro三种自定义设置快捷键的方法

Allegro自定义设置快捷键的三种方法: 1、在Allegro PCB editor 命令窗口直接定义 2、通过修改用户变量env文件来设置快捷键 3、定义笔画为快捷键 1、在Allegro PCB editor 命令窗口直接定义 ...

demyar
31分钟前
15
0
如何做一张能让人眼前一亮的大屏?

作为在职场驰骋的社会人,提到数据可视化大家应该都不陌生了。数据可视化的作用也不用我多说,主要是利用图形化手段,更清晰直观地将数据展示。多层次、交互式的可视化分析能够方便决策者理解...

朕想上头条
31分钟前
8
0
TL138/1808/6748-EthEVM开发板硬件CPU、FLASH、RAM

TL138/1808/6748-EthEVM是广州创龙基于SOM-TL138/1808/6748核心板开发的一款开发板,具有三个网络接口。由于SOM-TL138/1808/6748核心板管脚兼容,所以此三个核心板共用同一个底板。开发板采用...

Tronlong创龙
36分钟前
11
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部