[Mybatis] 绑定声明无效 Invalidbound statement (not found)

本文介绍了在使用Mybatis框架时遇到的“绑定声明无效”错误,并提供了两种解决方案:一是调整XML文件的位置并修改Spring配置;二是通过Maven配置确保XML文件正确包含在构建过程中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

[Mybatis] 绑定声明无效 Invalidbound statement (not found)

 

这是页面报出来的错误:

HTTP Status 500 - Request processing failed; nested exception is
org.apache.ibatis.binding.BindingException:Invalid bound statement (not found): com.test.dao.UserMapper.getAllUser
   at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:189)
   at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:43)
   at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:58)
   at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:51)
   at com.sun.proxy.$Proxy13.add(Unknown Source)
   at com.coocaa.test.service.UserService.getUser(UserService.java:24)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
   at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

经过查找,Spring-mybatis.xml中的配置也没有问题

 

 







然后在target目录下面发现没有mybatis目录的。 


这样就有两种解决方法了 


1.
将mybatis的xml文件全部放在resource下面 
再修改spring配置文件里面的路径 


2.需要在pom加一个resources的资源加载列表

<build>

     <!--这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里。-->

     <resources>

         <resource>

       <!--   描述存放资源的目录,该路径相对POM路径-->

              <directory>src/main/java</directory>

              <includes>

                  <include>**/*.xml</include>

              </includes>

         </resource>

     </resources>

  </build>

 

这个时候,再次运行项目,就可以看到target目录下面存在mybatis的xml文件了 

这个错误只是我遇到的一种类型,可能解决不了左右的类型,敬请谅解

 

### 三级标题:问题原因分析 在使用 MyBatis-Plus 时遇到 `Invalid bound statement (not found)` 错误,通常是因为 MyBatis 无法找到对应的 SQL 映射语句。具体到 `xxxMapper.selectById` 这个方法,其本质是由于 XML 文件中没有定义名为 `selectById` 的 SQL 语句,或者接口与 XML 文件中的命名不一致 [^1]。 此外,该错误也可能发生在调用 `super.listByIds(userIdList)` 方法时,因为 `listByIds` 方法依赖于 `selectById` 来构建查询逻辑,如果找不到这个 SQL 映射,则会出异常 [^3]。 ### 三级标题:解决方法 #### 检查 Mapper 接口与 XML 文件的对应关系 确保接口声明方法名与 XML 文件中的 `<select>`、`<insert>`、`<update>` 或 `<delete>` 标签的 `id` 属性完全匹配。例如: ```java // Mapper 接口 User selectById(Long id); ``` XML 文件应包含如下内容: ```xml <select id="selectById" resultType="com.example.demo.entity.User"> SELECT * FROM user WHERE id = #{id} </select> ``` 若名称不一致或缺少该 SQL 定义MyBatis 将无法绑定正确的 SQL 语句 [^4]。 #### 确保 XML 文件被正确加载 检查 Spring Boot 配置文件(如 `application.yml` 或 `application.properties`)是否指定了正确的 mapper XML 路径。典型的配置如下: ```yaml mybatis-plus: mapper-locations: classpath*:mapper/**/*.xml ``` 如果实际路径结构不符合配置中的通配符规则(如文件夹嵌套层级不正确),则会导致 XML 文件未被正确加载 [^5]。 #### Maven 项目资源过滤配置 对于 Maven 项目,需要确保 `pom.xml` 中包含了对资源目录下 `.xml` 文件的处理规则。否则,编译过程中 XML 文件可能不会被复制到最终的 `target/classes` 目录中,导致运行时找不到 SQL 映射。添加如下配置以确保资源被正确处理: ```xml <build> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> ``` 此配置将确保 Java 包下的 XML 文件也被识别并打包进应用中 [^4]。 #### 使用注解方式替代 XML 如果你更倾向于使用注解而非 XML 文件定义 SQL 语句,可以直接在接口方法上添加相应的注解,例如: ```java @Select("SELECT * FROM user WHERE id = #{id}") User selectById(Long id); ``` 这种方式避免了 XML 文件路径和命名的问题,并且可以快速验证 SQL 是否生效 [^2]。 ### 三级标题:调试建议 1. **启用 MyBatis 日志输出** 在配置文件中开启 MyBatis 的日志功能,可以帮助定位具体的 SQL 加载情况: ```yaml logging: level: com.example.demo.mapper: debug ``` 2. **检查 Mapper 扫描路径** 确保 `@MapperScan` 注解指向了正确的包路径,以便 Spring Boot 可以扫描到所有的 Mapper 接口。 3. **IDE 缓存问题** 有时 IDE(如 IntelliJ IDEA)可能会缓存旧的配置或类文件,清理项目并重新构建可以排除此类干扰。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值