Mybatis传递List集合

本文详细解析了MyBatis处理List参数的各种场景,包括直接使用List、通过注解指定参数名、将List包装进Map或Bean对象等,并提供了具体的DAO层与XML配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种: 参数是常规的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>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值