需求:将mysql中类型为timestamp的日期时间赋值为空,代表没有启动过。

mybatis:

<update id="update">
    UPDATE dev
        <set>
            <if test="devNo != null">`dev_no` = #{devNo}, </if>
            
            <!-- 如果有传入有时间,则正常赋值 -->
            <if test="startDate != null and startDate != ''">`start_date` = #{startDate}, </if>
            <!-- 如果传入时间为空,则赋NULL值 -->
            <if test="startDate != null and startDate == ''">`start_date` = NULL, </if>
        </set>
    WHERE
        id = #{id}
</update>

前端页面用的laydate,有个清空按钮,作用清空当前日期框的时间,点击保存,会把  '' (双单引号)值传给mybatis,引起报错。在mybatis中使用

<if test="startDate != null and startDate == ''">`start_date` = NULL </if>

给日期时间赋空值

Logo

更多推荐