【问题标题】:spring boot with multiple database - both of them have the same schema name and table name具有多个数据库的spring boot - 它们都具有相同的模式名称和表名称
【发布时间】:2022-01-20 13:54:26
【问题描述】:

我有两个数据库,但它们都包含相同的表名和模式名。 那么当我创建实体类时,如何指定哪个类连接到第一个数据库,另一个连接到第二个数据库?

【问题讨论】:

    标签: spring spring-boot jpa spring-data-jpa multiple-databases


    【解决方案1】:

    Spring Boot 为您完成。您所要做的就是向 Spring 表明您需要 2 个数据源。

    为此,您必须这样做:

    1. 创建 2 个单独的配置类。
    2. 您必须在每个中声明 bean:dataSource、entityManager 和 transactionalManager。
    3. 使用带有“前缀”的 @ConfigurationProperties 注释标记数据源 bean,该前缀在您的 properties/yml 文件中分隔数据源道具。
    4. 必须用@EnableJpaRepositories 注解标记配置类。在注释的参数中,您必须指定“basePackages” - 该值应指向您的实体模型(例如:“com.myprj.dao.entity.user”), "entityManagerFactoryRef" 作为你的 entityManager bean 的名称, “transactionManagerRef”作为 transactionalManager bean 的名称。

    Spring 将管理其余的工作。

    【讨论】:

      猜你喜欢
      • 2013-12-27
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 2014-11-28
      相关资源
      最近更新 更多