MyBatis 的动态 SQL 特性来实现有值字段才进行插入或更新

MyBatis 动态 SQL 解决方案

XML 复制代码
<!-- 动态INSERT语句 -->
<insert id="addDailyFreezeTer" parameterType="com.linyang.pecker.pojo.DailyFreezeTerminalBean">
    INSERT INTO dailyfreezeter_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
        checkId, terminalId, pointId, meterNo, dataDate, saveDate, stepFlag,
        <if test="day_050601FF != null">day_050601FF,</if>
        <if test="day_050602FF != null">day_050602FF,</if>
        <if test="day_050603FF != null">day_050603FF,</if>
        <if test="day_050604FF != null">day_050604FF,</if>
        <if test="day_050605FF != null">day_050605FF,</if>
        <if test="day_050606FF != null">day_050606FF,</if>
        <if test="day_050607FF != null">day_050607FF,</if>
        <if test="day_050608FF != null">day_050608FF,</if>
        <if test="day_E1008060 != null">day_E1008060,</if>
        <if test="day_E1008061 != null">day_E1008061,</if>
        <if test="day_E1008062 != null">day_E1008062,</if>
        <if test="day_E1008063 != null">day_E1008063,</if>
        <if test="day_040005FF != null">day_040005FF,</if>
        <if test="day_E1008031 != null">day_E1008031,</if>
    </trim>
    VALUES
    <trim prefix="(" suffix=")" suffixOverrides=",">
        #{checkId}, #{terminalId}, #{pointId}, #{meterNo}, #{dataDate}, #{saveDate}, #{stepFlag},
        <if test="day_050601FF != null">#{day_050601FF},</if>
        <if test="day_050602FF != null">#{day_050602FF},</if>
        <if test="day_050603FF != null">#{day_050603FF},</if>
        <if test="day_050604FF != null">#{day_050604FF},</if>
        <if test="day_050605FF != null">#{day_050605FF},</if>
        <if test="day_050606FF != null">#{day_050606FF},</if>
        <if test="day_050607FF != null">#{day_050607FF},</if>
        <if test="day_050608FF != null">#{day_050608FF},</if>
        <if test="day_E1008060 != null">#{day_E1008060},</if>
        <if test="day_E1008061 != null">#{day_E1008061},</if>
        <if test="day_E1008062 != null">#{day_E1008062},</if>
        <if test="day_E1008063 != null">#{day_E1008063},</if>
        <if test="day_040005FF != null">#{day_040005FF},</if>
        <if test="day_E1008031 != null">#{day_E1008031},</if>
    </trim>
</insert>

实现说明

动态 INSERT 语句

  • 使用<trim>标签处理括号和逗号
  • 通过<if>判断字段是否为 null,只有非 null 字段才会加入 SQL
  • suffixOverrides=","自动处理多余的逗号
XML 复制代码
<!-- 动态UPDATE语句 -->
<update id="updateDailyFreezeTer" parameterType="com.linyang.pecker.pojo.DailyFreezeTerminalBean">
    UPDATE dailyfreezeter_info
    <set>
        <if test="day_050601FF != null">day_050601FF = #{day_050601FF},</if>
        <if test="day_050602FF != null">day_050602FF = #{day_050602FF},</if>
        <if test="day_050603FF != null">day_050603FF = #{day_050603FF},</if>
        <if test="day_050604FF != null">day_050604FF = #{day_050604FF},</if>
        <if test="day_050605FF != null">day_050605FF = #{day_050605FF},</if>
        <if test="day_050606FF != null">day_050606FF = #{day_050606FF},</if>
        <if test="day_050607FF != null">day_050607FF = #{day_050607FF},</if>
        <if test="day_050608FF != null">day_050608FF = #{day_050608FF},</if>
        <if test="day_E1008060 != null">day_E1008060 = #{day_E1008060},</if>
        <if test="day_E1008061 != null">day_E1008061 = #{day_E1008061},</if>
        <if test="day_E1008062 != null">day_E1008062 = #{day_E1008062},</if>
        <if test="day_E1008063 != null">day_E1008063 = #{day_E1008063},</if>
        <if test="day_040005FF != null">day_040005FF = #{day_040005FF},</if>
        <if test="day_E1008031 != null">day_E1008031 = #{day_E1008031},</if>
        <if test="dataDate != null">dataDate = #{dataDate},</if>
        <if test="saveDate != null">saveDate = #{saveDate},</if>
        <if test="stepFlag != null">stepFlag = #{stepFlag},</if>
    </set>
    WHERE checkId = #{checkId}  <!-- 根据实际主键调整 -->
</update>

动态 UPDATE 语句

  • 使用<set>标签自动处理 SET 关键字和逗号
  • 同样通过<if>判断字段是否为 null
  • 需要确保 WHERE 子句使用正确的主键或唯一标识

注意事项

  • 对于必须字段(如 checkId, terminalId 等),建议在 Java 代码中确保其非空
  • 如果字段类型是基本数据类型(如 int, long),需要改用包装类(Integer, Long)才能判断 null
  • 对于日期类型字段,可能需要额外处理空字符串或默认值的情况
相关推荐
没有bug.的程序员2 分钟前
《Spring Boot应用工程化提升:多模块、脚手架与DevTools》
java·运维·spring boot
Derek_Smart28 分钟前
工业级TCP客户端高可靠连接架构设计与Netty优化实践
java·性能优化·netty
长安城没有风33 分钟前
从 0 到 1 认识 Spring MVC:核心思想与基本用法(上)
java·spring boot·spring·java-ee·mvc
许野平44 分钟前
Rust 同步方式访问 REST API 的完整指南
java·网络·rust·restful
设计师小聂!1 小时前
力扣热题100--------240.搜索二维矩阵
java·算法·leetcode·矩阵
狒狒的代码王国1 小时前
正则表达式
java
aningxiaoxixi1 小时前
安卓audio 架构解析
java·架构·ffmpeg
都叫我大帅哥2 小时前
Java OpenFeign:微服务通信的“魔法契约书”
java·spring boot·spring cloud
Full Stack Developme3 小时前
Java 日期时间处理:分类、用途与性能分析
java·开发语言·数据库
AA-代码批发V哥3 小时前
MyBatisPlus之CRUD接口(IService与BaseMapper)
mybatis