insert ... on duplicate update 返回主键ID

<insert id="insert" parameterClass="AddReq">
  INSERT INTO TABLE (  
  A,B,C
  ) VALUES(
  #A#,#B#,#C#
  )
<!-- 主键冲突更新逻辑 -->
	on duplicate key update
	<!-- 将ID赋值,当出现duplicate key会返回id字段,这里id是主键字段,如果不加这句出现唯一索引冲突更新后SELECT LAST_INSERT_ID()没有值,上层逻辑无法兼容 -->
	id = LAST_INSERT_ID(id),
	dataStatus = 1,
	updateTime = now()
	<isNotEmpty property="userName" prepend="," removeFirstPrepend="true">
	userName = #userName#
	</isNotEmpty>
	<selectKey resultClass="java.lang.Long" keyProperty="id">
	SELECT LAST_INSERT_ID()
	</selectKey>
</insert>
posted @ 2021-01-15 18:13  大摩羯先生  阅读(123)  评论(0编辑  收藏  举报