1. 程式人生 > >mybatis foreach遍歷陣列

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]