天天看點

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值是否相同。

繼續閱讀