MySQL批量插入批量更新及批量删除语句
Posted 牛大闲人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL批量插入批量更新及批量删除语句相关的知识,希望对你有一定的参考价值。
1、批量插入
-
<insert
-
id="insertBatch"
-
parameterType="java.util.List">
-
insert into
-
t_student(name, age, class)
-
values
-
<foreach collection="list" item="item" index="index" separator=",">
-
(
-
-
-
-
)
-
</foreach>
-
</insert>
2、批量更新
方式一:
-
<update id="updateBatch">
-
<foreach collection="list" separator=";" item="stud">
-
update t_studetn set
-
name =
-
age =
-
class =
-
where id =
-
</foreach>
-
</update>
方式二:
-
<update id="updateBatch" parameterType="list">
-
UPDATE t_student
-
SET name = CASE id
-
<foreach collection="list" item="i" index="index">
-
WHEN
-
THEN
-
</foreach>
-
END,
-
age = CASE id
-
<foreach collection="list" item="i" index="index">
-
WHEN
-
</foreach>
-
END
-
WHERE id IN
-
<foreach collection="list" separator="or" item="i" index="index" >
-
id=
-
</foreach>
-
</update>
3、批量删除
-
<delete id="deleteBatchByParams">
-
delete from
-
t_student
-
where
-
id IN
-
<foreach collection="ids" item="item" index="index" open="("close=")"separator=",">
-
#{item}
-
</foreach>
-
</delete>
item | 循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。 具体说明:在list和数组中是其中的对象,在map中是value。 该参数为必选。 |
collection |
要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象没有默认的键。 |
separator | 元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。 |
open | foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。 |
close | foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。 |
index | 在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。 |
https://blog.csdn.net/u014252478/article/details/91386289
以上是关于MySQL批量插入批量更新及批量删除语句的主要内容,如果未能解决你的问题,请参考以下文章