單個插入或者更新:
<update id="updateOrInsert">
INSERT INTO tb_user_integration_month
(userId,integralMonthCount,integralLeaveCount,month,createTime,updateTime)
VALUES
(#{userId},#{integralMonthCount},#{integralLeaveCount},#{month},now(),null)
ON DUPLICATE KEY UPDATE
integralMonthCount=integralMonthCount+#{integralMonthCount},integralLeaveCount=integralLeaveCount+#{integralLeaveCount},updateTime=now()
</update>
批量:
<!--批量插入or更新月度統計表-->
<update id="updateOrInsert">
<foreach collection="list" index="index" item="item" separator=";">
INSERT INTO tb_user_integration_month
(userId,integralMonthCount,integralLeaveCount,month,createTime,updateTime)
VALUES
(#{item.userId},#{item.integration},#{item.integration},date_format(now(),'%Y-%m'),now(),null)
ON DUPLICATE KEY UPDATE
integralMonthCount=integralMonthCount+#{item.integration},integralLeaveCount=integralLeaveCount+#{item.integration},updateTime=now()
</foreach>
</update>
dao層:
void updateOrInsert(@Param("list") List<UserMoney> list);
ON DUPLICATE KEY UPDATE後邊不需要values,看了好多博客後邊都加了values,我試了加了會報錯,可能是版本問題吧,這個問題當初查了好久才發現