Mybatis?批量更新實(shí)體對(duì)象方式
Mybatis批量更新實(shí)體對(duì)象
(1)Dao層接口
/** * 根據(jù)更新采購計(jì)劃(批量) * @param plans */ void batchUpdatePlan(List<PubPurchasePlan> plans);
(2)Mapper.xml 文件
<sql id="batchUpdatePlanCondition"> <where> <foreach collection="list" item="item" open="( " separator=") or (" close=" )"> comId = #{item.comId} AND id = #{item.id} </foreach> </where> </sql> <update id="batchUpdatePlan" parameterType="list"> UPDATE pub_purchase_plan <trim prefix="set" suffixOverrides=","> <trim prefix="warehouseId=case" suffix="end,"> <foreach collection="list" item="item" index="index"> WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.warehouseId} </foreach> </trim> <trim prefix="productId=case" suffix="end,"> <foreach collection="list" item="item" index="index"> WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.productId} </foreach> </trim> <trim prefix="amount=case" suffix="end,"> <foreach collection="list" item="item" index="index"> WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.amount} </foreach> </trim> <trim prefix="deleted=case" suffix="end,"> <foreach collection="list" item="item" index="index"> WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.deleted} </foreach> </trim> <trim prefix="price=case" suffix="end,"> <foreach collection="list" item="item" index="index"> WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.price} </foreach> </trim> <trim prefix="type=case" suffix="end,"> <foreach collection="list" item="item" index="index"> WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.type} </foreach> </trim> </trim> <include refid="batchUpdatePlanCondition"/> </update>
Mybatis批量更新數(shù)據(jù)三種方法效率對(duì)比
探討批量更新數(shù)據(jù)三種寫法的效率問題
實(shí)現(xiàn)方式有三種
- 1、用for循環(huán)通過循環(huán)傳過來的參數(shù)集合,循環(huán)出N條sql
- 2、用mysql的case when 條件判斷變相的進(jìn)行批量更新
- 3、用ON DUPLICATE KEY UPDATE進(jìn)行批量更新
下面進(jìn)行實(shí)現(xiàn)。
注意第一種方法要想成功,需要在db鏈接url后面帶一個(gè)參數(shù) &allowMultiQueries=true
即: jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true
其實(shí)這種東西寫過來寫過去就是差不多一樣的代碼,不做重復(fù)的贅述,直接上代碼。
<!-- 批量更新第一種方法,通過接收傳進(jìn)來的參數(shù)list進(jìn)行循環(huán)著組裝sql --> <update id="updateBatch" parameterType="java.util.List" > <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update standard_relation <set > <if test="item.standardFromUuid != null" > standard_from_uuid = #{item.standardFromUuid,jdbcType=VARCHAR}, </if> <if test="item.standardToUuid != null" > standard_to_uuid = #{item.standardToUuid,jdbcType=VARCHAR}, </if> <if test="item.gmtModified != null" > gmt_modified = #{item.gmtModified,jdbcType=TIMESTAMP}, </if> </set> where id = #{item.id,jdbcType=BIGINT} </foreach> </update> <!-- 批量更新第二種方法,通過 case when語句變相的進(jìn)行批量更新 --> <update id="updateBatch" parameterType="java.util.List" > update standard_relation <trim prefix="set" suffixOverrides=","> <trim prefix="standard_from_uuid =case" suffix="end,"> <foreach collection="list" item="i" index="index"> <if test="i.standardFromUuid!=null"> when id=#{i.id} then #{i.standardFromUuid} </if> </foreach> </trim> <trim prefix="standard_to_uuid =case" suffix="end,"> <foreach collection="list" item="i" index="index"> <if test="i.standardToUuid!=null"> when id=#{i.id} then #{i.standardToUuid} </if> </foreach> </trim> <trim prefix="gmt_modified =case" suffix="end,"> <foreach collection="list" item="i" index="index"> <if test="i.gmtModified!=null"> when id=#{i.id} then #{i.gmtModified} </if> </foreach> </trim> </trim> where <foreach collection="list" separator="or" item="i" index="index" > id=#{i.id} </foreach> </update> 批量更新第三種方法,用ON DUPLICATE KEY UPDATE <insert id="updateBatch" parameterType="java.util.List"> insert 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 <foreach collection="list" item="item" index="index" separator=","> (#{item.id,jdbcType=BIGINT},#{item.relationType,jdbcType=VARCHAR}, #{item.standardFromUuid,jdbcType=VARCHAR}, #{item.standardToUuid,jdbcType=VARCHAR}, #{item.relationScore,jdbcType=DECIMAL}, #{item.stat,jdbcType=TINYINT}, #{item.lastProcessId,jdbcType=BIGINT}, #{item.isDeleted,jdbcType=TINYINT}, #{item.gmtCreated,jdbcType=TIMESTAMP}, #{item.gmtModified,jdbcType=TIMESTAMP},#{item.relationDesc,jdbcType=VARCHAR}) </foreach> 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) </insert>
@Override public void updateStandardRelations() { List<StandardRelation> list=standardRelationMapper.selectByStandardUuid("xiemingjieupdate"); for(StandardRelation tmp:list){ tmp.setStandardFromUuid(tmp.getStandardFromUuid()+"update"); tmp.setStandardToUuid(tmp.getStandardToUuid()+"update"); } long begin=System.currentTimeMillis(); standardRelationManager.updateBatch(list); long end=System.currentTimeMillis(); System.out.print("當(dāng)前的批量更新的方法用時(shí)"+(end-begin)+"ms"); }
sql語句for循環(huán)效率其實(shí)相當(dāng)高的,因?yàn)樗鼉H僅有一個(gè)循環(huán)體,只不過最后update語句比較多,量大了就有可能造成sql阻塞。
case when雖然最后只會(huì)有一條更新語句,但是xml中的循環(huán)體有點(diǎn)多,每一個(gè)case when 都要循環(huán)一遍list集合,所以大批量拼sql的時(shí)候會(huì)比較慢,所以效率問題嚴(yán)重。使用的時(shí)候建議分批插入。
duplicate key update可以看出來是最快的,但是一般大公司都禁用,公司一般都禁止使用replace into和INSERT INTO … ON DUPLICATE KEY UPDATE,這種sql有可能會(huì)造成數(shù)據(jù)丟失和主從上表的自增id值不一致。而且用這個(gè)更新時(shí),記得一定要加上id,而且values()括號(hào)里面放的是數(shù)據(jù)庫字段,不是java對(duì)象的屬性字段。
根據(jù)效率,安全方面綜合考慮,選擇適合的很重要。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用google.kaptcha來生成圖片驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要介紹了使用google.kaptcha來生成圖片驗(yàn)證碼的實(shí)現(xiàn)方法,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09SpringBoot實(shí)現(xiàn)API接口多版本支持的示例代碼
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)API接口多版本支持的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10java前后端加密解密crypto-js的實(shí)現(xiàn)
這篇文章主要介紹了java前后端加密解密crypto-js的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05SSH框架網(wǎng)上商城項(xiàng)目第20戰(zhàn)之在線支付平臺(tái)
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第20戰(zhàn)之在線支付平臺(tái),關(guān)于第三方支付的內(nèi)容從本文開始,感興趣的小伙伴們可以參考一下2016-06-06IDEA實(shí)現(xiàn)添加 前進(jìn)后退 到工具欄的操作
這篇文章主要介紹了IDEA 前進(jìn) 后退 添加到工具欄的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02一文教會(huì)你如何搭建vue+springboot項(xiàng)目
最近在搗鼓?SpringBoot?與?Vue?整合的項(xiàng)目,所以下面這篇文章主要給大家介紹了關(guān)于如何通過一篇文章教會(huì)你搭建vue+springboot項(xiàng)目,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05