MyBatis中关于标签的属性判空问题(integer类型,时间日期类型Date)
<if test="id != null and id != ''">
and id = #{id}
</if>
当 id是Integer字符串类型,不要写成id != ''
,这样虽然不会报错,但是会导致数据无法修改;
当 id是String字符串类型,可以这样写"id != null and id != ''"
这样写。
- 原因:
如果属性是Integer类型的,那么当变量的值为0时,即 id= 0, MyBatis在进行 id != ''
的时候会认为 id的值是空字符串, 此时 invalid == ''
相当于为true
。
这时候只要判断 invalid != null 成立,就会执行该条语句。
<if test="id != null and true">
and id = #{id}
</if>