SpringBoot+Mybatis-plus多模块项目,启动后提示找不到XxxMapper或者BaseMapper

一、问题背景

SpringBoot拆分为多模块项目(按不同形式拆分)
按层次拆分:controller一个项目、service一个项目、dao(mapper)一个项目
按业务拆分:一个业务模块拆成一个项目(包含该业务的controller、service、dao等)
拆分完成,启动原来的Application后,提示 APPLICATION FAILED TO START

二、问题现象

Description:

Field userMapper in com.fanzyx.crane.framework.security.service.JwtUserDetailsService required a bean of type 'com.fanzyx.xx.mapper.XxxrMapper' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.fanzyx.xx.mapper.XxxrMapper' in your configuration.

或者提示找不到Mybatis-plus 的BaseMapper中的某个方法(你调用的方法)

三、问题原因

先排查下包引用是否冲突,如果没冲突再看下面

Application中没有配置mapper扫描的基础包路径,就只会在当前模块下寻找

e.g.

@SpringBootApplication()
public class XxxApplication {
    public static void main(String[] args) {
        SpringApplication.run(XxxApplication.class,args);
    }
}

四、解决方案

在XxxApplication(启动类) 上增加注解并加入扫描基础路径

// 多模块项目需要配置扫描基础包,否则会无法装载其他模块的类
@SpringBootApplication(scanBasePackages = {"com.fanzyx.xx"})
// 一定要配置到具体路径,中间 ** 可以匹配多层级目录,一个 * 只能匹配单层级目录
//@MapperScan(basePackages = {"com.fanzyx.xx.**.mapper"})
public class XxxApplication{
    public static void main(String[] args) {
        SpringApplication.run(XxxApplication.class,args);
    }
}
posted @ 2021-12-07 22:30  奇风顿行  阅读(3732)  评论(0编辑  收藏  举报