mybatis?foreach?屬性及其三種使用情況詳解
foreach 屬性介紹
foreach 用于迭代傳入過來的參數(shù)。
它的屬性介紹分別是
collection:表示傳入過來的參數(shù)的數(shù)據(jù)類型。該參數(shù)為必選。要做 foreach 的對象,作為入?yún)r(shí),List 對象默認(rèn)用 list 代替作為鍵,數(shù)組對象有 array 代替作為鍵,Map 對象沒有默認(rèn)的鍵。當(dāng)然在作為入?yún)r(shí)可以使用 @Param(“keyName”) 來設(shè)置鍵,設(shè)置 keyName 后,list,array 將會失效。 除了入?yún)⑦@種情況外,還有一種作為參數(shù)對象的某個字段的時(shí)候。舉個例子:如果 User 有屬性 List ids。入?yún)⑹?User 對象,那么這個 collection = “ids” 如果 User 有屬性 Ids ids;其中 Ids 是個對象,Ids 有個屬性 List id;入?yún)⑹?User 對象,那么 collection = “ids.id”
如果傳入的是單參數(shù)且參數(shù)類型是一個 List 的時(shí)候,collection 屬性值為 list
如果傳入的是單參數(shù)且參數(shù)類型是一個 array 數(shù)組的時(shí)候,collection 的屬性值為 array
如果傳入的參數(shù)是多個的時(shí)候,我們就需要把它們封裝成一個 Map 了,當(dāng)然單參數(shù)也可以封裝成 map。
item:循環(huán)體中的具體對象。支持屬性的點(diǎn)路徑訪問,如 item.age,item.info.details。具體說明:在 list 和數(shù)組中是其中的對象,在 map 中是 value,該參數(shù)為必選。(它是每一個元素進(jìn)行迭代時(shí)的別名)index:在 list 和數(shù)組中,index 是元素的序號;在 map 中,index 是元素的 key。open:表示該語句以什么開始close:表示該語句以什么結(jié)束separator:表示在每次進(jìn)行迭代之間以什么符號作為分隔符
介紹完屬性之后,下面就進(jìn)入實(shí)踐。首先先來看一個簡單到爆炸的表(表名:t_test_foreach)

