foreach的主要用在構建in條件中,它可以在SQL語句中進行迭代一個集合。
foreach元素的屬性主要有 item,index,collection,open,separator,close。
item表示集合中每一個元素進行迭代時的別名,
index指 定一個名字,用於表示在迭代過程中,每次迭代到的位置,
open表示該語句以什么開始,
separator表示在每次進行迭代之間以什么符號作為分隔 符,
collection表示要循環的集合,
close表示以什么結束。
在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況 下,該屬性的值是不一樣的,主要有一下3種情況:
1. 如果傳入的是單參數且參數類型是一個List的時候,collection屬性值為list
2. 如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值為array
3. 如果傳入的參數是多個的時候,我們就需要把它們封裝成一個Map了,當然單參數也可以封裝成map,實際上如果你在傳入參數的時候,在breast里面也是會把它封裝成一個Map的,map的key就是參數名,所以這個時候collection屬性值就是傳入的List或array對象在自己封裝的map里面的key
4.如果傳入的參數是實體類對象,該對象中有對應的要循環的集合或數組,則可以直接將collection的值寫成該字段名稱。
下面分別來看看上述四種情況的示例代碼:
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
依然會進入最后那一個循環中去,這點需要注意。