zoukankan      html  css  js  c++  java
  • Mybatis传递List集合

    完整错误如下: 
    org.apache.ibatis.binding.BindingException: Parameter ‘customerIdList’ not found. Available parameters are [collection, list]

    解释: 
    当我们传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。所以,当我们传递的是一个List集合时,mybatis会自动把我们的list集合包装成以list为Key值的map。

    DAO 层:
    Long selectCustomerCountList( List customerIdList);
    
    XML文件:
    <select id="selectCustomerCountList" parameterType="java.util.List" resultType="java.lang.Long">
            select count(1) from np_customer_info where id in
            <foreach item="item" collection="customerIdList" separator="," open="(" close=")" index="">  #{item, jdbcType=INTEGER}    
            </foreach> 
        </select>
    ==========================
    注意:DAO 层接口的参数名与XML 文件中的collection的属性值一致,是导致的问题的主要原因。
    

    解决方法 
    第一种:利用Mybatis给我们的封装进行XML配置,将我们的XML中collection属性值设置为list。

    DAO 层:
    Long selectCustomerCountList( List customerIdList);
    
    XML文件:
    <select id="selectCustomerCountList" parameterType="java.util.List" resultType="java.lang.Long">
            select count(1) from np_customer_info where id in
            <foreach item="item" collection="list" separator="," open="(" close=")" index="">  #{item, jdbcType=INTEGER}    
            </foreach> 
        </select>
    ======================
    注意:此时collection强制指定为list且不可改变
    

    第二种: 利用注解@Param指定我们的入参名称

    DAO层:
    Long selectCustomerCountList(@Param("customerIdList") List customerIdList);
    
    XML文件:
    <select id="selectCustomerCountList" parameterType="java.util.List" resultType="java.lang.Long">
            select count(1) from np_customer_info where id in
            <foreach item="item" collection="customerIdList" separator="," open="(" close=")" index="">  #{item, jdbcType=INTEGER}    
            </foreach> 
        </select>
    
    ======================
    注意: 此时的DAO层参数名可以 @Param("customerIdList") 与 collection的属性值一致
    

    第三种:将我们的List包装成Map参数进行传递

    在Service业务处理层次上面将参数进行包装
    public Long selectCustomerCountMap(List customerIdList) {   
            Map maps = new HashMap();
            maps.put("customerIds", customerIdList);
            return customerMapper.selectCustomerCountMap(maps);
        }
        DAO层:
        Long selectCustomerCountMap(Map maps);
    XML文件:
    
    <select id="selectCustomerCountMap" parameterType="java.util.Map" resultType="java.lang.Long">
            select count(1) from np_customer_info where id in
            <foreach item="item" collection="customerIds" separator="," open="(" close=")" index="">  #{item, jdbcType=INTEGER}    
            </foreach> 
        </select>
    ==============
    注意: 入参类型是java.util.Map而不再是List ,此时的collection属性值为Map中的Key值。
    

      

  • 相关阅读:
    Linux安装git报错 expected specifier-qualifier-list before ‘z_stream’
    Error: Cannot retrieve repository metadata (repomd.xml) for repository: FedoraPeople-sea. Please verify its path and try again
    Linux文件夹文件创建、删除
    confluence 常见问题处理
    git 删除本地和远程服务器分支
    yii DbCriteria相关属性常用方法
    git pull 撤销误操作
    如何在linux上按照行拆分大文件
    linux中rz的用法
    mac版本自带2.7.10版本的python情况下如何安装和使用python3.x
  • 原文地址:https://www.cnblogs.com/chenziyu/p/9455345.html
Copyright © 2011-2022 走看看