【问题标题】:In Spring MVC, How to connect to two databases (Mysql database and MongoDB) in the same project?? if this is possible [closed]在 Spring MVC 中,如何连接到同一个项目中的两个数据库(Mysql 数据库和 MongoDB)?如果可能的话[关闭]
【发布时间】:2015-03-07 22:59:28
【问题描述】:

在Spring MVC中,如何同时连接两个数据库(Mysql数据库和MongoDB)

【问题讨论】:

    标签: spring mongodb


    【解决方案1】:

    您的问题与mvc模块无关,而与数据访问层有关。

    简单来说,您需要配置 2 个不同的数据源,分别具有实体管理器和事务管理器。

    然后在你的 dao 类中你可以注入需要的实体管理器。

    @PersistenceContext(unitName="entityManager1")
    private EntityManager entityManager1;
    
    @PersistenceContext(unitName="entityManager2")
    private EntityManager entityManager2;
    

    根据您在数据层中使用的模块,在 google 上搜索可以最好地找到您的需求。一个建议是看一下spring data module,它抽象了很多东西。

    这里是一个带有spring数据的多个数据源的xml配置示例:https://github.com/spring-projects/spring-data-jpa/blob/master/src/test/resources/multiple-entity-manager-integration-context.xml

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 2018-11-08
      • 2021-03-11
      • 2014-05-14
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      相关资源
      最近更新 更多