mybatis实现mysql批量插入或者更新

单个插入或者更新:

 
      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()
  

批量:


	
		
			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()
		
	

dao层:


    void updateOrInsert(@Param("list") List list);

ON DUPLICATE KEY UPDATE后边不需要values,看了好多博客后边都加了values,我试了加了会报错,可能是版本问题吧,这个问题当初查了好久才发现

你可能感兴趣的:(web开发,db)