• mybatis中if判断为0无效


    问题描述

    下面sql语句中,level字段传入0的时候,插入和修改都会失败。

    	<update id="updateDept" parameterType="SysDept">
     		update sys_dept
     		<set>
     			<if test="parentId != null and parentId != 0">parent_id = #{parentId},if>
     			<if test="deptName != null and deptName != ''">dept_name = #{deptName},if>
     			<if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},if>
     			<if test="orderNum != null">order_num = #{orderNum},if>
     			<if test="leader != null">leader = #{leader},if>
     			<if test="phone != null">phone = #{phone},if>
     			<if test="email != null">email = #{email},if>
     			<if test="status != null and status != ''">status = #{status},if>
     			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},if>
    			<if test="level != null and level != ''">level = #{level},if>
     			update_time = sysdate()
     		set>
     		where dept_id = #{deptId}
    	update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    原因与方案

    mybatis中传入int类型的0时,会当成字符处理,即当成""空字符串处理。如果要插入0 ,把后面level != ''删掉即可,只留下面面的判断。
    修改后的level字段如下:

    	<update id="updateDept" parameterType="SysDept">
     		update sys_dept
     		<set>
     			<if test="parentId != null and parentId != 0">parent_id = #{parentId},if>
     			<if test="deptName != null and deptName != ''">dept_name = #{deptName},if>
     			<if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},if>
     			<if test="orderNum != null">order_num = #{orderNum},if>
     			<if test="leader != null">leader = #{leader},if>
     			<if test="phone != null">phone = #{phone},if>
     			<if test="email != null">email = #{email},if>
     			<if test="status != null and status != ''">status = #{status},if>
     			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},if>
    			<if test="level != null">level = #{level},if>
     			update_time = sysdate()
     		set>
     		where dept_id = #{deptId}
    	update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    Dubbo-聊聊通信模块设计
    Python基础——魔法方法(一)
    js实现页面元素的拖拽
    NCCL源码解析③:机器内拓扑分析
    可编程 USB 转串口适配器开发板 SHT3x-DIS 温湿度传感器芯片
    基于Matlab使用激光雷达检测分类跟踪车辆仿真(附源码)
    c语言中常见的数学函数
    @Configuration @Bean(SpringBoot的配置类)
    代码随想录动态规划——最长回文子序列
    如何将pdf转word?这几个软件可以做到文档格式转换
  • 原文地址:https://blog.csdn.net/ITzhongzi/article/details/126320777