mybatis foreach 屬性及其三種使用狀況

foreach 屬性介紹

foreach 用於迭代傳入過來的參數。 
它的屬性介紹分別是sql

  • collection:表示傳入過來的參數的數據類型。該參數爲必選。要作 foreach 的對象,做爲入參時,List 對象默認用 list 代替做爲鍵,數組對象有 array 代替做爲鍵,Map 對象沒有默認的鍵。固然在做爲入參時可使用 @Param(「keyName」) 來設置鍵,設置 keyName 後,list,array 將會失效。 除了入參這種狀況外,還有一種做爲參數對象的某個字段的時候。舉個例子: 
    若是 User 有屬性 List ids。入參是 User 對象,那麼這個 collection = 「ids」 若是 User 有屬性 Ids ids;其中 Ids 是個對象,Ids 有個屬性 List id;入參是 User 對象,那麼 collection = 「ids.id」 
    • 若是傳入的是單參數且參數類型是一個 List 的時候,collection 屬性值爲 list
    • 若是傳入的是單參數且參數類型是一個 array 數組的時候,collection 的屬性值爲 array
    • 若是傳入的參數是多個的時候,咱們就須要把它們封裝成一個 Map 了,固然單參數也能夠封裝成 map。
  • item: 循環體中的具體對象。支持屬性的點路徑訪問,如 item.age,item.info.details。具體說明:在 list 和數組中是其中的對象,在 map 中是 value,該參數爲必選。(它是每個元素進行迭代時的別名)
  • index:在 list 和數組中,index 是元素的序號;在 map 中,index 是元素的 key。
  • open:表示該語句以什麼開始
  • close:表示該語句以什麼結束
  • separator:表示在每次進行迭代之間以什麼符號做爲分隔符

介紹完屬性以後,下面就進入實踐。首先先來看一個簡單到爆炸的表(表名:t_test_foreach)數組

這裏寫圖片描述

單參數是 array 類型

測試類app

// 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

mapper.xml測試

<!--這裏的 item 值能夠和傳遞過來的參數名不同,在介紹屬性的時候已經說過這是一個別名了。好比能夠修改爲以下代碼:
    <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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

輸出結果spa

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'}]
  • 1
  • 2
  • 3
  • 4

單參數是 List 類型

測試類.net

// 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

mapper.xmlcode

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

輸出結果xml

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'}]
  • 1
  • 2
  • 3
  • 4

單參數是 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

mapper.xmlblog

<!--注意 collection 值是 ids,即要進行迭代的對象。以爲有點懵的夥伴能夠回到最開始介紹 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

輸出結果

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'}]
  • 1
  • 2
  • 3
  • 4

多參數

這種狀況在傳參數時,必定要改用 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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

輸出結果

DEBUG - ==>  Preparing: UPDATE t_test_foreach SET `name` = ? WHERE id IN ( ? , ? , ? ) 
DEBUG - ==> Parameters: updated(String), 1(Integer), 2(Integer), 4(Integer)
  • 1
  • 2

這裏寫圖片描述

尊重他人勞動成果,轉載請註明出處: 
http://blog.csdn.net/czd3355/article/details/75340080

參考文章: 
http://blog.csdn.net/isea533/article/details/21237175 
http://blog.csdn.net/u011029071/article/details/18504633

相關文章
相關標籤/搜索