Mybatis传递List集合方式

 更新时间:2025年07月23日 15:46:43   作者:jushisi  
MyBatis传递List参数时,若XML变量名不匹配默认的"list"键,会报错,需通过@Param指定名称、使用collection属性或索引方式正确引用

第一种

参数是常规的List, 但是xml变量名不是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的属性值一致,是导致的问题的主要原因。

第二种

参数是常规的List, xml变量名是list------正常

  • 利用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指定入参List的名称------正常

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值。

第五种

把List 放入一个Bean对象中 ------报错

Model层(Bean层):
public class CompanyQueryModel {
    private String companyType;
    private List<String> customsCodeList; 
}

DAO层:
List<CompanyResultModel> selectCompanyInfo (CompanyQueryModel companyQueryModel);

XML层:
<select id="selectCompanyInfo" parameterType="com..dto.CompanyQueryModel" resultType="com.dto.CompanyResultModel">
    select * from np_company_info 
	<if test="customsCodeList != null and customsCodeList.size() > 0">
        and v.CUSTOMS_CODE in
        <foreach item="item" index="index" collection="customsCodeList" open="(" separator="," close=")">
            #{item}
        </foreach>
	</if>
</select>

第六种

把List 放入一个Bean对象中,利用@Param指定入参Bean名称,Xml取Bean.List------正常

Model层(Bean层):
public class CompanyQueryModel {
    private String companyType;
    private List<String> customsCodeList; 
}

DAO层:
List<CompanyResultModel> selectCompanyInfo(@Param("model") CompanyQueryModel companyQueryModel);

XML层:
<select id="selectCompanyInfo" parameterType="com..dto.CompanyQueryModel" resultType="com.dto.CompanyResultModel">
    select * from np_company_info 
	<if test="model.customsCodeList != null and model.customsCodeList.size() > 0">
        and v.CUSTOMS_CODE in
        <foreach item="item" index="index" collection="model.customsCodeList" open="(" separator="," close=")">
            #{item}
        </foreach>
    </if>
</select>

第七种

把List 放入一个Bean对象中, XML不用#{item} 改为 #{tagIds[${index}]}

  • 这中写法意思是,取这个数组中的每一个,因为字段是List。
Bean层:
public class AlarmConditionDTO {
    private List<String> orgIds;  
    private List<String> tagIds;   
    private String alertType;
}

DAO层:
List<Map<String,String>> selectDeviceCountByCondition(AlarmConditionDTO alarmConditionDTO);

XML层:
<select id="selectDeviceCountByCondition" resultType="java.util.Map">
    SELECT * from md_tag_target_relation_device 
    where 1=1
    <if test="tagIds != null and tagIds.size()>0">
        and tag_id IN
        <foreach collection="orgIds" index="index" open="(" close=")" separator="," item="item">
            #{tagIds[${index}],jdbcType=VARCHAR}
        </foreach>
    </if>
    <if test="orgIds != null and orgIds.size()>0">
        and d.region_code IN
        <foreach collection="orgIds" index="index" open="(" close=")" separator="," item="item">
            #{orgIds[${index}],jdbcType=VARCHAR}
        </foreach>
    </if>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java反射机制的适用场景及利弊详解

    Java反射机制的适用场景及利弊详解

    这篇文章主要介绍了Java反射机制的适用场景及利弊详解,Spring用到很多反射机制,在xml文件或者properties里面写好了配置,然后在Java类里面解析xml或properties里面的内容,得到一个字符串,然后用反射机制,需要的朋友可以参考下
    2023-08-08
  • Apache Log4j2 报核弹级漏洞快速修复方法

    Apache Log4j2 报核弹级漏洞快速修复方法

    Apache Log4j2 是一个基于Java的日志记录工具,是 Log4j 的升级,是目前最优秀的 Java日志框架之一,这篇文章主要介绍了突发Apache Log4j2 报核弹级漏洞快速修复方法,需要的朋友可以参考下
    2021-12-12
  • 在Spring Boot中MyBatis 的自动提交行为全解析(最新整理)

    在Spring Boot中MyBatis 的自动提交行为全解析(最新整理)

    在Spring Boot中,MyBatis的“自动提交”行为由Spring框架控制,而不是MyBatis本身,在无事务注解的情况下,Spring会为每个数据库操作开启并立即提交一个独立事务,接下来通过本文给大家介绍在Spring Boot中MyBatis的自动提交行为解析,感兴趣的朋友跟随小编一起看看吧
    2026-01-01
  • 浅谈Springboot整合RocketMQ使用心得

    浅谈Springboot整合RocketMQ使用心得

    本篇文章主要介绍了Springboot整合RocketMQ使用心得,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Mybatis查询语句返回对象和泛型集合的操作

    Mybatis查询语句返回对象和泛型集合的操作

    这篇文章主要介绍了Mybatis查询语句返回对象和泛型集合的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 使用Java实现简单的区块链程序的方法

    使用Java实现简单的区块链程序的方法

    这篇文章主要介绍了使用Java实现简单的区块链程序的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • java高并发下CopyOnWriteArrayList替代ArrayList

    java高并发下CopyOnWriteArrayList替代ArrayList

    这篇文章主要为大家介绍了java高并发下CopyOnWriteArrayList替代ArrayList的使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • java 排序算法之希尔算法

    java 排序算法之希尔算法

    这篇文章主要介绍了java 排序算法之希尔排序,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08
  • Spring Boot中自定义注解结合AOP实现主备库切换问题

    Spring Boot中自定义注解结合AOP实现主备库切换问题

    这篇文章主要介绍了Spring Boot中自定义注解+AOP实现主备库切换的相关知识,本篇文章的场景是做调度中心和监控中心时的需求,后端使用TDDL实现分表分库,需要的朋友可以参考下
    2019-08-08
  • Java线程等待唤醒几种方法小结

    Java线程等待唤醒几种方法小结

    线程等待和唤醒有三种实现方法,分别是Object类中的wait、notify,Condition类中的await、signal,LockSupport类中的park、unpark方法,感兴趣的可以了解一下
    2023-10-10

最新评论