美文网首页
mybatis中判断foreach循环最后一个

mybatis中判断foreach循环最后一个

作者: 帮我的鸵鸟盖个章 | 来源:发表于2020-07-02 14:10 被阅读0次

mybatis使用foreach在遍历数组参数的时候,在最后一次不需要一些关键字,这个时候就需要对foreach的最后一次循环进行判断。

使用index进行判断。最后一次下标为数组长度-1

<!-- 物料包含多个 -->
<if test="nameList != null and nameList.size() > 0">
    and odod.id in (
    <foreach item="item" collection="nameList" index="index">
        select id from user where name = #{item}
        <if test="index != (nameList.size() - 1)">
            union all
        </if>
    </foreach>
    group by id
    )
</if>

相关文章

网友评论

      本文标题:mybatis中判断foreach循环最后一个

      本文链接:https://www.haomeiwen.com/subject/eiqrqktx.html