mybatis更新数据t加if_转载:mybatis批量更新数据三种方法

更新时间:2023-06-04 00:31:23 阅读: 评论:0

mybatis更新数据t加if_转载:mybatis批量更新数据三种⽅
实现⽅式有三种,
⼀种⽤for循环通过循环传过来的参数集合,循环出N条sql,
⽤mysql的ca when 条件判断变相的进⾏批量更新《短歌行》
情人节哪天
是⽤ON DUPLICATE KEY UPDATE进⾏批量更新
中华圣人吧
注意第⼀种⽅法要想成功,需要在db链接url后⾯带⼀个参数  &allowMultiQueries=true
jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true
第⼀种 for循环
update standard_relation
standard_from_uuid = #{item.standardFromUuid,jdbcType=VARCHAR},
standard_to_uuid = #{item.standardToUuid,jdbcType=VARCHAR},
gmt_modified = #{Modified,jdbcType=TIMESTAMP},
where id = #{item.id,jdbcType=BIGINT}
第⼆种 ca when
中国最低工资update standard_relation
when id=#{i.id} then #{i.standardFromUuid}
乐在途中when id=#{i.id} then #{i.standardToUuid}
when id=#{i.id} then #{i.gmtModified}
where
id=#{i.id}
诸子喻山水第三种 ON DUPLICATE KEY UPDATE
inrt into standard_relation(id,relation_type, standard_from_uuid,
standard_to_uuid, relation_score, stat,
last_process_id, is_deleted, gmt_created,
gmt_modified,relation_desc)VALUES
(#{item.id,jdbcType=BIGINT},
#{lationType,jdbcType=VARCHAR},
#{item.standardFromUuid,jdbcType=VARCHAR},
#{item.standardToUuid,jdbcType=VARCHAR},
#{lationScore,jdbcType=DECIMAL},
#{item.stat,jdbcType=TINYINT},
#{item.lastProcessId,jdbcType=BIGINT},
#{item.isDeleted,jdbcType=TINYINT},
#{Created,jdbcType=TIMESTAMP},
#{Modified,jdbcType=TIMESTAMP},
#{lationDesc,jdbcType=VARCHAR})
ON DUPLICATE KEY UPDATE
id=VALUES(id),relation_type = VALUES(relation_type),你是我的英文
standard_from_uuid = VALUES(standard_from_uuid),
standard_to_uuid = VALUES(standard_to_uuid),
relation_score = VALUES(relation_score),
stat = VALUES(stat),
last_process_id = VALUES(last_process_id),
微光作文
is_deleted = VALUES(is_deleted),
gmt_created = VALUES(gmt_created),
gmt_modified = VALUES(gmt_modified),
relation_desc = VALUES(relation_desc)
测试运⾏效率
sql语句for循环效率其实相当⾼的,因为它仅仅有⼀个循环体,只不过最后update语句⽐较多,量⼤了就有可能造成sql阻塞。
ca when虽然最后只会有⼀条更新语句,但是xml中的循环体有点多,每⼀个ca when 都要循环⼀遍list集合,所以⼤批量拼sql的时候会⽐较慢,所以效率问题严重。使⽤的时候建议分批插⼊。
duplicate key update可以看出来是最快的,但是⼀般⼤公司都禁⽤,公司⼀般都禁⽌使⽤replace into和INSERT INTO … ON DUPLICATE KEY UPDATE,这种sql有可能会造成数据丢失和主从上表的⾃增id值不⼀致。⽽且⽤这个更新时,记得⼀定要加上id,⽽且values()括号⾥⾯放的是数据库字段,不是java对象的属性字段。
根据效率,安全⽅⾯综合考虑,选择适合的很重要。

本文发布于:2023-06-04 00:31:23,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/853738.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:循环   批量   数据   效率   造成   集合   可能   丢失
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图