环境: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版本有些不太一样,还需要额外注意。

  1. 获取jpa配置的方式
  2. 数据源配置为jdbc-url

要注意:packages不能引用相同的model,否则会导致数据库卡死。

springboot2.x jpa接入多数据源的更多相关文章

  1. springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用

    springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用 原文链接:https://www.cnblogs.com/blog5277/p/10661 ...

  2. Spring Boot 2.x基础教程:Spring Data JPA的多数据源配置

    上一篇我们介绍了在使用JdbcTemplate来做数据访问时候的多数据源配置实现.接下来我们继续学习如何在使用Spring Data JPA的时候,完成多数据源的配置和使用. 添加多数据源的配置 先在 ...

  3. Springboot2 jpa druid多数据源

    package com.ruoyi; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans ...

  4. (四)SpringBoot2.0基础篇- 多数据源,JdbcTemplate和JpaRepository

    在日常开发中,经常会遇到多个数据源的问题,而SpringBoot也有相关API:Configure Two DataSources:https://docs.spring.io/spring-boot ...

  5. 6_1.springboot2.x整合JDBC与数据源配置原理解析

    1.引言 对于数据访问层,无论是SQL还是NOSQL,Spring Boot默认采用整合 Spring Data的方式进行统一处理,添加大量自动配置,屏蔽了很多设置.引入各种xxxTemplate,x ...

  6. grafana接入zabbix数据源

    一.grafana介绍 grafana是开源免费的应用数据可视化仪表盘,由于zabbix本身对监控数据可视化并不侧重,所以大多使用第三方数据可视化工具来做大屏.下面向小伙伴们介绍grafana接入za ...

  7. springboot2.0动态多数据源切换

    摘要:springboot1.x到springboot2.0配置变化有一点变化,网上关于springboot2.0配置多数据源的资料也比较少,为了让大家配置多数据源从springboot1.x升级到s ...

  8. spring boot 整合JPA多数据源

    上个文章介绍了spring boot在使用Mybatis持久化技术的时候如何使用多数据源,今天再补充一个使用spring data jpa实现多数据源的使用情况,JPA是一套数据库持久化规范,或者称之 ...

  9. springboot2多数据源完整示例

    springboot2 + mybatis + mysql + oracle + sqlserver多数据源的配置 相信很多朋友在开发的时候,可能会碰到需要一个项目,配置多个数据源的需求,可能是同一种 ...

随机推荐

  1. requests中text和content的区别

    # -*- coding: utf-8 -*- __author__ = "nixinxin" import re img_url = "https://f11.baid ...

  2. filter的使用场景

    1.filter的使用场景:假如有一个对象数组A,获取数组中的指定类型的对象放到B数组中 我们在ES5先进行for循环遍历数组,再进行if 判断,如果数组中某个对象的类型符合要求,push 到一个新数 ...

  3. lombok @Builder实现原理

    public class Person { private Person(Builder builder) { name = builder.name; age = builder.age; } pr ...

  4. Servlet - Servlet相关

    1. 概念 Servlet是指任何实现了Servlet接口的类, Servlet运行于支持Java的应用服务器中, Servlet可以响应任何类型的请求, 但大多数情况下, Servlet只用来扩展基 ...

  5. js设置当前窗口为最上层窗口

    实际使用iframe框架时会遇到当前窗口页面不能位于最上层窗口的情况,或者是防止自己的网站被别人放在他们的iframe中,这时就需要设置: <script language="java ...

  6. 安装zabbix需求环境

    yum install gcc gcc-c++ make mysql-server mysql-devel libcurl-devel net-snmp-devel php php-ldap php- ...

  7. [C#]记录一次异常排查,关于using语法、sqlserver数据库session、DBHelper类

    最近在做一个基于asp.net和sqlserver的网站项目,发现网站运行一段时间之后,会报异常: 超时时间已到,但是尚未从池中获取连接.出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小 ...

  8. ionic:安装

    ylbtech-ionic:安装 1.返回顶部 1. ionic 安装 本站实例采用了ionic v1.3.2 版本,使用的 CDN 库地址: <link href="https:// ...

  9. 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel 转载 https://www.cnblogs.com/yunfeifei/p/3993401.html

    5天玩转C#并行和多线程编程系列文章目录 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel 5天玩转C#并行和多线程编程 —— 第二天 并行集合和PLinq 5天玩转C#并行和多线程编 ...

  10. 2019 牛客多校第五场 B generator 1

    题目链接:https://ac.nowcoder.com/acm/contest/885/B 题目大意 略. 分析 十进制矩阵快速幂. 代码如下 #include <bits/stdc++.h& ...