foreach标签主要用于构建in条件,可在sql中对集合进行迭代。也常用到批量删除、添加等操作中。
- item表示集合中每一个元素进行迭代时的别名,
- index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
- open表示该语句以什么开始,
- separator表示在每次进行迭代之间以什么符号作为分隔符,
- close表示以什么结束
<update id="updateBatch">
<foreach item="item" index="index" collection="list" open=""
close="" separator=";">
<if test="item.statusType.toString() == 'DELETED'">
DELETE FROM 库名.表名 WHERE id = #{item.id}
</if>
<if test="item.statusType.toString() != 'DELETED'">
UPDATE 库名.表名
<set>
modifier = #{item.modifier,jdbcType=CHAR},
<if test="item.account != null">
account = #{item.account,jdbcType=VARCHAR},
</if>
<if test="item.name != null">
name = #{item.name,jdbcType=VARCHAR},
</if>
</set>
where
库名.表名.id =#{item.id}
</if>
</foreach>
</update>