需求是查询状态 0 的订单,status 类型 integer 类型,可是没有查询出 status 为 0 的订单。mybatis
<if test="status!= null and status!= '' "> and status= #{status} </if>
通过排查发现 integer 类型的参数为 0 时,mybatis 会把 0 解析成 ‘’空字符串,若是不空 null 而且不是‘’空字符串才当作查询条件,但这样写只能针对字符串 String 类型。code
解决方法:xml
一、简单处理把 type != '' 去掉ip
<if test="status!= null"> and status= #{status} </if>
二、增长 or status == 0 判断字符串
<if test="status!= null and status != '' or status == 0"> and status= #{status} </if>
亲测好用,详细缘由可自行分析 mybatis 源码,在此记录下此问题。源码