一条条更新效率太低,以下方式将提高效率。当然还有replace into的方式,但是该方式需要所有的字段,否则就为null。并且是根据主键或者是唯一索引来更新,有时候并不方便。
更新的sql:
update tblsupertitleresult set result =case
when (userHhCode=2001 and titleId=1)then 90
when (userHhCode=2001 and titleId=2)then 70
end
,checkState = case
when (userHhCode=2001 and titleId=1)then 80
when (userHhCode=2001 andtitleId=2)then 120
end
where (userHhCode=2001 and titleId=1) or(userHhCode=2001 and titleId=2) xml配置:
<update id="batchUpdateKeywords">
update sem_keywords
<trim prefix="set" suffixOverrides=",">
<trim prefix="platPlanId = case" suffix="end,">
<foreach collection="list" item="it" index="index">
when platKeywordsId=#{it.platKeywordsId} then #{it.platPlanId}
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="it" index="index">
(id= #{it.id} )
</foreach>
</update>
批量更新SQL与MyBatis配置
本文介绍了一种使用SQL及MyBatis进行高效批量更新的方法,通过案例展示了如何利用CASE WHEN语句来更新特定条件下的记录,并提供了MyBatis XML配置示例,包括动态SQL元素的使用技巧。
28万+

被折叠的 条评论
为什么被折叠?



