mybatis?foreach?屬性及其三種使用情況詳解
foreach 屬性介紹
foreach 用于迭代傳入過來的參數(shù)。
它的屬性介紹分別是
collection
:表示傳入過來的參數(shù)的數(shù)據(jù)類型。該參數(shù)為必選。要做 foreach 的對象,作為入?yún)r,List 對象默認(rèn)用 list 代替作為鍵,數(shù)組對象有 array 代替作為鍵,Map 對象沒有默認(rèn)的鍵。當(dāng)然在作為入?yún)r可以使用 @Param(“keyName”) 來設(shè)置鍵,設(shè)置 keyName 后,list,array 將會失效。 除了入?yún)⑦@種情況外,還有一種作為參數(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 的時候,collection 屬性值為 list
如果傳入的是單參數(shù)且參數(shù)類型是一個 array 數(shù)組的時候,collection 的屬性值為 array
如果傳入的參數(shù)是多個的時候,我們就需要把它們封裝成一個 Map 了,當(dāng)然單參數(shù)也可以封裝成 map。
item
:循環(huán)體中的具體對象。支持屬性的點路徑訪問,如 item.age,item.info.details。具體說明:在 list 和數(shù)組中是其中的對象,在 map 中是 value,該參數(shù)為必選。(它是每一個元素進(jìn)行迭代時的別名)index
:在 list 和數(shù)組中,index 是元素的序號;在 map 中,index 是元素的 key。open
:表示該語句以什么開始close
:表示該語句以什么結(jié)束separator
:表示在每次進(jìn)行迭代之間以什么符號作為分隔符
介紹完屬性之后,下面就進(jìn)入實踐。首先先來看一個簡單到爆炸的表(表名: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ù)名不一樣,在介紹屬性的時候已經(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)行迭代的對象。覺得有點懵的伙伴可以回到最開始介紹 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ù)時,一定要改用 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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
eclipse創(chuàng)建一個基于maven的web項目詳細(xì)步驟
開始學(xué)習(xí)maven,并用maven創(chuàng)建了第一個屬于自己的web項目,下面這篇文章主要給大家介紹了關(guān)于eclipse創(chuàng)建一個基于maven的web項目的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12SpringBoot中maven項目打成war包部署在linux服務(wù)器上的方法
這篇文章主要介紹了SpringBoot中maven項目打成war包部署在linux服務(wù)器上的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05spring boot @ResponseBody轉(zhuǎn)換JSON 時 Date 類型處理方法【兩種方法】
這篇文章主要介紹了spring boot @ResponseBody轉(zhuǎn)換JSON 時 Date 類型處理方法,主要給大家介紹Jackson和FastJson兩種方式,每一種方法給大家介紹的都非常詳細(xì),需要的朋友可以參考下2018-08-08基于Spring Web Jackson對RequestBody反序列化失敗的解決
這篇文章主要介紹了基于Spring Web Jackson對RequestBody反序列化失敗的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09