mybatis中有这样一个SQL判断,
<if test="status != null and status !='' ">
and a.STATUS = #{status,jdbcType=SMALLINT}
</if>
status是一个Byte类型对象,当status = 1时判断成立,status=0 就不成立,按理说都应该成立的。自己试着把status !=’’ 去掉之后就都成立了。后来网上查了好久,才明白mybatis会把number类型的0当成false,非0为true,类似于JavaScript中0!=''
结果为false,从而导致=0的时候判断总是不成立。
其实只有String类型才需要判断!=''
,number类型根本不需要。
<if test="type !=null and type !=''">
AND l.type=#{type,jdbcType=INTEGER}
</if>
mybatis if test 字符串比较 是否等于 abc(使用双引号)
<if test="type !=null and type ==
"abc" ">
</if>
mybatis if test 字符串比较 是否等于 a(使用单引号)
<if test="type !=null and type ==
'a' ">
</if>
mybatis if test 字符串比较 是否等于 abc(使用toString)
<if test="type !=null and type ==
‘abc’.toString() ">
</if>