今天遇到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是找不到的
Dao层代码: