Mybatis的set标签,动态SQL

set标签常用于update语句中,搭配if标签使用

set标签的作用

1、会动态加上前置set关键字

2、可以删除无关的逗号

示例代码:

sql 复制代码
    <update id="update">
        update employee
        <set>
            <if test="name != null">name = #{name},</if>
            <if test="username != null">username = #{username},</if>
            <if test="password != null">password = #{password},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="idNumber != null">id_number = #{idNumber},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateUser != null">update_User = #{updateUser},</if>
            <if test="status != null">status = #{status},</if>
        </set>
        where id = #{id}
    </update>

set标签和if标签的搭配就可以做到有效适应不同的SQL语句

相关推荐
AM越.1 小时前
Java设计模式详解--装饰器设计模式(含uml图)
java·设计模式·uml
5980354152 小时前
【java工具类】小数、整数转中文大写
android·java·开发语言
JIngJaneIL2 小时前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
吃喝不愁霸王餐APP开发者2 小时前
Java后端服务在对接全国性霸王餐API时的多数据中心部署与就近调用策略
java·开发语言
从心归零2 小时前
springboot-jpa的批量更新方法
java·spring boot·spring
这周也會开心2 小时前
128陷阱,==与equals区别
java·开发语言
Hello.Reader3 小时前
Flink Process Table Functions(PTF)实战详解:把 SQL 变成“可编程算子”,状态、时间、定时器一把梭
网络·sql·flink
TAEHENGV3 小时前
回收站模块 Cordova 与 OpenHarmony 混合开发实战
android·java·harmonyos
a努力。3 小时前
宇树Java面试被问:方法区、元空间的区别和演进
java·后端·面试·宇树科技
2501_916766543 小时前
【面试题1】128陷阱、==和equals的区别
java·开发语言