Mybatis中update语句的写法详解

mybatis中有很多时候是需要写到update语句的,update语句可以直接写成固定字段 也可以拼接成动态的sql

mybatis的xml更新语句中   update标签

可以直接写如下的update语句(方式一)

<update id="updateNoticeTest">
        update outbound_notice_test
        set notice_state = #{s.noticeState}, update_by = #{s.updateBy}, update_name = #{s.updateName}, update_time = #{s.updateTime}
        where id = #{s.id}
    </update>

其实就是形如:

update table set name=#{},age =#{}  where  id = #{}

也可以写成动态的更新sql(方式二)

<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio},</if>
    </set>
  where id=#{id}
</update>

<set>标签的作用:可以自动为update语句,加上set 关键字,然后对需要更新的字段,可以根据传值与否,来动态的拼接更新的字段。

同时:

在 Mybatis 中,update 语句可以使用 set 标签动态更新列。set 标签可以为 SQL 语句动态的添加 set 关键字,剔除追加到条件末尾多余的逗号。
作用:
自动在要修改的第一个字段之前添加SET关键字
去掉要修改的第一个字段前的连接符(,)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值