Mybatis之批量更新操作
来源:互联网 发布:阿里云哪里的服务器好 编辑:程序博客网 时间:2024/05/17 02:49
更新单条记录
UPDATE course SET name = 'course1' WHERE id = 'id1';
更新多条记录的同一个字段为同一个值
UPDATE course SET name = 'course1' WHERE id in ('id1', 'id2', 'id3);
更新多条记录为多个字段为不同的值
比较普通的写法,是通过循环,依次执行update语句。
Mybatis写法如下:
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update course
<set>
name=${item.name}
</set>
where id = ${item.id}
</foreach>
</update>
一条记录update一次,性能比较差,容易造成阻塞。
MySQL没有提供直接的方法来实现批量更新,但可以使用case when语法来实现这个功能。
UPDATE course
SET name = CASE id
WHEN 1 THEN 'name1'
WHEN 2 THEN 'name2'
WHEN 3 THEN 'name3'
END,
title = CASE id
WHEN 1 THEN 'New Title 1'
WHEN 2 THEN 'New Title 2'
WHEN 3 THEN 'New Title 3'
END
WHERE id IN (1,2,3)
这条sql的意思是,如果id为1,则name的值为name1,title的值为New Title1;依此类推。
在Mybatis中的配置则如下:
<update id="updateBatch" parameterType="list">
update course
<trim prefix="set" suffixOverrides=",">
<trim prefix="peopleId =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.peopleId!=null">
when id=#{i.id} then #{i.peopleId}
</if>
</foreach>
</trim>
<trim prefix=" roadgridid =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.roadgridid!=null">
when id=#{i.id} then #{i.roadgridid}
</if>
</foreach>
</trim>
<trim prefix="type =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.type!=null">
when id=#{i.id} then #{i.type}
</if>
</foreach>
</trim>
<trim prefix="unitsid =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.unitsid!=null">
when id=#{i.id} then #{i.unitsid}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
id=#{i.id}
</foreach>
</update>
UPDATE course SET name = 'course1' WHERE id = 'id1';
更新多条记录的同一个字段为同一个值
UPDATE course SET name = 'course1' WHERE id in ('id1', 'id2', 'id3);
更新多条记录为多个字段为不同的值
比较普通的写法,是通过循环,依次执行update语句。
Mybatis写法如下:
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update course
<set>
name=${item.name}
</set>
where id = ${item.id}
</foreach>
</update>
一条记录update一次,性能比较差,容易造成阻塞。
MySQL没有提供直接的方法来实现批量更新,但可以使用case when语法来实现这个功能。
UPDATE course
SET name = CASE id
WHEN 1 THEN 'name1'
WHEN 2 THEN 'name2'
WHEN 3 THEN 'name3'
END,
title = CASE id
WHEN 1 THEN 'New Title 1'
WHEN 2 THEN 'New Title 2'
WHEN 3 THEN 'New Title 3'
END
WHERE id IN (1,2,3)
这条sql的意思是,如果id为1,则name的值为name1,title的值为New Title1;依此类推。
在Mybatis中的配置则如下:
<update id="updateBatch" parameterType="list">
update course
<trim prefix="set" suffixOverrides=",">
<trim prefix="peopleId =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.peopleId!=null">
when id=#{i.id} then #{i.peopleId}
</if>
</foreach>
</trim>
<trim prefix=" roadgridid =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.roadgridid!=null">
when id=#{i.id} then #{i.roadgridid}
</if>
</foreach>
</trim>
<trim prefix="type =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.type!=null">
when id=#{i.id} then #{i.type}
</if>
</foreach>
</trim>
<trim prefix="unitsid =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.unitsid!=null">
when id=#{i.id} then #{i.unitsid}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
id=#{i.id}
</foreach>
</update>
0 0
- Mybatis之批量更新操作
- Mybatis之批量更新操作
- Mybatis之批量更新操作
- Mybatis -- 批量更新操作
- mybatis 进行批量更新操作
- spring springmvc mybatis 整合批量操作,批量更新,批量插入
- mybatis 批量查询插入、更新操作
- Mybatis之批量操作-Oracle
- MySql+Mybatis+Druid 优化之MyBatis批量删除、更新
- Mybatis使用经验分享之批量操作
- Mybatis使用经验分享之批量操作
- mybatis之mysql&oracle 批量操作
- SSM之Mybatis查询以及批量操作
- Mybatis 批量增加,批量更新
- Mybatis 批量插入、批量更新
- Mybatis批量更新
- Mybatis批量更新
- Mybatis批量更新
- Linux学习总结
- JS 中的 with 关键字
- 存储类型和static关键字作用小结
- spark机器学习 源码解析及原理分析
- Android初学习 - FLAG_ACTIVITY_NEW_TASK和singleTask一起使用
- Mybatis之批量更新操作
- Nagios 快速实现数据可视化的几种方式
- PRi——lab3部分代码
- 浅谈ROLAP、MOLAP和HOLAP区别
- js中this指向问题实例详解
- Linux里必须掌握的命令
- 不惑JAVA之JAVA基础 - 反射
- 解决Cannot change version of project facet Dynamic web module to 2.5
- hdu-4526-威威猫系列故事——拼车记-DP