mybatis 根據主鍵批量insert或update


1、需求:

  主鍵存在:update

  主鍵不存在:insert

2、關鍵語法:

  on duplicate key update

3、批量操作

 1    <insert id="insertOrUpdateCoLod" parameterType="map">
 2         insert into sd_colog_details (call_time, logid, call_number,
 3                                       call_type, call_result, time_length,
 4                                       trans_result,co_id
 5         )
 6         values
 7         <foreach collection="coLogList" item="coLog" separator=",">
 8             (
 9             #{coLog.callTime,jdbcType=TIMESTAMP},
10             #{coLog.logid,jdbcType=BIGINT},
11             #{coLog.callNumber,jdbcType=VARCHAR},
12             #{coLog.callType,jdbcType=SMALLINT},
13             #{coLog.callResult,jdbcType=SMALLINT},
14             #{coLog.timeLength,jdbcType=INTEGER},
15             #{coLog.transResult,jdbcType=SMALLINT},
16             #{coLog.coId,jdbcType=VARCHAR}
17             )
18         </foreach>
19         on duplicate key update
20             -- 需要更新的字段
21             logid=values(logid),
22             call_number=values(call_number),
23             call_type=values(call_type),
24             call_result=values(call_result),
25             time_length=values(time_length),
26             trans_result=values(trans_result)
27     </insert>

備注:其中主鍵為聯合主鍵(co_id, call_time)

 on duplicate key update 后面根據 需要更新的字段


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM