在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后面的数据
。