SpringBoot启动项目报错: Consider defining a bean of type ‘xxx‘ in your configuration.

今天遇到一个问题很奇怪,SpringBoot项目启动报错提示:

Description:

Field userDAO in com.cml.service.impl.UserServiceImpl required a bean of type 'com.cml.dao.UserDAO' that could not be found.


Action:

Consider defining a bean of type 'com.cml.dao.UserDAO' in your configuration.

我开始以为我是接口上面没有加@Mapper注解的原因
但是我看了一下却不是,而且idea提示我可以找到SpringBoot启动项目报错: Consider defining a bean of type ‘xxx‘ in your configuration._第1张图片
SpringBoot启动项目报错: Consider defining a bean of type ‘xxx‘ in your configuration._第2张图片

左边那个绿色小图标点击一下也能进入UserDAO

后来我看了一下日志发现里面有一行警告被我给忽略了

2021-12-26 19:51:52.415 WARN 15212 — [ restartedMain] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in ‘[com.cml.app]’ package. Please check your configuration.

mybatis默认扫描的是启动类下的包,而我的启动类和DAO接口不在同一个包下
SpringBoot启动项目报错: Consider defining a bean of type ‘xxx‘ in your configuration._第3张图片

所以启动类加上@MapperScan注解改一下扫描位置就好了

@SpringBootApplication(
        scanBasePackages = "com.cml"
)
@MapperScan(
        basePackages = "com.cml.dao"
)
public class App {

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

}

运行了一下果然成功了,看来以后不能忽略警告了

你可能感兴趣的:(笔记,intellij-idea,spring,boot,java)