Multiple datasources with same repositories and entities

折月煮酒 提交于 2019-12-24 19:08:51

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/53307851/multiple-datasources-with-same-repositories-and-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!