MyBatis foreach标签遍历数组

有时候开发中需要根据多个ID去查询,可以将ID封装为List或者数组然后使用MyBatis中的foreach标签构建in条件。

这里我将ID封装为String[]作为参数。

<select id="selectList" parameterType="java.util.List" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM t_user
        WHERE id IN
        <foreach collection="array" index="index" item="item"
            open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

这里需要注意的是collection必须为array,否则会报错如下:

Caused by: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [array]
原文地址:https://www.cnblogs.com/lyxy/p/6253133.html