org.apache.ibatis.binding.BindingException原因總結
阿新 • • 發佈:2019-01-08
今天遇到mybatis的報錯,搞了好久才搞懂,在網上找了好久的相似案例,也沒有搞定,先來看下網上常見的解決辦法吧,相信也能解決大部分人的報錯。
排查方法如下:
1、mapper介面和mapper.xml是否在同一個包(package)下?名字是否一樣(僅字尾不同)?
2、mapper.xml的名稱空間(namespace)是否跟mapper介面的包名一致?
3、介面的方法名,與xml中的一條sql標籤的id一致
4、如果介面中的返回值List集合(不知道其他集合也是),那麼xml裡面的配置,儘量用resultMap(保證resultMap配置正確),不要用resultType
5、如果你的專案是maven專案,請你在編譯後,到介面所在目錄看一看,很有可能是沒有生產對應的xml檔案,因為maven預設是不編譯的,因此,你需要在你的pom.xml的<build></build>裡面,加這麼一段:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
以上方法都沒有解決我的問題,不多說,上程式碼~
Dao層程式碼:
@Repository
public interface CustomDialogDao {
void updateDialogByFrom2(@Param("param") CustomDialog param, @Param("fromUser") String fromUser, @Param("appid") String appid);
}
mapper.xml程式碼:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.benmu.mts.wx.center.service.dao.CustomDialogDao"> <update id="updateDialogByFrom2" parameterType="com.benmu.mts.wx.center.custom.bean.CustomDialog"> update custom_dialog <set> <if test="status != null"> status = #{status} </if> </set> where 1=1 and appid = #{appid} and fromUser = #{fromUser} </update> </mapper>
報錯資訊如下:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'status' not found. Available parameters are [param, fromUser, appid, param3, param1, param2]
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:76)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:399)
at com.sun.proxy.$Proxy27.update(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:269)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:55)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy28.updateDialogByFrom2(Unknown Source)
at com.benmu.mts.wx.center.dao.CustomDialogDaoTest.updateDialogByFrom2(CustomDialogDaoTest.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
解決辦法:
<update id="updateDialogByFrom2" >
update custom_dialog
<set>
<if test="param.status != null">
status = #{param.status}
</if>
</set>
where 1=1 and appid = #{appid} and from_user = #{fromUser}
</update>
Dao層已經把CustomDialog定義成了param,如果要使用status,就要呼叫param這個物件的屬性,否則status是找不到的