search

Home  >  Q&A  >  body text

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

阿神阿神2802 days ago2036

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:50:33

    <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.

    reply
    0
  • 阿神

    阿神2017-04-18 10:50:33

    <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

    reply
    0
  • Cancelreply