网站首页 > 技术文章 正文
foreach 属性介绍
foreach 用于迭代传入过来的参数。
它的属性介绍分别是
- 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 类型
测试类
// 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>
输出结果
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'}]
单参数是 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>
输出结果
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 类型
测试类
// 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,即要进行迭代的对象。觉得有点懵的伙伴可以回到最开始介绍 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)
- 上一篇: MySQL索引(java下一页)
- 下一篇: 周一分享(五十五):Matlab精读复刻论文
猜你喜欢
- 2024-10-02 掌控你的MySQL语句执行方案(如何让mysql执行脚本?)
- 2024-10-02 我爱Julia之入门-075(字符串05)
- 2024-10-02 Java入门超经典教程-数组的操作(java数组视频教学)
- 2024-10-02 AdaBoost算法(手稿+代码)(adaboost算法详解)
- 2024-10-02 有了for循环 为什么还要forEach?(为什么用for循环)
- 2024-10-02 UFS深入浅出 第二章 UFS结构 第三节 UFS分区
- 2024-10-02 Ruby 最常用指令和函数(备忘查询)
- 2024-10-02 MYSQL优化有理有据全分析(面试必备)
- 2024-10-02 最小优先队列 Index min priority queue
- 2024-10-02 mysql explain用法(mysql游标的定义与使用)
- 最近发表
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- js判断是否空对象 (63)
- pythoncase语句 (81)
- es6includes (73)
- sqlset (64)
- phprequire_once (61)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)