mybatis+sqlServer 实现insertOrUpdate

这两天遇到一个头疼的问题,我们系统需要请求第三方数据,第三方收到请求后会生成相应的数据并入库,我们通过定时任务将第三方数据同步到我们数据库。当我们发送请求后第三方会立即返回一个值,我们会根据返回值去数据库更新同步过来的表字段,sql语句执行完了,没有任何错误,在同步表中查看同步的数据都有且where条件完全符合,但是就是没有将指定字段更新掉,最后通过多方对比,发现更新在前,插入在后。在此,贴出最简单的解决方法:

<insert id="insertOrUpdate">

if not exists (select 1 from table_name where column_name = XX)
    insert into table_name(id, update_time) values(1, getdate())
else
    update table_name set update_time = getdate() where id = 1

</insert>

先同步或是先更新没有确定,所以如果已存在则更新否则插入

猜你喜欢

转载自www.cnblogs.com/xxjcai/p/11443976.html