mybatis if 里的坑

一:
Xml代码    收藏代码
  1. <if test="type=='y'">  
  2.     and status = 0   
  3. </if>  

当传入的type的值为y的时候,if判断内的sql也不会执行,应改为:

Xml代码    收藏代码
  1. <if test='type=="y"'>  
  2.     and status = 0   
  3. </if>  

或者改成:

<if test="type eq 'y'.toString()">
  and status = 0
</if>

 

就可以执行了,这样"y"解析出来是一个字符串,两者相等!

二:

<if test="id != null and id != '' ">
	id = #{id}
</if>

如果id类型为int 当id=0时 这个判断不会进入,这种写法只适用于 id 类型为字符串。

改为:

<if (test="id != null and id != '') or test==0"> 
  id = #{id}
</if>
或者:
<if test="id != null"> 
  id = #{id}
</if>

猜你喜欢

转载自bugyun.iteye.com/blog/2223193