在传过来的参数为1时,if 判断起作用了,但是传过来的为0时,if 判断不起作用了
<if test=" invoiceState != null and invoiceState != '' ">
and invoice_state = #{invoiceState}
if>
原因是:mybatis使用OGNL表达式来解析的,在OGNL的表达式中,'0’会被解析成字符,java是强类型的,char 和
一个string 会导致不等,所以if标签中的sql不会被解析。由于invoiceState等于0表示空字符,也就是ASCII码表里的0号字符,等同于 ‘’ 这个。所以 invoiceState !=‘’ 条件进不去
解决办法一:使用双引号
<if test=' invoiceState != null and invoiceState != "" '>
and invoice_state = #{invoiceState}
if>
解决办法二:使用toString()方法转换为字符串
<if test=" invoiceState != null and invoiceState.toString() != '' ">
and invoice_state = #{invoiceState}
if>