MySql foreach属性
foreach属性
属性 | 描述 |
---|---|
item | 循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。 具体说明:在list和数组中是其中的对象,在map中是value。 该参数为必选。 |
collection | 要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象用map代替作为键。 当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array,map将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子: 如果User有属性List ids。入参是User对象,那么这个collection = "ids" 如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id" 上面只是举例,具体collection等于什么,就看你想对那个元素做循环。 该参数为必选。 |
separator | 元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。 |
open | foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。 |
close | foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。 |
index | 在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。 |
1.select count(*) from users id in (x1,x2,x3,...)
<select id="countByUserList" resultType="int" parameterType="list"> select count(*) from users <where> id in <foreach item="item" collection="list" separator="," open="(" close=")" index=""> #{item.id, jdbcType=NUMERIC} </foreach> </where> </select>
2.循环插入表数据,用到dual伪表给数据做掩护。
insert into deliver (col1,col2,col3,col4,...) select col1,col2,col3,col4... from dual union all select col11,col22,col33,col44,... from dual。(字段col1,col2,col3,col4,...)添加
或者
insert into deliver select col1,col2,col3,col4,... from dual union all select col11,col22,col33,col44,... from dual。(全部字段添加)
<insert id="addList"> INSERT INTO DELIVER ( <include refid="selectAllColumnsSql"/> ) <foreach collection="deliverList" item="item" separator="UNION ALL"> SELECT #{item.id, jdbcType=NUMERIC}, #{item.name, jdbcType=VARCHAR} FROM DUAL </foreach> </insert>
3.循环插入map值 insert into users(key,values) values(key1,values1),(key2,values3),(key3,values4)
<insert id="ins_string_string"> insert into string_string (key, value) values <foreach item="item" index="key" collection="map" open="" separator="," close="">(#{key}, #{item})</foreach> </insert>
4.select count(*) from key_cols where col_a = ? AND col_b = ?
(一定要注意到$和#的区别,$的参数直接输出,#的参数会被替换为?,然后传入参数值,加上' '后执行。可以防止sql注入)
<select id="sel_key_cols" resultType="int"> select count(*) from key_cols where <foreach item="item" index="key" collection="map" open="" separator="AND" close="">${key} = #{item}</foreach> </select>
5.select * from t_news n where n.tags like ? or n.tags like ?
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper"> select * from t_news n where <foreach collection="listTag" index="index" item="tag" open="" separator="or" close=""> n.tags like '%'||#{tag}||'%' </foreach> <select>
分类:
数据库及优化
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库