【mybatis】mapper.xml中foreach的用法,含批量查询、插入、修改、删除方法的使用

一、xml文件中foreach的主要属性

foreach元素的属性主要有 collection,item,index,separator,open,close。

collection: 表示集合,数据源

item :表示集合中的每一个元素

index :用于表示在迭代过程中,每次迭代到的位置

separator :表示在迭代时数据以什么符号作为分隔符

open :表示该语句以什么开始

close :表示以什么结束

二、foreach批量查询数据

1、当查询的参数只有一个时

例如:findByIds(List ids)

a.如果参数类型为List,在使用时,collection的属性需指定为list

b.如果参数类型为数组,则在使用时,collection属性需指定为array

<select id="findByIdsMap" resultMap="BaseResultMap">

Select sum(mark)

from table_user where user_id in

<foreach item="item" index="index" collection="list"

open="(" separator="," close=")">

#{item}

</foreach>

</select>

2、当查询的参数是一个对象时

例如:findByIds(List userList)

<select id="findByIdsMap" resultMap="BaseResultMap">

Select sum(mark)

from table_user where user_id in

<foreach item="item" index="index" collection="list"

open="(" separator="," close=")">

#{item.userId}

</foreach>

</select>

三、foreach批量插入数据

实现foreach批量插入数据有两种方法,一种是只发送一条 SQL,插入的多条数据之间通过"," 分隔开,另一种方式是每插入一条数据就发送一条 SQL 语句,多个 SQL 语句之间用";"分割。

1.一条 SQL 批量插入数据

对应的Mapper接口代码如下:

/** 返回值为 Integer 类型 */

Integer addStudentByList(@Param("list") List<Student> list);

1

2

对应的SQL 映射文件如下:

<insert id="addStudentByList" parameterType="">

INSERT INTO Student(username, age, password) VALUES

<foreach collection="list" item="item" separator=",">

(#{ item.username}, #{ item.age}, #{ item.password})

</foreach>

</insert>

2.执行多条 SQL 批量插入数据

对应的Mapper接口代码和一条 SQL 批量插入数据相同

对应的SQL 映射文件在一条 SQL 批量插入数据的基础上修改如下:

<insert id="addEmpsByList" parameterType="com.jas.mybatis.bean.Employee">

<!-- 每插入一条数据就执行一次 SQL,中间用";"分隔开 -->

<foreach collection="list" item="emp" separator=";">

INSERT INTO Student(username, age, password) VALUES

(#{ item.username}, #{ item.age}, #{ item.password})

</foreach>

</insert>

三、foreach批量更新数据

实现foreach批量插入数据有两种种方法,第一种是一条sql语句来批量更新数据,第二种是批量更新的sql。

一条 SQL 批量更新数据

一条sql语句来批量更新所有数据,下面直接看一下在mybatis中通常是怎么写的(去掉mybatis语法就是原生的sql语句了,所有就没单独说sql是怎么写的)

对应的SQL 映射文件如下:

<update id="updateBatch" parameterType="java.util.List">

update Student set username=

<foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">

when #{ item.id} then #{ item.username}

</foreach>

where id in

<foreach collection="list" index="index" item="item" separator="," open="(" close=")">

#{ item.id,jdbcType=BIGINT}

</foreach>

</update>

<------------------------------------分隔符-------------------------------->

<update id="updateBatch" parameterType="java.util.List">

<foreach collection="list" item="item" index="index" open="" close="" separator=";">

update Student

<set>

username=${ item.username}

</set>

where id = ${ item.id}

</foreach>

</update>

四、foreach批量删除数据

所对应的SQL映射文件如下:

<delete id="delAssetstype" parameterType="java.util.List">

DELETE FROM WHERE id in

<foreach collection="list" item="item" open="(" close=")" separator=",">

#{ item}

</foreach>

</delete>

对应的Mapper接口代码如下:

public void deleteAssetstype(List<Integer> ids);

相关推荐
CS Beginner2 小时前
【C语言】windows下编译mingw版本的glew库
c语言·开发语言·windows
小鸡脚来咯4 小时前
Redis与MySQL双写一致性(实战解决方案)
spring·oracle·mybatis
小鸡脚来咯6 小时前
Redis三大问题:穿透、击穿、雪崩(实战解析)
java·spring·mybatis
别来无恙blwy9 小时前
SQL Server高可用自动故障转移失败(短时间内多次转移失败,只需一步可处理)
数据库·windows·sqlserver·负载均衡·可用性测试
石像鬼₧魂石12 小时前
SET的钓鱼网站钓鱼模块
windows·学习·ubuntu
喵了几个咪12 小时前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:Makefile 在后端开发中的应用与 Windows 环境配置
windows
love530love14 小时前
在 PyCharm 中配置 x64 Native Tools Command Prompt for VS 2022 作为默认终端
ide·人工智能·windows·python·pycharm·prompt·comfyui
陌路2014 小时前
redis缓存雪崩,击穿,穿透
redis·缓存·mybatis
gf132111114 小时前
python_制作视频开头_根据短句字长占总字幕的长度比例拆分
windows·python·音视频
非凡ghost14 小时前
eDiary电子日记本(记录生活点滴)
windows·学习·生活·软件需求