MyBatis 中foreach的三种遍历

作者:袖梨 2026-07-08
各参数解释
collection :collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array
item : 表示在迭代过程中每一个元素的别名
index :表示在迭代过程中每次迭代到的位置(下标)
open :前缀
close :后缀
separator :分隔符,表示迭代时每个元素之间以什么分隔数组

int[] ids = {1,2,3,4,5}<select id="getTeam" parameterType="java.util.arraylist" resultType="Team"> <foreach collection="array" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></select>

3 .Map

mybatis 中foreach的三种遍历

<select id="getTeam" parameterType="java.util.HashMap" resultType="Team">select * from team where title like "%"#{name}"%" and id in <foreach collection="keys" index="index" item="item" open="(" separator="," close=")">#{item} </foreach> </select>

4 .List

<select id="getTeam" parameterType="java.util.List" resultType="Team"> select * from team where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item}</foreach></select>

相关文章

精彩推荐