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语句

相关推荐
野生技术架构师36 分钟前
一线大厂Java面试八股文全栈通关手册(含源码级详解)
java·开发语言·面试
廋到被风吹走42 分钟前
【AI】Codex 多语言实测:Python/Java/JS/SQL 效果横评
java·人工智能·python
tERS ERTS1 小时前
MySQL中查看表结构
java
坊钰1 小时前
Java 死锁问题及其解决方案
java·开发语言·数据库
于先生吖1 小时前
SpringBoot+MQTT 无人健身房智能管控系统源码实战
java·spring boot·后端
仍然.2 小时前
算法题目---模拟
java·javascript·算法
wefly20172 小时前
纯前端架构深度解析:jsontop.cn,JSON 格式化与全栈开发效率平台
java·前端·python·架构·正则表达式·json·php
nbwenren3 小时前
node.js内置模块之---crypto 模块
java
weyyhdke3 小时前
springboot和springframework版本依赖关系
java·spring boot·后端