具有相同存储库和实体的多个数据源 [英] Multiple datasources with same repositories and entities

查看:40
本文介绍了具有相同存储库和实体的多个数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前要求使用 jpa 在 Spring Boot 中允许相同存储库/实体的多个数据源.

我设法找到的总是与多个数据源相关,具有多个存储库/实体.

然而,在这里我希望多个数据源具有相同的存储库和实体(即相同的数据库结构).

并基于相同的属性,以编程方式激活特定数据源.

我在这里尝试了安装演示:
1. application.properties(默认):如您所见,您可以在数据源文件之间切换.
2. application-test.properties(示例)
我希望这可以帮助你.:D

I have a current requirement of allowing multiple data sources for the same repositories/entities in spring boot using jpa.

What I've managed to find was always related to multiple datasources, with multiple repositories/entites.

However, here I would like to have the same repositories and entities (ie same database structure) for multiple datasources.

And based on same property, programmatically activate a specific datasource.

I've tried the setup demo'ed here: https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources

And in both configurations use the same package in the factoryBean.setPackagesToScan() call. This does now work however, one datasource overrides the other.

My current test can be found here: https://github.com/nWidart/spring-data-multi-datasource/tree/master/src/main/java/com/example/multidatasources

It shows the 2 configuration files (client1 and client2) with a repository and entity. The controller has 2 endpoints for both data sources (not working).

Thanks!

解决方案

you can do like below :


1. application.properties (as default) : as you see, you can switch between datasource files.
2. application-test.properties (sample)
I hope this can help you. :D

这篇关于具有相同存储库和实体的多个数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