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]
相關文章
相關標籤/搜索