Mybatis不支持batchInsertOrUpdate返顯id問題
Mybatis不支持batchInsertOrUpdate返顯id
需求背景
batchInsertOrUpdate 一批數(shù)據(jù)到 MySQL,并且需要將每條記錄的 id 返顯(write back)
現(xiàn)象
有ON DUPLICATE KEY UPDATE的 batchInsertOrUpdate 無法將所有 id write back,在我的例子中,是 batch 兩條,但只有其中一條 id 能帶回來,并且更嚴重的是映射到對應(yīng)的對象偶爾也是錯的。
mybatis-version: 3.5.5
<insert id="batchInsert" useGeneratedKeys="true" keyProperty="id">
INSERT INTO month (month, game_id)
VALUES
<foreach collection="list" item="f" separator=",">
(#{f.month}, #{f.gameId})
</foreach>
ON DUPLICATE KEY UPDATE
game_id = VALUES(game_id)
</insert>把ON DUPLICATE KEY UPDATE刪掉變成 batchInsert 則可以批量返顯id
排查方法
Google + 追蹤分析 Mybatis 源碼 和 JDBC 源碼 + Mybatis Github 和 Mybatis 官方 Google Groups 請教國外開發(fā)者。
心得
Mybatis 不支持 batchInsertOrUpdate 返顯id,僅 batchInsert 可批量返顯id,需拆分 batchInsert 和 batchUpdate;或者單條insertOrUpdate。
問題出在JDBC源碼這StatementImpl.getGeneratedKeysInternal()。
Mybatis執(zhí)行批量更新batch update方式
1、數(shù)據(jù)庫連接必須配置:&allowMultiQueries=true
我的配置如下:
jdbc:mysql://10.20.13.16:3306/CALENDAR?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
2、批量修改并加判斷條件(修改字段可選)
? ? <!-- 批量更新賽程 -->
? ? <update id="updateMatchs" parameterType="java.util.List">
? ? ? ? <foreach collection="matchs" item="item" index="index" open="" close="" separator=";">
? ? ? ? ? ? update t_match
? ? ? ? ? ? <set>
? ? ? ? ? ? ? ? <if test="item.title !=null">
? ? ? ? ? ? ? ? ? ? TITLE = #{item.title,jdbcType=VARCHAR},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="item.homeScore !=null">
? ? ? ? ? ? ? ? ? ? HOME_SCORE = #{item.homeScore,jdbcType=INTEGER},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="item.visitScore !=null">
? ? ? ? ? ? ? ? ? ? VISTT_SCORE = #{item.visitScore,jdbcType=INTEGER},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="item.liveSource !=null">
? ? ? ? ? ? ? ? ? ? LIVE_SOURCE = #{item.liveSource,jdbcType=VARCHAR},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="item.liveURL !=null">
? ? ? ? ? ? ? ? ? ? LIVE_URL = #{item.liveURL,jdbcType=VARCHAR},
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? <if test="item.isHotMatch !=null">
? ? ? ? ? ? ? ? ? ? IS_HOT_MATCH = #{item.isHotMatch,jdbcType=VARCHAR}
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? </set>
? ? ? ? where HOME_TEAM_ID = #{item.homeTeamId,jdbcType=VARCHAR} and
? ? ? ? VISIT_TEAM_ID = #{item.visitTeamId,jdbcType=VARCHAR} and
? ? ? ? MATCH_TIME = #{item.matchTime,jdbcType=BIGINT}
? ? ? ? </foreach>
? ? </update>java接口
? ? /** ? ? ?* 批量修改賽程 ? ? ?* ? ? ?* @param matchs ? ? ?* @throws DaoException ? ? ?*/ ? ? void updateMatchs(@Param(value = "matchs")List<MatchBasic> matchs);
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java 中createStatement()方法的實例詳解
這篇文章主要介紹了java 中createStatement()方法的實例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
Java線程池ForkJoinPool(工作竊取算法)的使用
Fork就是把一個大任務(wù)切分為若干個子任務(wù)并行地執(zhí)行,Join就是合并這些子任務(wù)的執(zhí)行結(jié)果,最后得到這個大任務(wù)的結(jié)果。Fork/Join?框架使用的是工作竊取算法。本文主要介紹了ForkJoinPool的使用,需要的可以參考一下2022-11-11
SpringBoot中多環(huán)境yml的配置與打包問題
這篇文章主要介紹了SpringBoot中多環(huán)境yml的配置與打包問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Java反轉(zhuǎn)字符串和相關(guān)字符編碼的問題解決
反轉(zhuǎn)字符串一直被當(dāng)作是簡單問題,大家的思想主要就是利用遍歷,首尾交換字符實現(xiàn)字符串的反轉(zhuǎn)。例如下面的代碼,就可以簡單實現(xiàn)反轉(zhuǎn)。2013-05-05
詳解springboot設(shè)置cors跨域請求的兩種方式
這篇文章主要介紹了詳解springboot設(shè)置cors跨域請求的兩種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11

