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关键字
去掉要修改的第一个字段前的连接符(,)