Mybatis传参为0不进if判断条件

项目场景:

在应用构建平台应用搭建过程中,发现获取流水号接口报错


问题描述:

经过排查发现,当前端step传入值为0时,数据库中step_为NULL
SerialNumberMapper.xml

<insert id="insert" parameterType="SerialNumberPO">
		insert into sys_serial_number
		<trim prefix="(" suffix=")" suffixOverrides=",">
		    <if test="id != null and id != ''">id_,</if>
			create_time_,
			update_time_,
			<if test="code != null and code != ''">code_,</if>
			<if test="name != null and name != ''">name_,</if>
			<if test="prefix != null and prefix != ''">prefix_,</if>
			<if test="yearRule != null and yearRule != ''">year_rule_,</if>
			<if test="separator != null and separator != ''">separator_,</if>
			<if test="suffix != null and suffix != ''">suffix_,</if>
			<if test="type != null and type != ''">type_,</if>
			<if test="length != null and length != ''">length_,</if>
			<if test="current != null and current != ''">current_,</if>
			<if test="createBy != null and createBy != ''">create_by_,</if>
			<if test="updateBy != null and updateBy != ''">update_by_,</if>
			<if test="remark != null and remark != ''">remark_,</if>
			<if test="tenantId != null and tenantId != ''">tenant_id_,</if>
			<if test="step != null and step != ''">step_,</if>
			<if test="isSystem != null and isSystem != ''">is_system_,</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
		    <if test="id != null and id != ''">#{id},</if>
			sysdate(),
			sysdate(),
			<if test="code != null and code != ''">#{code},</if>
			<if test="name != null and name != ''">#{name},</if>
			<if test="prefix != null and prefix != ''">#{prefix},</if>
			<if test="yearRule != null and yearRule != ''">#{yearRule},</if>
			<if test="separator != null and separator != ''">#{separator},</if>
			<if test="suffix != null and suffix != ''">#{suffix},</if>
			<if test="type != null and type != ''">#{type},</if>
			<if test="length != null and length != ''">#{length},</if>
			<if test="current != null and current != ''">#{current},</if>
			<if test="createBy != null and createBy != ''">#{createBy},</if>
			<if test="updateBy != null and updateBy != ''">#{updateBy},</if>
			<if test="remark != null and remark != ''">#{remark},</if>
			<if test="tenantId != null and tenantId != ''">#{tenantId},</if>
			<if test="step != null and step != ''">#{step},</if>
			<if test="isSystem != null and isSystem != ''">#{isSystem},</if>
		</trim>
</insert>

原因分析:

经过测试发现,Mybatis 在判断整数0的时候会默认为空字符串,所以判断 !=’ '的条件为false 不会进入判断条件


解决方案:

  • 判断条件只需要判断为null
<if test="step != null">step_,</if>
  • 判断条件判断==0
<if test="step == 0 or (step != null and step != '')">step_,</if>

参考:

  1. https://blog.csdn.net/sea_and_sea/article/details/108314855
  2. https://www.cnblogs.com/ast935478677/p/14351414.html
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值