Mybatis: Mybatis foreach逗号分隔字符串 遍历方式

时间:2025-02-18 08:34:35
前端传递字段
accountType: a,b,c
buType: d,e
mapper:
List<DetailVO> findList(Page<DetailVO> page, @Param("q") Map<String, Object> params);

通过myBatis自带功能foreach,直接把逗号分隔的字符串传到即可,后台不用过多操作,拼接部分sql如下:

<if test=" != null and  !='' ">
            and bu.bu_type in
            <foreach item="item" index="index" collection="(',')" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test=" !=null and  !='' ">
            and coa.mgmt_control_category in
            <foreach item="item" index="index" collection="(',')" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>

.split(’,’)进行切割,注意是英文输入状态的单引号.