Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

mybatis mysql 如何实现upsert功能?

1、数据不存在insert;
2、数据存在update;
3、表中有一个唯一约束;并根据该约束执行具体的插入或者修改操作。

————————————————————————————

insert into T_name (uid, app_id,createTime,modifyTime) 
values(111, 1000000,'2017-03-07 10:19:12','2017-03-07 10:19:12') 
on duplicate key update uid=111, app_id=1000000, createTime='2017-03-07 10:19:12',modifyTime='2017-05-07 10:19:12'

如何把上边的sql,用mybatis改写?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<insert id="upsert" parameterType="Model">
    insert into T_name
        (uid, name, age, balance) values (100,"yangyang", 23, 100000000)
    on duplicate key update balance=balance + 100
</insert>

以上代码,自己测试可以的。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...