mybatis if判断Integer类型的值不等于

2019-10-22 15:12:00
admin
原创
3139
摘要:mybatis if判断Integer类型的值不等于''引发的问题(!=''等价于!=0)
当传入的person属性age的值为0时,mybatis预编译下面的语句会报错,因为预编译的sql为:update person where id = 1
<update id="update" parameterType="com.p7.demo.model.Person">
   	update person
   	<set>
   		<if test="age != ''">
   			age = #{age}
   		</if>
   	</set>
   	where id = 1
</update>
通过源码了解到,mybatis在预编译sql时,使用OGNL表达式来解析if标签,对于Integer类型属性,在判断不等于''时,例如age != '',OGNL会返回''的长度,源码:(s.length() == 0) ? 0.0 : Double.parseDouble( s ),因此表达式age != ''被当做age != 0来判断,所以当age为0时,if条件判断不通过。

发表评论
评论通过审核之后才会显示。
文章分类
联系方式
联系人: 郑州-小万
电话: 13803993919
Email: 1027060531@qq.com
QQ: 1027060531
网址: www.wanhejia.com