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]

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM