MyBatis foreach标签遍历数组

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

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

<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]
相关文章
相关标签/搜索