java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误

解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题

作者:嘿;-)翔

本文主要讲述了在使用MyBatis和MyBatis-Plus时遇到的绑定异常问题,通过排查和总结,作者发现使用MyBatis-Plus Boot Starter可以解决这个问题,文章详细对比了MyBatis-Plus Boot Starter和MyBatis Spring Boot Starter的功能和使用场景

mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误

不是知道你是否 出现过这样的错误

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 

经过各种度娘,无非就是让你检查三种情况

如果 程序中这三种情况都没有出现 ,那么,再看看 我遇到的问题。

首先我那个是个单元测试 这个时候我的数据是能出来的,但是。。。。

细心的同学可能发现了 如果字段没有加 as做别名映射,实体类字段是不能被赋值的。

于是,是不是实体类上加上@TableName @TableField 就好了,于是我加上了。。。

此时我引用的依赖是

<dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.4</version>
            </dependency>

但是 @TableName @TableField 的包是com.baomidou.mybatisplus.annotation里边的 所以要更改引用依赖

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.5.1</version>
            </dependency>

一切准备就绪。。。😏启动。。。

报错。。。。😳

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.cdliker.infrastructure.persistent.dao.IAwardDao.getAwardPOList 

于是 排查了三种情况,都不符合。所以目标就锁定了 依赖上,

mybatis-plus-boot-starte与mybatis-spring-boot-starter的区别

1. 功能与特性

mybatis-plus-boot-starter:

mybatis-spring-boot-starter:

2. 使用场景

3. 依赖关系

值得注意的是,mybatis-plus-boot-starter在其内部已经包含了mybatis-spring-boot-starter的依赖(或者至少包含了与MyBatis集成所需的关键依赖),因此在大多数情况下,如果你选择了mybatis-plus-boot-starter,就无需再额外引入mybatis-spring-boot-starter。这也是为什么在一些项目中,同时引入这两个starter时,移除其中一个并不会对项目运行造成影响的原因。

综上所述,mybatis-plus-boot-starter和mybatis-spring-boot-starter在功能和特性上有所区别,选择哪个启动器主要取决于你的项目需求和对数据库操作的具体要求。如果你需要更多的高级特性和扩展性,可以选择mybatis-plus-boot-starter;如果你只是需要基本的MyBatis集成功能,那么mybatis-spring-boot-starter就足够了。

正常的三种情况 出发点都是扫描的问题,难道问题还在扫描?既然mybatis-plus包含mybatis 那么。。。

mybatis-plus-boot-starter 时遇到 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 这样的错误,而切换到 mybatis-spring-boot-starter 后错误消失,这通常意味着 MyBatis 或 MyBatis-Plus 在寻找或解析 Mapper 接口与其对应的 XML 映射文件时出现了问题。可能会出现以下情况

1.Mapper 接口与 XML 映射文件的位置或命名不匹配:

确保 Mapper 接口和 XML 映射文件位于正确的包路径下,并且文件名与接口名匹配(去掉接口名中的 I 前缀,并加上 .xml 后缀)。

检查 XML 映射文件的 namespace 是否与 Mapper 接口的全限定名完全一致。

2.Mapper 扫描路径未正确配置:

在 Spring Boot 配置中,确保你已经正确配置了 Mapper 扫描路径。这可以通过 @MapperScan 注解或在 application.properties/application.yml 文件中设置 mybatis-plus.mapper-locations 来实现。

如果你的 Mapper 接口和 XML 文件位于不同的包路径下,你需要确保 @MapperScan 注解或配置属性包含了所有这些路径。

XML 映射文件未正确放置在资源目录中:

确保 XML 映射文件被放置在 Spring Boot 项目的资源目录(如 src/main/resources)中,并且其路径与 Mapper 接口的路径相对应。

3.依赖冲突:

检查你的项目依赖,确保没有引入多个版本的 MyBatis 或 MyBatis-Plus,这可能会导致类加载器加载了错误的类。

如果你的项目中同时包含了 mybatis-spring-boot-starter 和 mybatis-plus-boot-starter,确保只保留一个,因为它们是互斥的。

Spring Boot 的自动配置问题:

有时候,Spring Boot 的自动配置可能不会按预期工作。尝试在 application.properties/application.yml 中显式配置 MyBatis-Plus 的相关属性,或者在你的配置类中手动配置 SqlSessionFactory 和 MapperScannerConfigurer。

4.检查 MyBatis-Plus 特有的配置:

如果你在使用 MyBatis-Plus 特有的功能(如分页插件、自动填充等),请确保你已经按照 MyBatis-Plus 的要求进行了配置。

5.IDE 或构建工具的问题:

有时候,IDE(如 IntelliJ IDEA 或 Eclipse)或构建工具(如 Maven 或 Gradle)可能没有正确更新或同步你的项目配置。

尝试重启 IDE、重新构建项目或清理并重新导入 Maven/Gradle 依赖。

6.查看日志和错误堆栈:

仔细查看错误堆栈和 Spring Boot 的启动日志,可能会有更多关于为什么 MyBatis 找不到绑定语句的线索。

配置,配置 application.properties/application.yml 文件中设置 mybatis-plus.mapper-locations 来实现

在使用 mybatis-spring-boot-starter 的时候 使用

mybatis:
  mapper-locations: classpath:/mybatis/mapper/*.xml
  config-location:  classpath:/mybatis/config/mybatis-config.xml

在使用 mybatis-plus-boot-starter 的时候 使用

mybatis-plus:
  mapper-locations: classpath:/mybatis/mapper/*.xml
  config-location:  classpath:/mybatis/config/mybatis-config.xml

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文