有時候開發中需要根據多個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]