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]
发布了267 篇原创文章 · 获赞 75 · 访问量 48万+

猜你喜欢

转载自blog.csdn.net/spt_dream/article/details/97800696