單參數(shù)是 array 類型
測試類
// ids = {1,2,3}
public List<User> testFindByArray(int[] ids) throws Exception {
? ? SqlSession sqlSession = getSession().openSession();
? ? userList = sqlSession.selectList(NameSpace + ".findByArray", ids);
? ? System.out.println(userList.toString());
? ? sqlSession.close();
? ? return userList;
}mapper.xml
<!--這里的 item 值可以和傳遞過來的參數(shù)名不一樣,在介紹屬性的時(shí)候已經(jīng)說過這是一個別名了。比如可以修改成如下代碼:
? ? <foreach collection="array" item="id" index="index" open="(" close=")" separator=",">
? ? ? ? #{id} ? <!--這里要和 item 值保持一致-->
? ? </foreach>
-->
<select id="findByArray" resultType="com.test.foreach.User">
? ? SELECT id,`name` FROM t_test_foreach WHERE id IN
? ? <foreach collection="array" item="ids" index="index" open="(" close=")" separator=",">
? ? ? ? #{ids}
? ? </foreach>
</select>輸出結(jié)果
DEBUG - ==> Preparing: SELECT id,`name` FROM t_test_foreach WHERE id IN ( ? , ? , ? )
DEBUG - ==> Parameters: 1(Integer), 2(Integer), 3(Integer)
DEBUG - <== Total: 3
[User{name='n1', id='1'}, User{name='n2', id='2'}, User{name='n3', id='3'}]
單參數(shù)是 List 類型
測試類
// List 元素有 1,3,5
public List<User> testFindByList(List<Integer> ids) throws Exception {
? ? SqlSession sqlSession = getSession().openSession();
? ? userList = sqlSession.selectList(NameSpace + ".findByList", ids);
? ? System.out.println(userList.toString());
? ? sqlSession.close();
? ? return userList;
}mapper.xml
<select id="findByList" resultType="com.test.foreach.User">
? ? SELECT id,`name` FROM t_test_foreach WHERE id IN
? ? <foreach collection="list" item="ids" index="index" open="(" close=")" separator=",">
? ? ? ? #{ids}
? ? </foreach>
</select>輸出結(jié)果
DEBUG - ==> Preparing: SELECT id,`name` FROM t_test_foreach WHERE id IN ( ? , ? , ? )
DEBUG - ==> Parameters: 1(Integer), 3(Integer), 5(Integer)
DEBUG - <== Total: 3
[User{name='n1', id='1'}, User{name='n3', id='3'}, User{name='n5', id='5'}]
單參數(shù)是 Map 類型
測試類
// Map<String, Object> 中的元素有 int[] ids = {2, 4};map.put("ids", ids);
public List<User> testFindByMap(Map map) throws Exception {
? ? SqlSession sqlSession = getSession().openSession();
? ? System.out.println(map.toString());
? ? List<Object> objects = sqlSession.selectList(NameSpace + ".findByMap", map);
? ? System.out.println(objects.toString());
? ? sqlSession.close();
? ? return userList;
}mapper.xml
<!--注意 collection 值是 ids,即要進(jìn)行迭代的對象。覺得有點(diǎn)懵的伙伴可以回到最開始介紹 collection 屬性那里看看,不要急-->
<select id="findByMap" resultType="com.test.foreach.User">
? ? SELECT id,`name` FROM t_test_foreach WHERE id IN
? ? <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
? ? ? ? #{id}
? ? </foreach>
</select>輸出結(jié)果
DEBUG - ==> Preparing: SELECT id,`name` FROM t_test_foreach WHERE id IN ( ? , ? )
DEBUG - ==> Parameters: 2(Integer), 4(Integer)
DEBUG - <== Total: 2
[User{name='n2', id='2'}, User{name='n4', id='4'}]
多參數(shù)
這種情況在傳參數(shù)時(shí),一定要改用 Map 方式
測試類
public void testUpdateByParams(int[] ids,String name) throws Exception {
? ? SqlSession sqlSession = getSession().openSession();
? ? Map<String,Object> map = new HashMap<String, Object>();
? ? map.put("ids",ids); // ids = {1,2,4}
? ? map.put("name",name);// name = "updated"
? ? sqlSession.selectList(NameSpace + ".findByParams", map);
? ? sqlSession.close();
}mapper.xml
<select id="findByParams">
? ? UPDATE t_test_foreach SET `name` = '#{name}' WHERE id IN
? ? <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
? ? ? ? #{item}
? ? </foreach>
</select>輸出結(jié)果
DEBUG - ==> Preparing: UPDATE t_test_foreach SET `name` = ? WHERE id IN ( ? , ? , ? )
DEBUG - ==> Parameters: updated(String), 1(Integer), 2(Integer), 4(Integer)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot基于maven打包分離lib及resource
這篇文章主要介紹了Springboot基于maven打包分離lib及resource,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
try catch finally的執(zhí)行順序深入分析
首先執(zhí)行try,如果有異常執(zhí)行catch,無論如何都會執(zhí)行finally,當(dāng)有return以后,函數(shù)就會把這個數(shù)據(jù)存儲在某個位置,然后告訴主函數(shù),我不執(zhí)行了,接下來你執(zhí)行吧,所以函數(shù)就會推出2013-09-09
Maven依賴中scope的runtime和provied的區(qū)別及說明
這篇文章主要介紹了Maven依賴中scope的runtime和provied的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Springcloud基于OpenFeign實(shí)現(xiàn)服務(wù)調(diào)用代碼實(shí)例
這篇文章主要介紹了Springcloud基于OpenFeign實(shí)現(xiàn)服務(wù)調(diào)用代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
SpringCloud?OpenFeign?服務(wù)調(diào)用傳遞?token的場景分析
這篇文章主要介紹了SpringCloud?OpenFeign?服務(wù)調(diào)用傳遞?token的場景分析,本篇文章簡單介紹?OpenFeign?調(diào)用傳遞?header?,以及多線程環(huán)境下可能會出現(xiàn)的問題,其中涉及到?ThreadLocal?的相關(guān)知識,需要的朋友可以參考下2022-07-07
mybatis中Oracle參數(shù)為NULL錯誤問題及解決
這篇文章主要介紹了mybatis中Oracle參數(shù)為NULL錯誤問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

