mybatis實現mysql批量插入或者更新

單個插入或者更新:

 <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,我試了加了會報錯,可能是版本問題吧,這個問題當初查了好久才發現

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章