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

前端传递字段
accountType: a,b,c
buType: d,e
mapper:
List<DetailVO> findList(Page<DetailVO> page, @Param("q") Map<String, Object> params);

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

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

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

你可能感兴趣的:(sql)