在开发过程中曾遇到一些if标签判断不生效的情况,一般分为两种
1.判断条件为单个字符,如:
<if test=" type =='1'">
and template_number ='1'
</if>
发现用不了,上网查了一下原来是因为mybatis会将'1'解析为字符,而java是强类型的,字符和传入的字符串必然不相等,所以判断永远失效。
解决办法:
1.改为用双引号表示字符串
<if test=' type =="1" '>
and template_number ='1'
</if>
2.在字符后面加个.toString
<if test=" type =='1'.toString()">
and template_number ='1'
</if>
2.传入数字类型0,如:
<if test="status!=null and status!=''">
and f.status =#{status}
</if>
如果此时传入的status为0的话,mybatis会把 ' '和int类型的0都转化成double的0.0进行比较,就导致了虽然我传入的是0,但是实际上判断的时候会等价于 ' ' ,所以if标签会失效
解决办法:
把0转化为String类型再传入mapper