解决启动SpringBoot项目报错:Unsatisfied dependency expressed through field ‘baseMapper‘.....问题

Unsatisfied dependency expressed through field 'baseMapper',XXXMapper包扫描不到

  • 当你看到这样的报错,你会怎么解决呢:

    Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.memory.memoryiconbackend.mapper.WallpaperMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

    • 这个报错信息大致意思是,未扫描到你的XXXMapper包,项目启动失败
    • 这个问题可谓最常见了,刚刚我就又被这个问题恶心到了,网上查了半天,感觉他们都是一知半解
    • 那么我是怎么解决这个问题的呢?思路如下:

      XXXMapper.xml配置错误

      • 检查resource下的XXXMapper.xml配置,检查实体类扫描和mapper扫描路径是否正确:

        ​
                                                                                                                            ​
                    id,name,url,
                type,tags,create_time,
                update_time,is_delete,user_id
            
        • 确保XXXMapper包的扫描路径正确后,再继续排查:

          XXXMapper上添加@Mapper注解

          • 检查XXXMapper上是否添加了@Mapper注解:

            @Mapper
            public interface WallpaperMapper extends BaseMapper {
            ​
            }
            • 如果这两部还没有解决你的问题,请一定继续往下看:

            开启@MapperScan注解

            • @MapperScan注解是干嘛的呢?它是用来在项目启动后,扫描你的XXXMapper所在路径,用法如下:
            @SpringBootApplication
            @MapperScan("com.memory.memoryiconbackend.mapper.WallpaperMapper")
            public class MemoryIconBackendApplication {
                public static void main(String[] args) {
                    SpringApplication.run(MemoryIconBackendApplication.class, args);
                }
            }
            • 那这个注解跟上面提到的@Mapper注解,功能不是一样的吗?都是将XXXMapper标注为一个Bean,交给Spring管理
            • 没错,这两个注解的作用是可以说是一摸一样的,无非就是写的位置不一样
            • 正是因为这两个注解作用是一样的,所以在开发过程中,这两个注解写一个就行,而且只能写一个,否则会报错
            • 网上总会有蠢蛋,说在XXXMapper上,添加了@Mapper注解之后,一定不要忘了在启动类上添加@MapperScan注解
            • 这种方法肯定解决不了问题,是大错特错的
            • 所以,如果你已经在XXXMapper上添加了@Mapper注解,一定记得删除启动类上的@MapperScan注解
            • 如果到这里,你已经按照上面的方法解决了问题,成功启动了项目,恭喜你;如果仍旧报错,请继续往下看:

            MybatisPlusConfig配置

            • 我们在项目中,导入了MybatisPlus依赖之后,总会写一个MybatisPlusConfig的分页配置:
            /**
             * mybatis-plus 分页配置类
             */
            @Configuration
            @MapperScan("com.memory.memoryiconbackend.mapper.WallpaperMapper")
            public class MybatisPlusConfig {
                @Bean
                public MybatisPlusInterceptor mybatisPlusInterceptor() {
                    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
                    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
                    return interceptor;
                }
            }
            • 如果你的问题没有解决,一定是因为在这个配置类上边,写上了@MapperScan注解

              • 而这个注解的作用,跟启动类上的@MapperScan注解的作用是一模一样的,删除它就好了

              总结

              • 如果你已经在XXXMapper上添加了@Mapper注解,请把启动类和MybatisPlusConfig配置类上的@MapperScan注解删除
              • 如果你已经在启动类和MybatisPlusConfig配置类上添加了@MapperScan注解,请把XXXMapper上的@Mapper注解删除
              • 希望这篇文章对你有帮助,感谢您的支持!😁