搜索
查看
编辑修改
首页
UNITY
NODEJS
PYTHON
AI
GIT
PHP
GO
CEF3
JAVA
HTML
CSS
搜索
二进制舞者2
这个屌丝很懒,什么也没留下!
关注作者
热门标签
jquery
HTML
CSS
PHP
ASP
PYTHON
GO
AI
C
C++
C#
PHOTOSHOP
UNITY
iOS
android
vue
xml
爬虫
SEO
LINUX
WINDOWS
JAVA
MFC
CEF3
CAD
NODEJS
GIT
Pyppeteer
article
热门文章
1
难题 [已解决] :如何给Ubuntu系统安装无线网卡驱动?_ubuntu 网卡驱动
2
2023年最强手机远程控制横测:ToDesk、向日葵、Airdroid三款APP免Root版本
3
aigc修复美颜学习笔记
4
【MQTT】 emqx | mqtt | 共享订阅、延迟发布 | 分组订阅 | 集群订阅_mqtt 共享订阅
5
前端开发新趋势:Web3、区块链与虚拟现实_web3时代的前端开发
6
网络编程六--UDP服务器客户端_udp客户端
7
【网络奇遇记】揭秘计算机网络的性能指标:时延带宽积|往返时间|利用率|丢包率
8
深入探讨YOLOv8 网络架构
9
【期末复习】微信小程序复习大纲_第2章 微信小程序页面制作 csdn
10
5分钟搭建开源运维监控工具Uptime Kuma并实现无公网IP远程访问_uptime安装
当前位置:
article
> 正文
4. Spring Boot 1.2.5,Spring Data JPA多数据源支持_jpaproperties.gethibernateproperties
作者:二进制舞者2 | 2024-01-31 22:19:24
赞
踩
jpaproperties.gethibernateproperties
1 配置文件
Properties代码
wisely.primary.datasource.driverClassName=oracle.jdbc.OracleDriver
wisely.primary.datasource.url=jdbc\:oracle\:thin\:@192.168.1.103\:1521\:xe
wisely.primary.datasource.username=gis
wisely.primary.datasource.password=gis
wisely.secondary.datasource.driverClassName=oracle.jdbc.OracleDriver
wisely.secondary.datasource.url=jdbc\:oracle\:thin\:@192.168.1.103\:1522\:xe
wisely.secondary.datasource.username=gis
wisely.secondary.datasource.password=gis
2 datasource配置
第一个数据源
@Configuration
public class DataSourcePrimaryConfig {
@Bean(name = "primaryDS") @Qualifier("primaryDS")
@Primary
@ConfigurationProperties(prefix="wisely.primary.datasource")
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
}
}
第二个数据源
@Configuration
public class DataSourceSecondaryConfig {
@Bean(name = "secondaryDS") @Qualifier("secondaryDS")
@ConfigurationProperties(prefix="wisely.secondary.datasource")
public DataSource secondaryDataSource(){
return DataSourceBuilder.create().build();
}
}
3 实体管理器及事务管理器配置
第一个数据源
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactoryPrimary",transactionManagerRef="transactionManagerPrimary",basePackages= { "com.wisely.demo.dao.one" })//设置dao(repo)所在位置
public class RepositoryPrimaryConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired @Qualifier("primaryDS")
private DataSource primaryDS;
@Bean(name = "entityManagerPrimary")
@Primary
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactoryPrimary")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDS)
.properties(getVendorProperties(primaryDS))
.packages("com.wisely.demo.domain.one") //设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit")
.build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerPrimary")
@Primary
PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
第二个数据源
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactorySecondary",transactionManagerRef="transactionManagerSecondary",basePackages= { "com.wisely.demo.dao.two" })
public class RepositorySecondaryConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired @Qualifier("secondaryDS")
private DataSource secondaryDS;
@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondaryDS)
.properties(getVendorProperties(secondaryDS))
.packages("com.wisely.demo.domain.two")
.persistenceUnit("secondaryPersistenceUnit")
.build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
4 使用
此时来自不同数据库的dao(repo)可以任意在其它的bean里注入
@Controller
public class TestController {
@Autowired
SysRoleRepo1 sysRoleRepo1;
@Autowired
SysRoleRepo2 sysRoleRepo2;
@RequestMapping("/test")
public @ResponseBody String test(){
System.out.println(Lists.newArrayList(sysRoleRepo1.findAll()).size());
System.out.println(Lists.newArrayList(sysRoleRepo2.findAll()).size());
return "ok";
}
}
声明:
本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:
https://www.wpsshop.cn/article/detail/52315
推荐阅读
article
Spring Boot 实战 | Spring Boot整合JPA常见问题解决方案...
Spring Boot 整合 JPA(Java Persistence API)主要是指将 Spring Boot 与 ...
赞
踩
article
Spring Boot 整合RabbitMQ...
在业务活动期间,由于用户请求量短时间内剧增,可能导致系统压力过大甚至崩溃。通过
消息队列
实现请求的缓冲。在高并发场景下,系...
赞
踩
article
【微服务】
spring webflux
响应式编程使用详解_webflux 微服务...
webflux使用
详解_webflux 微服务webflux 微服务 目录 一、webflux介...
赞
踩
article
Spring Boot 整合 分布式
搜索引擎
Elastic Search 实现 搜索、分页与结果过...
Spring Boot 整合 Elastic Search 如何 实现 旅游 搜索、分页与结果过滤?_springboo...
赞
踩
article
Spring RabbitMQ那些事(2-两种方式实现延时消息订阅)...
被消费者nack(negatively acknowleged)的消息。TTL过期后未被消费的消息。超过队列长度限制后被...
赞
踩
article
(附源码)
spring
boot
大学毕业设计管理系统 毕业设计 030945_基于
spring
boo...
使用大学毕业设计管理系统的分为管理员和用户、两个权限模块。管理员所能使用的功能主要有首页、站点管理(轮播图、公告栏)用户...
赞
踩
article
【Git】解决fatal: unable to access..Failure when recei...
因为
git
在拉取或者提交项目时,中间会有
git
的http和https代理,但是我们本地环境本身就有SSL协议了,所以取消...
赞
踩
article
Spring Boot 整合SpringSecurity和JWT和Redis实现统一鉴权认证...
Spring Security是一个强大且高度可定制的身份验证和访问控制框架。它是保护基于Spring的应用程序的实际标...
赞
踩
article
【Spring Cloud Alibaba】2.服务注册与发现(Nacos安装)_nacos.cor...
我们要搭建一个`Spring Cloud Alibaba`项目就绕不开`Nacos`,阿里巴巴提供的`Nacos`组件,...
赞
踩
article
Spring Boot 实战 | Spring Boot整合JPA常见问题解决方案_dependen...
Spring Boot 整合 JPA(Java Persistence API)主要是指将 Spring Boot 与 ...
赞
踩
article
HarmonyOS应用开发-首选项与后台通知管理_@ohos.data.preferences...
首选项为应用提供Key-Value键值型的数据存储能力,支持应用持久化轻量级数据,并对其进行增删除改查等。该存储对象中的...
赞
踩
article
【SpringCloud】7、Spring Cloud Gateway限流配置...
Spring Cloud Gateway 的限流配置主要涉及到令牌桶算法的实现。令牌桶算法可以对某一时间窗口内的请求数进...
赞
踩
article
Git配置代理:fatal: unable to access***
git
hub Failure ...
Git 设置代理,解决不能访问
git
hub 问题。_
git
failure when receiving data f...
赞
踩
article
【
Spring
Boot
3】【
数据源
】自定义
JDBC
数据源
...
开发
Spring
Boot
JDBC
应用时,一旦引入依赖并添加正确的
数据源
对象实例(事务管理器对象实例(对象实例(我们可...
赞
踩
article
Spring
MVC 的
controller
方法
返回值...
说明:
controller
方法
中定义ModelAndView对象并返回,对象中可添加model数据、指定view。Spr...
赞
踩
article
Spring
Boot
自定义
启动
Banner
在线生成工具...
记录一下,后续有更新添加。
Spring
Boot
自定义
启动
Banner
在线生成工具 ...
赞
踩
article
l
in
ux
centos
系统盘
文件系统
损坏-已解决_
corruption
of
in
-memory...
当我们使用的L
in
ux虚拟机(云服务器/vps)磁盘出现xfs
文件系统
损坏时,该如何进行修复?xfs格式
文件系统
损坏,是...
赞
踩
article
【
Unity
】
Attribute
meta-
data
#com.google.
android
.
play
...
Max聚合安装了多个广告源,不确定是哪个广告的SDK依赖更高版本结算库,导致SDK依赖和IAP依赖冲突。2、Max由6....
赞
踩
article
解决编译失败 Attribute
meta
-
data
#
android
.
support
.
VERSION
...
报错如下: Attribute
meta
-
data
#
android
.
support
.
VERSION
@
value
valu...
赞
踩
article
第八章 :
Spring
cloud
网关
中心
Gateway
(
动态
路由
)...
重点介绍
动态
网关
路由
的背景、
动态
路由
与静态
路由
的概念,以及如何基于Nacos实现
动态
网关
路由
的实战案例。第八章 : S...
赞
踩
相关标签
spring boot
后端
java
jpa
java-rabbitmq
rabbitmq
消息队列
削峰
webflux使用
webflux使用总结
webflux使用详解
spring webflux
java使用webflux
elasticsearch
搜索引擎
大数据
微服务
spring
mysql
python