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>

总结

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

相关文章

  • maven项目在实践中的构建管理之路的方法

    maven项目在实践中的构建管理之路的方法

    这篇文章主要介绍了maven项目在实践中的构建管理之路的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05
  • java实现发牌小程序

    java实现发牌小程序

    这篇文章主要为大家详细介绍了java实现发牌小程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • Java将文件压缩成zip并下载

    Java将文件压缩成zip并下载

    这篇文章主要为大家详细介绍了Java如何将文件压缩成zip和下载,并处理base64和url,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下
    2025-04-04
  • 在Spring Boot使用Undertow服务的方法

    在Spring Boot使用Undertow服务的方法

    Undertow是RedHAT红帽公司开源的产品,采用JAVA开发,是一款灵活,高性能的web服务器,提供了NIO的阻塞/非阻塞API,也是Wildfly的默认Web容器,这篇文章给大家介绍了在Spring Boot使用Undertow服务的方法,感兴趣的朋友跟随小编一起看看吧
    2023-05-05
  • springboot上传文件,url直接访问资源问题

    springboot上传文件,url直接访问资源问题

    这篇文章主要介绍了springboot上传文件,url直接访问资源问题。具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • 解决Maven parent.relativePath带给我的坑

    解决Maven parent.relativePath带给我的坑

    在Linux环境下使用Maven进行项目打包时,可能会遇到“当前目录没有pom文件”的错误,需要确认在包含pom.xml文件的项目目录下执行Maven命令,另外,如果遇到“parent.relativePath points at wrong local POM”错误,可能是父模块依赖问题
    2024-09-09
  • Mybatisplus集成springboot完成分页查询功能(示例代码)

    Mybatisplus集成springboot完成分页查询功能(示例代码)

    今天小编给大家分享Mybatisplus集成springboot完成分页查询功能,本文通过实例代码给大家介绍的非常详细,需要的朋友参考下吧
    2023-11-11
  • Java编程一道多线程问题实例代码

    Java编程一道多线程问题实例代码

    这篇文章主要介绍了Java编程一道多线程问题实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • 详解Spring Boot整合Mybatis实现 Druid多数据源配置

    详解Spring Boot整合Mybatis实现 Druid多数据源配置

    本篇文章主要介绍了详解Spring Boot整合Mybatis实现 Druid多数据源配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • SpringBoot中的server.context-path的实现

    SpringBoot中的server.context-path的实现

    本文主要介绍了SpringBoot中的server.context-path的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-08-08

最新评论