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
原文地址:https://www.cnblogs.com/vipsoft/p/15035802.html