• 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
  • 相关阅读:
    javascript之createElement事件
    rgb颜色列表
    欧美很好听的调调
    转:JMeter--使用代理录制Web性.能测试脚.本
    转:使用 JMeter 完成常用的压力测试
    转: 理解 JMeter 聚合报告(Aggregate Report)
    转:jmeter实践
    转:支付漏洞的三种常见类型
    转:LoadRunner获取毫秒及字符串替换实现
    转:Windows平台配置Appium+Java环境
  • 原文地址:https://www.cnblogs.com/vipsoft/p/15035802.html
Copyright © 2020-2023  润新知