mybatis接收参数为对象,其中包含List<string>处理

实体类代码:

public class AlarmConditionDTO {
    private List<String> orgIds;  
    private List<String> tagIds;   
    private String alertType;
    private Long startTime;
    private Long endTime;
    private int pageNum;
    private int pageSize;
    private String deviceName;
    private String searchConditionTime;
}

mapper,dao层:

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

错误写法上图解释:
在xml中想遍历list<String>这个值,写成
<foreach collection="orgIds" open="(" close=")" separator="," item="item">
#{orgIds}
</foreach>
这样会报错,list类型参数无法正确解析。
解决办法有两种:
第一:

<select id="selectDeviceCountByCondition" resultType="java.util.Map">
        SELECT
            device_type deviceType,count(device_type) count
        FROM
            (select DISTINCT obj_id obj_id from md_tag_target_relation_device where 1=1
        <if test="tagIds != null and tagIds.size()>0">
            and tag_id IN
            <foreach collection="tagIds" index="index" open="(" close=")" separator="," item="item">
                #{tagIds[${index}],jdbcType=VARCHAR}
            </foreach>
        </if>
        ) td
        LEFT JOIN
            md_resource_device d ON td.obj_id = d.uuid
        where 1=1 and device_name is not null
        <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>
        group by
            d.device_type
    </select>

这中写法意思是,取这个数组中的每一个,因为字段是List<String>。
第二种:
修改mapper接口方法的参数,增加注解 @Param("aa") AlarmConditionDTO alarmConditionDTO
param中名字随便起,这里起aa,xml中写:

<if test="aa.orgIds!= null and aa.orgIds.size()>0">
            and d.region_code IN
            <foreach collection="aa.orgIds" index="index" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>



转载作者:楼兰King
转载链接:https://www.jianshu.com/p/4b9fbc047d56

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值