当前位置:Gxlcms > 数据库问题 > mybatis 查询sql时foreach使用法

mybatis 查询sql时foreach使用法

时间:2021-07-01 10:21:17 帮助过:5人阅读

找到俩个例子摘下来

sql查询用户in传list参数

<select id="getEmpsByConditionForeach" resultType="com.test.beans.Employee">
SELECT * FROM tb1_emplyee WHERE id IN
<foreach collection="list" item="item_id" separator="," open="(" close=")">
#{item_id}
</foreach>
</select>

批量插入


<insert id="addEmps">
INSERT INTO tb1_emplyee(last_name,email,gender,d_id)
VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
</foreach>
</insert>

collection:指定要遍历的集合:
list类型的参数会特殊处理封装在map中,map的key就叫list
item:将当前遍历出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
index:索引。遍历list的时候是index就是索引,item就是当前值
遍历map的时候index表示的就是map的key,item就是map的值
#{变量名}就能取出变量的值也就是当前遍历出的元素

mybatis 查询sql时foreach使用法

标签:sql   用户   value   bean   result   from   集合   结果   字符   

人气教程排行