Mybatis中if标签中的整型判断问题

用mybatis进行数据修改的时候,age属性没有赋值,但是我使用update的时候age这个属性也被修改了。age属性是一个int类型。

<set>              
    <if test="name!=null">user_name=#{name},</if>             
    <if test="age!=null">age=#{age},</if>              
    <if test="remark!=null">remark=#{remark},</if>          
</set> 

发现原来这个age为int类型,默认值是0,这个时候0不为null,自然需要update。但是没有谁的年龄是0岁的。所以如果是0,肯定要被拦截。

解决办法:

<set>
	<if test="name!=null">user_name=#{name},</if>
	<if test="age!=null and age!=''">age=#{age},</if>
	<if test="remark!=null">remark=#{remark},</if>
</set>

但是这种解决办法只是对于这种0不能存在的情况下。意思是age不允许为0;

但是如果是id属性。id是主键,主键是允许为0。所以在这是id的时候需要在id为0的时候不进行拦截。

解决的办法是:
 

<set>
	<if test="name!=null">user_name=#{name},</if>
	<if test="age!=null and age!=''">age=#{age},</if>
	<if test="remark!=null">remark=#{remark},</if>
</set>
<where>
	<if test="id!=null and id!='' or id==0">id=#{id}</if>
</where>

原文地址:https://blog.csdn.net/qq_28081081/article/details/78532583

猜你喜欢

转载自blog.csdn.net/charlene717/article/details/82878042