foreach 用於迭代傳入過來的參數。
它的屬性介紹分別是sql
介紹完屬性以後,下面就進入實踐。首先先來看一個簡單到爆炸的表(表名:t_test_foreach)數組
測試類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; }
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>
輸出結果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'}]
測試類.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; }
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>
輸出結果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'}]
測試類對象
// 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.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>
輸出結果
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'}]
這種狀況在傳參數時,必定要改用 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>
輸出結果
DEBUG - ==> Preparing: UPDATE t_test_foreach SET `name` = ? WHERE id IN ( ? , ? , ? ) DEBUG - ==> Parameters: updated(String), 1(Integer), 2(Integer), 4(Integer)
尊重他人勞動成果,轉載請註明出處:
http://blog.csdn.net/czd3355/article/details/75340080
參考文章:
http://blog.csdn.net/isea533/article/details/21237175
http://blog.csdn.net/u011029071/article/details/18504633