Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k 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 Reply

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>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...