解决启动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扫描路径是否正确:

image-20230801180028233

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.memory.memoryiconbackend.mapper.WallpaperMapper">
​
    <resultMap id="BaseResultMap" type="com.memory.memoryiconbackend.model.Wallpaper">
            <result property="id" column="id" jdbcType="VARCHAR"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="url" column="url" jdbcType="VARCHAR"/>
            <result property="type" column="type" jdbcType="VARCHAR"/>
            <result property="tags" column="tags" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time" jdbcType="VARCHAR"/>
            <result property="updateTime" column="update_time" jdbcType="VARCHAR"/>
            <result property="isDelete" column="is_delete" jdbcType="VARCHAR"/>
            <result property="userId" column="user_id" jdbcType="VARCHAR"/>
    </resultMap>
​
    <sql id="Base_Column_List">
        id,name,url,
        type,tags,create_time,
        update_time,is_delete,user_id
    </sql>
</mapper>
  • 确保XXXMapper包的扫描路径正确后,再继续排查:

    XXXMapper上添加@Mapper注解

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

    image-20230801180147709

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

    开启@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注解

    image-20230801182811187

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

    总结

    • 如果你已经在XXXMapper上添加了@Mapper注解,请把启动类和MybatisPlusConfig配置类上的@MapperScan注解删除
    • 如果你已经在启动类和MybatisPlusConfig配置类上添加了@MapperScan注解,请把XXXMapper上的@Mapper注解删除
    • 希望这篇文章对你有帮助,感谢您的支持!😁
  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值