<insert id="insertOrUpdate" parameterType="XXXX" useGeneratedKeys="true"
keyProperty="id">
<include refid="insertSql"/>
on duplicate key update
<trim suffixOverrides=",">
<include refid="updateSql"/>
</trim>
</insert>
注意 配置 useGeneratedKeys="true" keyProperty="id"
on duplicate key update 存在即修改。但是当修改时,数据库中没有字段值变化,即返回的影响行为0时,这时是不返回主键的
不返回主键
返回主键
所以要确保一定有主键返回,就必须让mysql有影响行
on duplicate key update 修改时如果原值保存,就不能确保一定有字段值更新,就不保证一定有影响行,确保影响行就是加上更新时间字段,每次带上。updated_at = NOW(); 最为合适
请不要依赖Mysql datetime的 修改自动填入值 (ON UPDATE CURRENT_TIMESTAMP),因为影响行依然为0;