Mybatis?批量更新實(shí)體對(duì)象方式
Mybatis批量更新實(shí)體對(duì)象
(1)Dao層接口
/**
* 根據(jù)更新采購(gòu)計(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ù)三種寫(xiě)法的效率問(wèn)題
實(shí)現(xiàn)方式有三種
- 1、用for循環(huán)通過(guò)循環(huán)傳過(guò)來(lái)的參數(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í)這種東西寫(xiě)過(guò)來(lái)寫(xiě)過(guò)去就是差不多一樣的代碼,不做重復(fù)的贅述,直接上代碼。
<!-- 批量更新第一種方法,通過(guò)接收傳進(jìn)來(lái)的參數(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>
<!-- 批量更新第二種方法,通過(guò) case when語(yǔ)句變相的進(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語(yǔ)句for循環(huán)效率其實(shí)相當(dāng)高的,因?yàn)樗鼉H僅有一個(gè)循環(huán)體,只不過(guò)最后update語(yǔ)句比較多,量大了就有可能造成sql阻塞。
case when雖然最后只會(huì)有一條更新語(yǔ)句,但是xml中的循環(huán)體有點(diǎn)多,每一個(gè)case when 都要循環(huán)一遍list集合,所以大批量拼sql的時(shí)候會(huì)比較慢,所以效率問(wèn)題嚴(yán)重。使用的時(shí)候建議分批插入。
duplicate key update可以看出來(lái)是最快的,但是一般大公司都禁用,公司一般都禁止使用replace into和INSERT INTO … ON DUPLICATE KEY UPDATE,這種sql有可能會(huì)造成數(shù)據(jù)丟失和主從上表的自增id值不一致。而且用這個(gè)更新時(shí),記得一定要加上id,而且values()括號(hào)里面放的是數(shù)據(jù)庫(kù)字段,不是java對(duì)象的屬性字段。

根據(jù)效率,安全方面綜合考慮,選擇適合的很重要。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot應(yīng)用中如何動(dòng)態(tài)指定數(shù)據(jù)庫(kù)實(shí)現(xiàn)不同用戶不同數(shù)據(jù)庫(kù)的問(wèn)題
讓我們創(chuàng)建一個(gè) Spring Boot 項(xiàng)目首先設(shè)置一個(gè)具有必要依賴項(xiàng)的新 Spring Boot項(xiàng)目,在項(xiàng)目配置中包括 Spring Web、Spring Data JPA 和關(guān)于數(shù)據(jù)庫(kù)的依賴項(xiàng),接下來(lái)介紹Spring?Boot應(yīng)用中如何動(dòng)態(tài)指定數(shù)據(jù)庫(kù),實(shí)現(xiàn)不同用戶不同數(shù)據(jù)庫(kù)的場(chǎng)景?,需要的朋友可以參考下2024-04-04
java如何自定義List中的sort()排序,用于日期排序
這篇文章主要介紹了java如何自定義List中的sort()排序,用于日期排序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java基礎(chǔ)之創(chuàng)建虛擬機(jī)對(duì)象的過(guò)程詳細(xì)總結(jié)
本文基于虛擬機(jī)HotSpot和常用的內(nèi)存區(qū)域Java堆深入對(duì)象分配、布局和訪問(wèn)的全過(guò)程,文中有非常詳細(xì)的圖文解說(shuō),對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Java編程細(xì)節(jié)重構(gòu)之為什么if-else不是好代碼詳析
這篇文章主要給大家介紹了關(guān)于Java編程細(xì)節(jié)重構(gòu)之為什么if-else不是好代碼的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)些學(xué)習(xí)吧2018-09-09
Java中的IP地址和InetAddress類(lèi)使用詳解
這篇文章主要介紹了Java中的IP地址和InetAddress類(lèi)使用詳解,是Java入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10
Java使用TCP套接字實(shí)現(xiàn)多人聊天功能詳解
這篇文章主要介紹了Java使用TCP套接字實(shí)現(xiàn)多人聊天功能,結(jié)合實(shí)例形式詳細(xì)分析了java使用socket通信實(shí)現(xiàn)tcp協(xié)議下的聊天功能客戶端與服務(wù)器端相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-10-10
解決springboot application.properties server.port配置問(wèn)題
這篇文章主要介紹了解決springboot application.properties server.port配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

