单个插入或者更新:
<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,我试了加了会报错,可能是版本问题吧,这个问题当初查了好久才发现