zoukankan      html  css  js  c++  java
  • 初学者手册-MyBatis踩坑记(org.apache.ibatis.binding.BindingException)

    1、参数绑定失败

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'msgs3' not found. Available 
    parameters are [msgs, param1]

     相关信息

    <insert id="insertBatch">
        INSERT INTO t_user
                (id, name, del_flag)
        VALUES
        <foreach collection ="msgs3" item="user" separator =",">
             (#{user.id}, #{user.name}, #{user.delFlag})
        </foreach >
    </insert>
    //Mapper类         
    public interface UserMapper {         
      public void insertBatch (List<User> users);
    }

    分析思路

    经过测试发现错误是从map.xml文件报出来的,也就是说,系统是可以读到xml文件的,但是通过xml文件读取对应的参数msgs3时报错。即,问题出在map.java上面。但是,检查了命名,并没有相关问题。

    解决方案

    既然是绑定的问题,那么问题肯定不是在xml文件上,就是在对应的map的java方法上。所以,有两种解题方法。

    值得注意的是:指定了传参名称以后,默认值就会失效

    1、修改xml文件

    通过测试发现,foreach 中的collection貌似默认值为 list,当不指定传参的名称时,可以直接使用。

    <insert id="insertBatch">
        INSERT INTO t_user
                (id, name, del_flag)
        VALUES
        <foreach collection ="list" item="user" separator =",">
             (#{user.id}, #{user.name}, #{user.delFlag})
        </foreach >
    </insert>

    2、修改Mapxx.java中的方法参数

    //Mapper类         
    public interface UserMapper {         
      public void insertBatch (@Param("msgs") List<User> users);
    }
  • 相关阅读:
    Redis-其他命令
    Redis-发布与订阅
    C#使用命令编译代码
    Redis有序集合操作
    Redis散列操作
    设置ul水平居中
    Redis集合操作
    Redis列表操作
    java连SQLServer失败 java.lang.ClassNotFoundException:以及 javax.xml.bind.JAXBException
    SQLServer 用法简例
  • 原文地址:https://www.cnblogs.com/liuyp-ken/p/9585896.html
Copyright © 2011-2022 走看看