Mybatis中實現批量更新的幾種姿勢,總有一款適合你

Mybatis中實現批量更新的幾種姿勢,總有一款適合你

一、概述

mybatis中實現批量插入是很簡單的,相比大家都知道,這裏就不贅述,本文主要講述如何實現批量更新。

下面介紹本文要講的幾種方式主要是在xml中實現,不包含需要改動代碼邏輯的方法,這裏,除了網上說的普通情況,還有適合mysql和oracle的批量更新方式:

  1. case when
  2. foreach成多條sql
  3. ON DUPLICATE KEY UPDATE (mysql)
  4. replace into (mysql)

這次,我要講的就是這四種方式。

首發地址:
品茗IT-同步發佈

如果大家正在尋找一個java的學習環境,或者在開發中遇到困難,可以加入我們的java學習圈,點擊即可加入,共同學習,節約學習時間,減少很多在學習中遇到的難題。

二、case when

這種方式實現的批量更新操作效率很低,而且,當更新的字段很多時,SQL語句會特別長。

<update id="updateBatch">
	update t_calendar_extend
	<trim prefix="set" suffixOverrides=",">
		<trim prefix="modify_time = case index" suffix="end,">
			<foreach collection="list" item="item">
				when #{item.index} then #{item.modifyTime}
			</foreach>
		</trim>
		<trim prefix="user_type = case index" suffix="end">
			<foreach collection="list" item="item">
				when #{item.index} then #{item.type}
			</foreach>
		</trim>
	</trim>
	<where>
		index in (
		<foreach collection="list" separator="," item="item">
			#{item.index}
		</foreach>
		)
	</where>
</update>

這裏,根據index值來更新modify_time 和user_type的值,有幾個字段,就要遍歷幾遍,效率很低。

三、foreach成多條sql

這種方式最簡單,就是用foreach組裝成多條update語句,但Mybatis映射文件中的sql語句默認是不支持以" ; " 結尾的,也就是不支持多條sql語句的執行。所以需要在連接mysql的url上加 &allowMultiQueries=true 這個纔可以執行。

<update id="updateBatch"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update tableName
        <set>
            name=${item.name},
            name2=${item.name2}
        </set>
        where id = ${item.id}
    </foreach>      
</update>

四、ON DUPLICATE KEY UPDATE

MYSQL中的ON DUPLICATE KEY UPDATE,是基於主鍵(PRIMARY KEY)或唯一索引(UNIQUE INDEX)使用的。

如果已存在該唯一標示或主鍵就更新,如果不存在該唯一標示或主鍵則作爲新行插入。

<update id="updateBatch">
	insert into t_output_calendar (index, 
	  cal_date, user_type, create_time, 
	  modify_time, delete_flag
	  )
	values
	<foreach collection="list" item="item" index="index"
		separator=",">
		(
		#{item.index,jdbcType=INTEGER}, 
		#{item.calDate,jdbcType=TIMESTAMP}, 
		#{item.type,jdbcType=TINYINT}, 
		#{item.createTime,jdbcType=TIMESTAMP}, 
		#{item.modifyTime,jdbcType=TIMESTAMP}, 
		#{item.deleteFlag,jdbcType=TINYINT}
		)
	</foreach>
	ON DUPLICATE KEY UPDATE modify_time = VALUES(modify_time), user_type = VALUES(user_type);
</update>

五、replace into

msql的replace into跟insert into的用法完全一樣,但是它帶有更新功能:

如果發現表中已經有此行數據(根據主鍵或者唯一索引判斷)則先刪除此行數據,然後插入新的數據。否則,直接插入新數據。

注意,它是先刪除數據,然後再插入,這是和ON DUPLICATE KEY UPDATE不同的地方,如果當前的數據庫用戶沒有刪除權限,是不能使用replace into的。

示例:

<insert id="updateBatch" parameterType="java.util.List">
	replace into t_output_calendar (index, cal_date, user_type, create_time, 
	  modify_time, delete_flag
	  )
	values
	<foreach collection="list" item="item" index="index"
		separator=",">
		(
		#{item.index,jdbcType=INTEGER}, 
		#{item.calDate,jdbcType=TIMESTAMP}, 
		#{item.type,jdbcType=TINYINT}, 
		#{item.createTime,jdbcType=TIMESTAMP}, 
		#{item.modifyTime,jdbcType=TIMESTAMP}, 
		#{item.deleteFlag,jdbcType=TINYINT}
		)
	</foreach>
</insert>

快速構建項目

Spring項目快速開發工具:

一鍵快速構建Spring項目工具

一鍵快速構建SpringBoot項目工具

一鍵快速構建SpringCloud項目工具

一站式Springboot項目生成

Mysql一鍵生成Mybatis註解Mapper

Spring組件化構建

SpringBoot組件化構建

SpringCloud服務化構建

喜歡這篇文章麼,喜歡就加入我們一起討論SpringBoot使用吧!
品茗IT交流羣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章