java-int类型:int默认为0导致更新操作未赋值的情况下将值更新为0

日常开发中,做更新操作的时候的处理方法为:当这个字段有值则更新,没有值就不更新,在mybatis的xml中表现为:

<!-- 修改记录,只修改只不为空的字段 -->
    <update  parameterType="Object" >
        update tb_mj_user set 
        <trim  suffixOverrides="," >
        <if test="sort!= null" > 
            sort=#{sort},
        </if> 
        <if test="updateBy != null" > 
            update_by=#{updateBy},
        </if> 
        <if test="updateDate != null" > 
            update_date=#{updateDate},
        </if> 
        <if test="delFlag != null" > 
             del_flag = #{delFlag},
        </if> 
        </trim> 
        where id = #{id}
    </update>    

当sort字段为int类型的时候,我们做更新操作即使没有给sort设值,执行完updateBySelective操作后,sort也更新为0了。

原因是:int是java的提供8中原始数据类型之一,默认值为0,无法表达未赋值的情况。此时我们可以定义sort为Integer类型,Integer默认null,可以区分未赋值的情况,也满足updateBySelective操作的限制,不为空的时候则更新。