mybatis高效率批量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>
1、controlller
@RequestMapping(value = "/updateUserLog", method = RequestMethod.POST)
public BWJsonResult updateUserLog(@RequestBody List<UserLog> userLogList) {
if (CollectionUtils.isEmpty(userLogList)) {
return BWJsonResultUtil.buildErrBWJsonResult(UserServiceConstants.CODE_80110002, UserServiceConstants.CODE_MSG_80110002);
}
try {
userService.updateUserLog(userLogList);
return BWJsonResultUtil.buildSuccBWJsonResult("操作成功");
} catch (Exception e) {
logger.error("UserController findUserByUserArea error {}", e);
return BWJsonResultUtil.buildErrBWJsonResult(UserServiceConstants.CODE_80110001, UserServiceConstants.CODE_MSG_80110001);
}
}
2、service
void updateUserLog(List<UserLog> userLogList);
3、serviceImpl
public void updateUserLog(List<UserLog> userLogList) {
//修改用户日志表信息
userLogMapper.updateUserLog(userLogList);
}
4、dao
void updateUserLog(List<UserLog> userLogList);
5、mapper
<update id="updateUserLog" parameterType="java.util.List">
update ps_user_log
SET dept_id =
<foreach collection="list" item="item" index="index"
separator=" " open="case id" close="end">
when #{item.id} then #{item.deptId}
</foreach>
, type =
<foreach collection="list" item="item" index="index"
separator=" " open="case id" close="end">
when #{item.id} then #{item.type}
</foreach>
where id in (
<foreach collection="list" item="item" index="index"
separator=",">
#{item.id}
</foreach>
)
</update>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-05-05 mybatis 遍历字段和字段对应的值循环插入