[mybatis] Determine whether the subscript of the array parameter is the last one in mybatis

When traversing the array parameters and splicing sql, the last parameter does not require certain keywords, so it is necessary to determine whether it is the end of the loop

signal

<select id="count" parameterType="list" resultType="java.lang.Integer">
 <if test="list!=null and list.size()>0">
        <foreach item="item" collection="list" index="index">
            SELECT
                count(1)
            FROM
                t_user
            WHERE
                create_at &gt;= #{item.startTime}
            AND
                create_at &lt; #{item.endTime}
            AND
                <if test="index != (list.size() - 1)">
                    union all
                </if>
        </foreach>
    </if>
</select>

The index is the subscript of the loop, list.size() - 1 is the meaning of the last subscript of the array, it is feasible to test

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325747272&siteId=291194637