环境: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. (Struts2学习系列三)Struts2动态方法调用:通配符方式

    更改src/struts2.xml的代码: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  2. linux fcntl 对文件描述符控制

    linux fcntl 对文件描述符控制 linux fcntl 对文件描述符控制 linux fcntl 对文件描述符控制

  3. Gulp执行预处理

    1. 在项目中安装 gulp-sass插件来编译Sass npm install gulp-sass --save-dev 2. 在gulpfile.js中编写 var gulp = require( ...

  4. Superset安装出错 sqlalchemy.exc.InvalidRequestError: Can't determine which FROM clause to join from, ...

    $ superset db upgrade ... Traceback (most recent call last): File "/home/jhadmin/.pyenv/version ...

  5. dvaJs使用注意事项

    项目参考地址 dva-yicha 1. 使用路由跳转的方式 (1)所有的路由跳转功能都放到 dva/router 里面的 import { routerRedux } from 'dva/router ...

  6. mysql在win系统dos 安装版配置步骤详解

    1.准备工作 下载mysql的最新免安装版本mysql-noinstall-5.1.53-win32.zip,解压缩到相关目录,如:d:\ mysql-noinstall-5.1.53-win32.这 ...

  7. MySQL数据库中,使用 group by 时,不重复字段如何拼接显示

    就不写文字描述了,直接用图和sql来表述吧. 这是测试数据,表名为 person 现在,我想按照性别进行分组,也就是字段 sex ,同时能将分组后的姓名全部显示出来. sql 语句如下: SELECT ...

  8. error C1083: 无法打开包括文件: “ui_roadquery.h”: No such file or directory c:\users\administrator\desktop\g_w_j\04 开发阶段\开发工程\imcshowapp\imcshowapp\roadquery.h 5 1 IMCShowApp

    1.我这里出现的原因是忘记将项目文件添加进入项目中,在项目中右击添加即可

  9. MySQL基础管理

    1.用户管理 1.用户的作用: 登录:管理相对应的库表 2.定义 定义用户名和白名单 all@'10.0.0.%' 命名用户名时,最好不要太长,要和业务相关 白名单类型: user@'10.0.0.5 ...

  10. CSS如何让文字垂直居中?

    在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...