天天看点

mybatis报错—— Invalid bound statement (not found): com.mapper.EmployeeMapper.updEmpmybatis报错—— Invalid bound statement (not found): com.mapper.EmployeeMapper.updEmp

mybatis报错—— Invalid bound statement (not found): com.mapper.EmployeeMapper.updEmp

异常如下:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.mapper.EmployeeMapper.updEmp
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:223)
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)
    at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:59)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
    at com.sun.proxy.$Proxy6.updagteEmp(Unknown Source)
    at com.test.Test_crud.test(Test_crud.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
................
................
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
           

异常原因:

很简单,与映射文件绑定的Mapper接口中声明的方法,在Mapper.xml中找不到对应的SQL语句标签。

代码如下:

Mapper接口中的代码:

public interface EmployeeMapper {
    public Employee getEmpById(Integer id);
    public Integer addEmp(Employee employee);
    public void updEmp(Employee employee);
    public void deleteEmpById(Employee employee);
    public void deleteEmpById(int i);
}
           

Mapper.xml中的代码:

<!-- Mapper接口中声明的方法
    public void updEmp(Employee employee); 
-->
<!-- 接口中声明的方法为updEmp,与sql标签的id属性值updateEmp不相同,于是报错 -->
<update id="updateEmp">
    UPDATE `mybatis`.`tbl_employee` 
    SET `last_name`=#{lastName}, `gender`=#{gender}, `email`=#{email} WHERE (`id`=#{id});   
</update>
           

原因总结:

接口中声明的方法为updEmp,与sql标签的id属性值updateEmp不相同,导致加载Mapper接口时,mybatis找不到接口中的updEmp()方法所对应的sql标签(通过id值来对应),于是报Invalid bound statement (not found)——即找不到绑定的声明。

所以,Mapper中声明的方法,在绑定的映射文件中必须能找到对应的sql标签,且方法名与对应的sql标签的id值必须相同。

异常解决:

检查Mapper中声明的方法,是否都能在绑定的映射文件中找到对应的sql标签。如果有,则检查方法名与对应sql标签的id值是否相同。

继续阅读