其他分享
首页 > 其他分享> > spring – 具有相同实体和repo的多个数据源

spring – 具有相同实体和repo的多个数据源

作者:互联网

目前正在开发我的Spring Boot项目需要的项目
利用同一数据库服务器中的多个数据源或模式.我有
发现了几个教授多个数据源配置的教程
Spring引用,其中实体foo存在于DataSource A中,而bar存在于
DataSource B即下面.,

https://scattercode.co.uk/2016/01/05/multiple-databases-with-spring-boot-
和弹簧数据JPA /
https://scattercode.co.uk/2013/11/18/spring-data-multiple-databases/
https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7

但我的用例是实体foo和bar存在于多个模式中,我想使用单个实体和存储库来访问所有模式.数据不会在所有模式中复制.它在它们之间进行划分.

因此,如果我需要搜索用户John Doe,我必须通过Schema 1和
如果我找不到他,请转到下一个模式.

我已经尝试了以上所有教程(即使他们没有与我的排队
用例)希望我可以破解它以使其正常工作
概念证明.
我也研究过AbstractRoutingDataSource
 (http://fizzylogic.nl/2016/01/24/make-your-spring-boot-application-multi-tenant-aware-in-2-steps/,http://kimrudolph.de/blog/spring-datasource-routing)
和MultiTentancy,但这两个谈论有权访问单一
任何时间点的架构.
我只需要一些指导或链接即可完成.

提前致谢.

解决方法:

您需要查看AbstractRoutingDataSource并使用它.

So if I need to search for User John Doe I have to go through Schema 1 and if I don’t find him, move onto the next schema.

因此,您需要搜索第一个模式,如果没有找到,则继续下一个模式.

在上面链接中给出的那个例子中,

 CustomerContextHolder.setCustomerType(CustomerType.GOLD);
 List<Item> items = catalog.getItems();
 if(isEmpty(goldItems)){
  CustomerContextHolder.setCustomerType(CustomerType.SILVER);
  items = catalog.getItems();  
 }

更多细节可以在in another qn here找到

标签:spring,spring-boot,spring-data-jpa,multi-tenant
来源: https://codeday.me/bug/20190929/1832089.html