zoukankan      html  css  js  c++  java
  • Mybatis 模块拆份带来的 Mapper 扫描问题

     项目中,两个模块中都放了 Mapper,如下所示

    @MapperScan(basePackages ={"com.vipsoft.his.mapper","com.vipsoft.his.sdfyy.mapper"} )

    为了方便,两个合成了一个,

    @MapperScan(basePackages ={"com.vipsoft.his"} )

    问题来了

    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    Field fromHisApi in com.vipsoft.his.web.controller.CustodyController required a single bean, but 2 were found:
      - fromHisApiImpl: defined in file [D:Projects80.HISTestMastervipsoft-his-sdfyy	argetclassescomvipsofthissdfyyserviceimplFromHisApiImpl.class]
      - IFromHisApi: defined in file [D:Projects80.HISTestMastervipsoft-his-api	argetclassescomvipsofthisapiIFromHisApi.class]
    
    
    Action:
    
    Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

    解决办法【不好】:在实现类上增加 @Primary,或者在调用的地方加上 @Qualifier("fromHisApiImpl") 

    最终解决方案:

    两个合一个
    @MapperScan(basePackages ={"com.vipsoft.his.mapper"} )

    注意:

    1. mapper xml放的目录(mapper namespace 前面要一致)

    2. public interface HisMapper  mapper接口类的 package 要= MapperScan 中的 packages 否则会报找不到 xxx.xml

    另一个问题来了。C引用A,C里的 Mapper可以了,A里面的不行了。

    原因:classpath只加载了一个mapper的改成

    解决方案:classpath带有一个" * "号

    mybatis:
      # 指定sql映射文件位置
        mapper-locations: classpath:mapper/*.xml
    变为:
    mybatis:
      # 指定sql映射文件位置
        mapper-locations: classpath*:mapper/*.xml
  • 相关阅读:
    java开发实战经典
    defer属性---->执行外部脚本
    深入理解JavaScript 模块模式
    JavaScript模块化开发一瞥
    缓存是新的内存
    php站点
    数据库
    .NET中使用Redis
    Scrum中的User Story
    如何编写敏捷开发中的user story
  • 原文地址:https://www.cnblogs.com/vipsoft/p/15035802.html
Copyright © 2011-2022 走看看