java - mybatis mysql 如何实现upsert功能?
阿神
阿神 2017-04-18 10:48:33
0
2
2029

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改写?

阿神
阿神

闭关修行中......

reply all(2)
左手右手慢动作
<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>

You can test the above code by yourself.

阿神

<insert id="upsert" parameterType="Model">

insert into T_name
    (uid, name, age, balance) values (#{uid,jdbcType=VARCHAR},#{name}, 23, 100000000)
on duplicate key update balance=balance + 100

</insert>

Use #{} in my mybatis to get the vo attribute value

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template