Spring Boot + MyBatis No qualifying bean of type

记录一个今天同事遇到的BUG,以前也遇到过,但是当时没有记录,结果浪费了两个小时时间处理这个BUG。

项目是Spring Boot + MyBatis,项目包结构如下:

Spring Boot + MyBatis No qualifying bean of type_第1张图片

代码报错为:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.jy.common.base.BaseMapper
	' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
	{@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
	... 32 more

说我没有定义Bean。

No qualifying bean of type 
检查了一遍又一遍的代码,我定义了啊!没毛病啊!Pom里面该引入的都引入了啊!MybatisConfig文件也写的没毛病啊!

检查了一个小时...

后来想起来以前解决过类似的问题,当年那是在做SpringMVC+MyBatis结合的项目的时候,也出过这种诡异的事情。问题出在我的项目目录结构有问题,

或者说我的MybatisConfig文件的扫描注解路径有问题。包结构看上面... MyBatisConfig代码如下:

@Configuration
@MapperScan("com.jy.affairs.mapper.*")
public class MybatisConfig {


    @Autowired
    private Environment environment;
    private RelaxedPropertyResolver propertyResolver;
    @Autowired
    private DataSource dataSource;
    @Autowired
    private MybatisProperties properties;
    @Autowired
    private ResourceLoader resourceLoader = new DefaultResourceLoader();
    @Autowired(required = false)
    private Interceptor[] interceptors;
    @Autowired(required = false)
    private DatabaseIdProvider databaseIdProvider;
注解的路径是:

@MapperScan("com.jy.affairs.mapper.*")
MyBatis的Mapper扫描路径是指的包名,* 不代表类,代表的是包,而项目中的Mapper类都在com.jy.affairs.mapper包下面,并没有再向下的层级,所以Mybatis没有扫描到Mapper类,就报了刚才的错误。

解决办法:

根据项目情况,是同事在建包结构的时候有问题,所以不改注解中的路径,而是修改包结构,修改为:

Spring Boot + MyBatis No qualifying bean of type_第2张图片


再次Run,成功!



你可能感兴趣的:(BUG处理记)