【问题标题】:Multiple datasources with same repositories and entities具有相同存储库和实体的多个数据源
【发布时间】:2019-04-17 20:54:08
【问题描述】:

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

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

但是,在这里我希望为多个数据源提供相同的存储库和实体(即相同的数据库结构)。

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

我已经尝试过这里的设置演示:https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources

在两种配置中,factoryBean.setPackagesToScan() 调用中使用相同的包。但是现在这确实有效,一个数据源会覆盖另一个数据源。

我目前的测试可以在这里找到:https://github.com/nWidart/spring-data-multi-datasource/tree/master/src/main/java/com/example/multidatasources

它显示了带有存储库和实体的 2 个配置文件(client1 和 client2)。控制器对两个数据源都有 2 个端点(不工作)。

谢谢!

【问题讨论】:

    标签: java spring spring-boot spring-data-jpa spring-data


    【解决方案1】:

    你可以像下面这样:


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

    【讨论】:

    • 为您补充。在大项目中,文件应用程序属性不保存在项目中。它有信息连接到数据库而不是安全性。你应该在这里使用spring cloud config https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html
    • 谢谢。但是,我希望在一个数据源中执行此操作。我遇到了这篇接近的博客文章:spring.io/blog/2007/01/23/dynamic-datasource-routing
    猜你喜欢
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多