springboot2.x jpa接入多数据源
环境:springboot 2.1.4
数据源引入方式
- 数据源一
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackages = {"com.xxj.primary.repository"}
)
public class SourceDataConfig { @Autowired
private HibernateProperties hibernateProperties; @Autowired
private JpaProperties jpaProperties; private Map<String, Object> getVendorProperties() {
return hibernateProperties.determineHibernateProperties(
jpaProperties.getProperties(), new HibernateSettings()
);
} @Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary") # 配置数据源获取的涞源
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
} @Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.properties(getVendorProperties())
.packages("com.xxj.primary.model")
.persistenceUnit("primaryPersistenceUnit")
.build();
} @Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager propertyTransactionManager(
@Qualifier("entityManagerFactoryPrimary") EntityManagerFactory propertyEntityManagerFactory) {
return new JpaTransactionManager(propertyEntityManagerFactory);
}
}
- 数据源二
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactorySecond",
transactionManagerRef = "transactionManagerSecond",
basePackages = {"com.xxj.second.repository"}
)
public class SecondDataConfig { @Autowired
private HibernateProperties hibernateProperties; @Autowired
private JpaProperties jpaProperties; private Map<String, Object> getVendorProperties() {
Map<String, Object> map = hibernateProperties.determineHibernateProperties(
jpaProperties.getProperties(), new HibernateSettings());return map;
} @Primary
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource targetDataSource(){
return DataSourceBuilder.create().build();
} @Primary
@Bean(name = "entityManagerFactorySecond")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder, @Qualifier("secondDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.properties(getVendorProperties())
.packages("com.xxj.second.model")
.persistenceUnit("targetPersistenceUnit")
.build();
} @Primary
@Bean(name = "transactionManagerSecond")
public PlatformTransactionManager propertyTransactionManager(
@Qualifier("entityManagerFactorySecond") EntityManagerFactory propertyEntityManagerFactory) {
return new JpaTransactionManager(propertyEntityManagerFactory);
}
}
数据源配置
spring.datasource.primary.jdbc-url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&useSSL=false&requireSSL=false
spring.datasource.primary.username = root
spring.datasource.primary.password = 1234567
spring.datasource.primary.driver-class-name = com.mysql.jdbc.Driver spring.datasource.second.jdbc-url = jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&useSSL=false&rewriteBatchedStatements=true
spring.datasource.second.username = root
spring.datasource.second.password = 1234567
spring.datasource.second.driver-class-name = com.mysql.jdbc.Driver
springboot 2.x版本jpa多数据源引入跟1.x版本有些不太一样,还需要额外注意。
- 获取jpa配置的方式
- 数据源配置为jdbc-url
要注意:packages不能引用相同的model,否则会导致数据库卡死。
springboot2.x jpa接入多数据源的更多相关文章
- springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用
springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用 原文链接:https://www.cnblogs.com/blog5277/p/10661 ...
- Spring Boot 2.x基础教程:Spring Data JPA的多数据源配置
上一篇我们介绍了在使用JdbcTemplate来做数据访问时候的多数据源配置实现.接下来我们继续学习如何在使用Spring Data JPA的时候,完成多数据源的配置和使用. 添加多数据源的配置 先在 ...
- Springboot2 jpa druid多数据源
package com.ruoyi; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans ...
- (四)SpringBoot2.0基础篇- 多数据源,JdbcTemplate和JpaRepository
在日常开发中,经常会遇到多个数据源的问题,而SpringBoot也有相关API:Configure Two DataSources:https://docs.spring.io/spring-boot ...
- 6_1.springboot2.x整合JDBC与数据源配置原理解析
1.引言 对于数据访问层,无论是SQL还是NOSQL,Spring Boot默认采用整合 Spring Data的方式进行统一处理,添加大量自动配置,屏蔽了很多设置.引入各种xxxTemplate,x ...
- grafana接入zabbix数据源
一.grafana介绍 grafana是开源免费的应用数据可视化仪表盘,由于zabbix本身对监控数据可视化并不侧重,所以大多使用第三方数据可视化工具来做大屏.下面向小伙伴们介绍grafana接入za ...
- springboot2.0动态多数据源切换
摘要:springboot1.x到springboot2.0配置变化有一点变化,网上关于springboot2.0配置多数据源的资料也比较少,为了让大家配置多数据源从springboot1.x升级到s ...
- spring boot 整合JPA多数据源
上个文章介绍了spring boot在使用Mybatis持久化技术的时候如何使用多数据源,今天再补充一个使用spring data jpa实现多数据源的使用情况,JPA是一套数据库持久化规范,或者称之 ...
- springboot2多数据源完整示例
springboot2 + mybatis + mysql + oracle + sqlserver多数据源的配置 相信很多朋友在开发的时候,可能会碰到需要一个项目,配置多个数据源的需求,可能是同一种 ...
随机推荐
- TRUNCATE - 清空一个表
SYNOPSIS TRUNCATE [ TABLE ] name DESCRIPTION 描述 TRUNCATE 快速地从一个表中删除所有行.它和无条件的 DELETE 有同样的效果,不过因为它不做表 ...
- 使用ribbon实现负载均衡
使用ribbon之前的准备工作: 1.你要有两个服务,一个是服务消费方(下图的xing-movie是消费方),一个是服务提供方(xing-user是服务提供者),并且服务提供方要有两个实例,也就是xi ...
- shell脚本中关于日期的操作
一.计算指定日期的前一天的日期 date -d "yesterday 20150401 " +%Y%m%d 二.如果获取当前日期的前一天 date -d " ...
- 单向连通图 Going from u to v or from v to u? poj2762
http://poj.org/problem?id=2762 强连通求子图和子图关系 + 子图关系是链式结构 #include <cstdio> #include <cstdlib& ...
- SpringMVC 拦截器原理
前言 SpringMVC 拦截器也是Aop(面向切面)思想构建,但不是 Spring Aop 动态代理实现的, 主要采用责任链和适配器的设计模式来实现,直接嵌入到 SpringMVC 入口代码里面. ...
- 微信小程序自定义导航栏组件,完美适配所有手机,可实现各种功能和情况
背景 在做小程序时,关于默认导航栏,我们遇到了以下的问题: Android.IOS 手机对于页面 title 的展示不一致,安卓 title 的显示不居中 页面的 title 只支持纯文本级别的样式控 ...
- JavaWeb学习篇之----web应用的虚拟目录映射和主机搭建(Tomcat)
从今天开始来学习JavaWeb的相关知识,之前弄过一段时间JavaWeb的,就是在做毕业设计的时候搞过,但是那时候完全是为了任务去学习,所以效果不好,好多东西都没有深入的研究过,所以接下来的一段时间我 ...
- Delphi操作ACCESS技巧集
1.DELPHI中操作access数据库(建立.mdb文件,压缩数据库)以下代码在WIN2K,D6,MDAC2.6下测试通过,编译好的程序在WIN98第二版无ACCESS环境下运行成功.//在之前us ...
- easyUI tabs 显示与隐藏 tab 页
隐藏: tab_option = $('#tabs').tabs('getTab'," 单位信息 ").panel('options').tab; tab_option.hide( ...
- 4.RabbitMQ Linux安装
这里使用的Linux是CentOS6.2 将/etc/yum.repo.d/目录下的所有repo文件删除 先下载epel源 # wget -O /etc/yum.repos.d/epel-erlan ...