环境: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. CentOS7.6下安装MySQL

    注:本教程使用XShell ssh到CentOS服务器,并使用root用户登录,如使用其他普通用户登录,请在命令前加sudo 1).在/usr/local/目录下(看个人情况)新建文件夹mysql用来 ...

  2. HashMap 1.7 与 1.8 的 区别,说明 1.8 做了哪些优化,如何优化的

    JDK1.7用的链表散列结构,JDK1.8用的红黑树 在扩充HashMap的时候,JDK1.7的重新计算hash, JDK1.7只需要看看原来的hash值新增的那个bit是1还是0就好了,是0的话索引 ...

  3. [翻译]windows下 连接到 bitnami的phpmyadmin

    bitnami 因为安全考虑,只能 localhost 访问 phpmyadmin 为了能通过SSH 隧道访问 phpMyAdmin,你需要一个ssh 客户端.参考文章介绍使用中选择使用 PuTTY, ...

  4. 50. Set接口和Set的实现类HashSet

    集合分类:-------------------| Collection 单列集合的根接口   ---------------| List 如果实现了List接口的集合类,具备的特点是:有序,可重复- ...

  5. HTML+CSS项目——模拟京东网页

    项目准备 项目名称:京东商城 项目描述:京东首页公共部分的头部和尾部制作,京东首页中间部分. 设计目标 保证浏览器 ie7及以上, 火狐, 360, safari,chrome等.谁让我再测ie6,就 ...

  6. bzoj1029题解

    [解题思路] 贪心,先按结束时间排序,从左到右扫描过去,如果当前建筑可以修复则入大根堆,否则,若其修复时间比堆顶小则弹出堆顶并入堆,处理完堆后则更新总时间.复杂度O(nlog2n). [参考代码] # ...

  7. VC图片裁剪源代码

    #include <atlimage.h> int main() { CString filepathname = "D:\\1.png", filepathname1 ...

  8. NX二次开发-UFUN CSYS坐标系转换UF_CSYS_map_point

    1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_curve.h> 5 #include <uf_csys.h> 6 ...

  9. NX二次开发-NXOPEN设置工程图表格注释字体workPart->Fonts()->AddFont("chinesef_fs", NXOpen::FontCollection::TypeNx);

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

  10. 转-C/C++ new与malloc区别

    1.属性 new/delete是C++关键字,需要编译器支持.malloc/free是库函数,需要头文件支持. 2.参数 使用new操作符申请内存分配时无须指定内存块的大小,编译器会根据类型信息自行计 ...