项目场景:
在应用构建平台应用搭建过程中,发现获取流水号接口报错
问题描述:
经过排查发现,当前端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>
参考: