springboot和mybatisplus的版本匹配与整合问题:Invalid bean definition with name ‘xxxMapper‘ defined in flle解决方法

06-01 1370阅读

一般来说出现这个问题Invalid bean definition with name ‘xxxMapper‘ defined in flle主要是下面几个原因:

  1. Mapper 接口未被正确扫描
  2. Lombok 未正确生成代码
  3. JDK 版本不匹配
  4. MyBatis 版本与 Spring Boot 不兼容

现在给出对应的解决方法,希望能帮到您

1. 确认 Mapper 接口扫描配置

(1)在mapper包下所有的类都分别添加@mapper注解(不建议)

(2)编写mybayisPlusConfig配置类并且添加扫描包

@Configuration

@MapperScan("com.atguigu.lease.web.*.mapper")

public class MybatisPlusConfiguration {}

(2)在启动类(XXXXApplication.java)中添加 @MapperScan 注解:

@SpringBootApplication

@MapperScan("com.xxx.xxx.*.mapper") // 确保与要扫描的mapper包路径一致

public class AdminWebApplication {

    public static void main(String[] args) {

        SpringApplication.run(AdminWebApplication.class, args);

    }

}

springboot和mybatisplus的版本匹配与整合问题:Invalid bean definition with name ‘xxxMapper‘ defined in flle解决方法
(图片来源网络,侵删)
2. 验证 Lombok 配置
  • IDE 设置:
    • IntelliJ:File > Settings > Build > Compiler > Annotation Processors →  勾选启用
    • Eclipse:安装 Lombok 插件后重启
    • 检查编译后的类:

      在 target/classes 目录下确认 XXXMapper.class 是否包含预期的注解和方法。

      如果检查发现target/classes 目录下没有生成对应的注解和方法,重新引入lombok的依赖,尽量引用较新的版本

      3. 检查 JDK 版本

      Spring Boot 3.x 至少需要 JDK 17+,验证环境变量:

      springboot和mybatisplus的版本匹配与整合问题:Invalid bean definition with name ‘xxxMapper‘ defined in flle解决方法
      (图片来源网络,侵删)

      java -version    # 输出应包含 "17" 或更高版本号

      如果不知道自己的jdk与哪个spring boot版本匹配,可以参考这位大佬文章,非常详细:

      springboot和mybatisplus的版本匹配与整合问题:Invalid bean definition with name ‘xxxMapper‘ defined in flle解决方法
      (图片来源网络,侵删)

      Springboot各版本与Java JDK的对应关系及JDK商用版本_springboot版本和jdk版本-CSDN博客

       4. 检查 MyBatis 依赖版本

      在 pom.xml 中验证依赖(Spring Boot 3.x 需使用 MyBatis Starter 3.x):

      如果不知道自己的springboot与mybatis的版本分别怎么匹配,可以参考这位大佬的博客,对版本问题非常详细:

      Spring Boot、MyBatis、MyBatis-Plus 依赖版本对应关系总结_mybatisplus与springboot版本对应关系-CSDN博客

      建议按照下面的方式引入(根据自己的springboot和mybatis进行修改):

      
          com.baomidou
          mybatis-plus-spring-boot3-starter
          3.5.7
      
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码