Mybatis中的foreach用法

在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後面的數據

我是膜拜大佬的程序員!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章