foreach的主要用在構建in條件中,它能夠在SQL語句中進行迭代一個集合。
foreach元素的屬性主要有 item,index,collection,open,separator,close。
item表示集合中每個元素進行迭代時的別名, 數組
index指 定一個名字,用於表示在迭代過程當中,每次迭代到的位置,mybatis
open表示該語句以什麼開始, app
separator表示在每次進行迭代之間以什麼符號做爲分隔 符,spa
collection表示要循環的集合,code
close表示以什麼結束。
在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,可是在不一樣狀況 下,該屬性的值是不同的,主要有一下3種狀況:
1. 若是傳入的是單參數且參數類型是一個List的時候,collection屬性值爲listorm
2. 若是傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值爲array對象
3. 若是傳入的參數是多個的時候,咱們就須要把它們封裝成一個Map了,固然單參數也可以封裝成map,實際上若是你在傳入參數的時候,在breast裏面也是會把它封裝成一個Map的,map的key就是參數名,因此這個時候collection屬性值就是傳入的List或array對象在本身封裝的map裏面的key blog
4.若是傳入的參數是實體類對象,該對象中有對應的要循環的集合或數組,則能夠直接將collection的值寫成該字段名稱。it
下面分別來看看上述四種狀況的示例代碼:io
1.單參數List的類型:
<select id="dynamicForeach" resultType="Services"> select * from t_service where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
若傳入的是List<String>類型 在Mapper文件中處理事後,SQL以下:(直接引用註解中的參數名字,這樣mybatis會忽略類型,按名字引入)
<select id="dynamicForeach" resultType="Services"> select * from t_services where id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection的值爲list,對應的Mapper是這樣的
public List dynamicForeachTest(List ids);
如果傳入的是的是List<String>類型的 可能會報出兩個參數的異常,這時咱們能夠用Mybatis官方的註解@Param
public List dynamicForeach(@Param("ids")List<String> ids);
2.單參數array數組的類型:
<select id="dynamicForeach2" resultType="Services"> select * from t_service where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection爲array,對應的Mapper代碼:
public List dynamicForeach2(int[] ids);
3.本身把參數封裝成Map的類型:
<select id="dynamicForeach3" resultType="Services"> select * from t_service where title like "%"#{title}"%" and id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection的值爲ids,是傳入的參數Map的key,對應的Mapper代碼:
public List dynamicForeach3(Map params);
4.傳入的參數是實體類對象,該對象中有對應的要循環的集合或數組。
public List<T> findList(T entity) { return dao.findList(entity); }
對應的mapper能夠這樣使用:
<select id="findList" resultType="Services"> SELECT <include refid="cmsServicesColumns"/> FROM cms_Services a <include refid="cmsServicesJoins"/> <where> <if test="category.id != null and category.id != ''"> AND (a.category_id = #{category.id}) </if> <if test="categoryList != null"> and a.category_id in <foreach item="category" collection="categoryList" open="(" separator="," close=")"> #{category.id} </foreach> </if> </where> </select>
collection對應的categoryList是實體類中的List:
僞代碼:
public class Services extends DataEntity<Services> { private List<Category> categoryList;// 分類編號(組) }
另外要注意的是:foreach循環出來的實體,是能夠當成實體在循環外面去調用的,即若是例4中代碼寫成以下這樣:
<select id="findList" resultType="Services"> SELECT <include refid="cmsServicesColumns"/> FROM cms_Services a <include refid="cmsServicesJoins"/> <where> <if test="categoryList != null"> and a.category_id in <foreach item="category" collection="categoryList" open="(" separator="," close=")"> #{category.id} </foreach> </if> <if test="category.id != null and category.id != ''"> AND (a.category_id = #{category.id}) </if> </where> </select>
則若是該實體沒有傳入category
依然會進入最後那一個循環中去,這點須要注意。