在MySQL中我們要實現in
的篩選查詢,我們一般直接通過
select *
from user
where id in (2,3)
這樣直接通過 in
操作就可以進行篩選,但是在Mybatis中卻無法直接使用 in
,這就需要使用foreach
方法來實現MySQL中的 in
篩選。
樣式如下:
select *
from user
where
<if test="ids != null and ids.size > 0">
id in
<foreach collection="user.ids" item="user.ids" index="index" open="(" close=")" separator=",">
#{user.ids}
</foreach>
</if>
通過使用 foreach 就可以實現MySQL中的 in
,因爲我們在Mybatis中無法直接傳入一個String
類型拼接的數據給in後面的數據
。