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 后面根据 需要更新的字段