一、概述
mybatis中實現(xiàn)批量插入是很簡單的,相比大家都知道,這里就不贅述,本文主要講述如何實現(xiàn)批量更新。
下面介紹本文要講的幾種方式主要是在xml中實現(xiàn),不包含需要改動代碼邏輯的方法,這里,除了網(wǎng)上說的普通情況,還有適合mysql和oracle的批量更新方式:
- case when
- foreach成多條sql
- ON DUPLICATE KEY UPDATE (mysql)
- replace into (mysql)
- 5.MERGE INTO(oracle)
- 6.INSERT ALL(oracle)
二、case when
這種方式實現(xiàn)的批量更新操作效率很低,而且,當(dāng)更新的字段很多時,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>
這里,根據(jù)index值來更新modify_time 和user_type的值,有幾個字段,就要遍歷幾遍,效率很低。
三、foreach成多條sql
這種方式最簡單,就是用foreach組裝成多條update語句,但Mybatis映射文件中的sql語句默認(rèn)是不支持以" ; " 結(jié)尾的,也就是不支持多條sql語句的執(zhí)行。所以需要在連接mysql的url上加 &allowMultiQueries=true 這個才可以執(zhí)行。
<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)使用的。
如果已存在該唯一標(biāo)示或主鍵就更新,如果不存在該唯一標(biāo)示或主鍵則作為新行插入。
<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的用法完全一樣,但是它帶有更新功能:
如果發(fā)現(xiàn)表中已經(jīng)有此行數(shù)據(jù)(根據(jù)主鍵或者唯一索引判斷)則先刪除此行數(shù)據(jù),然后插入新的數(shù)據(jù)。否則,直接插入新數(shù)據(jù)。
注意,它是先刪除數(shù)據(jù),然后再插入,這是和ON DUPLICATE KEY UPDATE不同的地方,如果當(dāng)前的數(shù)據(jù)庫用戶沒有刪除權(quán)限,是不能使用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>
五、MERGE INTO
判斷需要插入的數(shù)據(jù)是否存在,如果不存在就更新
語法:
<insert id="saveBatch" parameterType="java.util.List" >
MERGE INTO 表名 T1
USING (
<foreach collection="list" item="item" index="index" separator="union" >
SELECT
SYS_GUID() AS CODE,
#{item.createDate,jdbcType=VARCHAR} AS CREATE_DATE,
#{item.createBy,jdbcType=VARCHAR} AS CREATE_BY,
#{item.flagDel,jdbcType=CHAR} AS FLAG_DEL,
#{item.flagDisplay,jdbcType=CHAR} AS FLAG_DISPLAY,
#{item.sort,jdbcType=DECIMAL} AS SORT
FROM DUAL
</foreach>) T2
/*判斷是更新還是新記錄的條件*/
ON (
T1./*判斷數(shù)據(jù)是否重復(fù)的字段*/ = T2./*判斷數(shù)據(jù)是否重復(fù)的字段*/
AND T1./*判斷數(shù)據(jù)是否重復(fù)的字段*/ = T2./*判斷數(shù)據(jù)是否重復(fù)的字段*/
)
/*數(shù)據(jù)存在則更新*/
WHEN MATCHED THEN
UPDATE SET T./*需要更新的字段*/=T1/*需要更新的字段*/
/*數(shù)據(jù)不存在則插入新記錄*/
WHEN NOT MATCHED THEN
INSERT
(CODE,CREATE_DATE,CREATE_BY,FLAG_DEL,FLAG_DISPLAY,SORT)
VALUES
(T2.CODE,T2.CREATE_DATE,T2.CREATE_BY,T2.FLAG_DEL,T2.FLAG_DISPLAY,T2.SORT)
</insert>
例子:
<insert id="batchSave" parameterType="java.util.List">
MERGE INTO RES_SCHOOL_CLUB t1
USING (
<foreach collection="list" item="item" index="index" separator="union" >
select
#{item.id,jdbcType=VARCHAR} ID,
#{item.clsSchoolId,jdbcType=VARCHAR} CLS_SCHOOL_ID,
#{item.originSchoolId,jdbcType=VARCHAR} ORIGIN_SCHOOL_ID,
#{item.resourceId,jdbcType=VARCHAR} RESOURCE_ID,
#{item.clsClubId,jdbcType=VARCHAR} CLS_CLUB_ID,
#{item.baseAreaId,jdbcType=VARCHAR} BASE_AREA_ID,
#{item.createTime,jdbcType=TIMESTAMP} CREATE_TIME
from dual
</foreach>) t2
ON (
t1.CLS_SCHOOL_ID = t2.CLS_SCHOOL_ID
AND t1.RESOURCE_ID = t2.RESOURCE_ID
)
WHEN MATCHED THEN
UPDATE SET t.CREATE_TIME = t1.CREATE_TIME
WHEN NOT MATCHED THEN
INSERT
(ID, CLS_SCHOOL_ID, ORIGIN_SCHOOL_ID, RESOURCE_ID, CLS_CLUB_ID, BASE_AREA_ID, CREATE_TIME)
VALUES
(t2.ID, t2.CLS_SCHOOL_ID, t2.ORIGIN_SCHOOL_ID, t2.RESOURCE_ID, t2.CLS_CLUB_ID, t2.BASE_AREA_ID, t2.CREATE_TIME)
</insert>
注意:很容易造成的一個錯誤是:
錯誤:更新或者插入時候?qū)懕砻际清e誤的!比如:UPDATE 表名 SET... 或者 INSERT INTO 表名(...)
正確:直接UPDATE SET...和INSERT VALUES (....)括號里填相應(yīng)的字段,如果沒有給空值.
錯誤:ON 后面缺少(),ORA-00969: missing ON keyword
正確:無論 ON 后面條件多少都添加()
六、INSERT ALL
- mybatis批量插入oracle時需要顯式指定為 useGeneratedKeys="false"
<insert id="addList" parameterType="java.util.List" useGeneratedKeys="false">
INSERT ALL
<foreach item="item" index="index" collection="list">
INTO T_APPLAUD
(
ID,
USER_ID,
BUSINESS_TYPE,
PRODUCT_ID,
CREATE_TIME
) VALUES
(
#{item.id, jdbcType=NUMERIC},
#{item.userId, jdbcType=VARCHAR},
#{item.businessType, jdbcType=VARCHAR},
#{item.productId, jdbcType=VARCHAR},
#{item.createdTime, jdbcType=NUMERIC}
)
</foreach>
SELECT 1 FROM DUAL
</insert>
- 另外一種方法是 insert into table(...) (select ... from dual) union all (select ... from dual)
<insert id="addList" parameterType="java.util.List" useGeneratedKeys="false">
INSERT INTO T_APPLAUD
(
ID,
USER_ID,
BUSINESS_TYPE,
PRODUCT_ID,
CREATE_TIME
)
<foreach item="item" index="index" collection="list" separator="union all">
(
SELECT
#{item.id},
#{item.userId},
#{item.businessType},
#{item.productId},
#{item.createdTime}
FROM DUAL
)
</foreach>
</insert>