org.apache.ibatis.binding.BindingException原因总结

微信搜索:“二十同学” 公众号,欢迎关注一条不一样的成长之路

今天遇到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是找不到的

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
报错: ```org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.nowcoder.toutiao.dao.NewsDAO.selectByUserIdAndOffset at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:214) ~[mybatis-3.4.0.jar:3.4.0] at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48) ~[mybatis-3.4.0.jar:3.4.0] at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:59) ~[mybatis-3.4.0.jar:3.4.0] at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) ~[mybatis-3.4.0.jar:3.4.0] at com.sun.proxy.$Proxy60.selectByUserIdAndOffset(Unknown Source) ~[na:na] at com.nowcoder.toutiao.servicr.NewsService.getLatestNews(NewsService.java:16) ~[classes/:na] at com.nowcoder.toutiao.controller.HomeController.index(HomeController.java:30) ~[classes/:na] ``` 其中 NewsService.java package com.nowcoder.toutiao.servicr; import com.nowcoder.toutiao.dao.NewsDAO; import com.nowcoder.toutiao.model.News; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class NewsService { @Autowired private NewsDAO newsDAO; public List<News> getLatestNews(int userId,int offset,int limit){ return newsDAO.selectByUserIdAndOffset(userId,offset,limit); } } ``` ``` HomeController代码: package com.nowcoder.toutiao.controller; import com.nowcoder.toutiao.dao.NewsDAO; import com.nowcoder.toutiao.model.News; import com.nowcoder.toutiao.model.ViewObject; import com.nowcoder.toutiao.servicr.NewsService; import com.nowcoder.toutiao.servicr.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.jws.WebParam; import javax.servlet.http.HttpSession; import java.util.ArrayList; import java.util.List; @Controller public class HomeController{ @Autowired NewsService newsService; @Autowired UserService userService; @RequestMapping(path={"/","/index"},method={RequestMethod.GET,RequestMethod.POST}) public String index(Model model){ List<News> newsList=newsService.getLatestNews(0,0,10); List<ViewObject> vos=new ArrayList<>(); for (News news:newsList){ ViewObject vo=new ViewObject(); vo.set("news",news);//vo里面可以放任何东西 vo.set("user",userService.getUser(news.getUserId())); vos.add(vo); } model.addAttribute("vos",vos); return "home"; } } ``` ```
©️2020 CSDN 皮肤主题: 点我我会动 设计师:上身试试 返回首页