springboot 中 Invalid bound statement (not found):错误

本文解析了在使用MyBatis框架时遇到的binding exception错误,详细检查了EmployeeMapper接口和XML映射文件中方法ID的一致性问题,并提供了正确的配置示例。

 

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.zpjeck.mapper.EmployeeMapper.insertEmp
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:227) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:49) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) ~[mybatis-3.4.6.jar:3.4.6]
	at com.sun.proxy.$Proxy61.insertEmp(Unknown Source) ~[na:na]
	at com.zpjeck.contraller.EmpContraller.insertEmp(EmpContraller.java:31) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
	at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45009) ~[na:1.8.0_121]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45012) ~[na:1.8.0_121]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:215) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:142) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]


遇到这种错误,首先先排查一下错误,EmployeeMapper.java文件中的 方法是不是与EmployeeMapper.xml 的操作数据的语句中的id保持一致??

//EmployeeMapper.java文件

package com.zpjeck.mapper;
import com.zpjeck.bean.Employee;
public interface EmployeeMapper {
    public Employee selectById(Integer id);
    public int insertEmp(Employee employee);

}
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.zpjeck.mapper.EmployeeMapper">
    <select id="selectById" resultType="com.zpjeck.bean.Employee">
        select * from employee where id=#{id}
    </select>

    <insert id="insertEmp">
        insert into employee(lastName,email,gender,d_id) values (#{lastName},#{email},#{gender},#{d_id})
    </insert>

</mapper>

 第二个容易出错的地方:

 写对正确的配置,就能运行。

### Spring Boot 中 'Invalid bound statement (not found)' 错误的解决方案 此错误通常发生在 MyBatis 或 MyBatis-Plus 配置不当的情况下,具体表现为无法找到 Mapper 接口或其对应的 XML 文件。以下是对此问题的深入解析和解决方法。 #### 1. 检查 `@MapperScan` 注解是否正确配置 如果项目中未正确扫描到 Mapper 接口所在的包,则可能导致该错误。需确保在启动类或其他配置类上添加了 `@MapperScan` 注解,并指定正确的包路径[^1]: ```java @SpringBootApplication @MapperScan("com.example.mapper") // 替换为实际的Mapper接口所在包路径 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 2. 确认 `mybatis.mapper-locations` 是否指向正确的 XML 路径 MyBatis 的映射文件(XML)需要被正确加载才能与 Mapper 接口绑定。可以通过设置 `mybatis.mapper-locations` 属性来指定 XML 文件的位置[^3]。例如,在 `application.yml` 中配置如下: ```yaml mybatis: mapper-locations: classpath:mapper/*.xml # 替换为实际的XML文件位置 ``` 注意:路径中的通配符应匹配项目的目录结构,否则可能遗漏某些 XML 文件。 #### 3. 核实 Mapper 接口名称与 XML 文件名的一致性 MyBatis 默认会根据 Mapper 接口全限定名寻找同名的 XML 文件。因此,确保 Mapper 接口的命名与其对应 XML 文件保持一致是非常重要的[^2]。例如,对于以下接口定义: ```java package com.example.mapper; import org.apache.ibatis.annotations.Mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; @Mapper public interface UserMapper extends BaseMapper<User> {} ``` 其对应的 XML 文件应当命名为 `UserMapper.xml` 并放置于上述配置所指的路径下。 #### 4. 检查依赖版本兼容性 不同版本的 Spring Boot 和 MyBatis/MyBatis-Plus 可能存在兼容性问题。建议查阅官方文档并调整至推荐组合版本。例如,当使用 Spring Boot 3.x 版本时,可搭配 MyBatis-Plus 3.5.x 使用。 #### 5. 排查 IDE 设置影响 部分开发工具(如 IntelliJ IDEA)可能存在大小写敏感度差异的情况,这可能会干扰资源定位逻辑[^4]。可通过清理缓存 (`File -> Invalidate Caches / Restart`) 来尝试解决问题;或者切换构建工具命令运行程序以排除环境因素干扰。 --- ### 示例代码片段 以下是一个完整的 Maven 项目示例配置,展示如何集成 MyBatis-Plus 并规避常见配置陷阱: ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.9</version> </dependency> <!-- MySQL Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> ``` 同时,确保数据库连接池等相关组件已正确定义。 --- ####
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值