关于mybatis传入0被判断为空的问题

 

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

在mybatis中这样写的话,如果是String类型的话是没有任何问题的,但是如果是传入的Integer的话就会出现传入0被判断为空的现象,但是也不是必现的,为什么不是必现的也不太清,但是如果变量的值是0,即 status= 0, mybatis在进行 status!= ‘’ 的时候会认为 status的值是空字符串, 即 status== ‘’ 为true。所以如果是Integer类型只需要判断 != null 即可。

如:

<if test="status!= null>
  status=#{status},
</if>

  

 

 

 

 注意,以上调整可能会引起另外一个细节问题----------------

1、上面的调整去掉了status!= ''的判断,如果前端没有status的查询条件但传入了status= ''的参数,在mybatis会把status=0进行查询。

 

<if test="status!= null  and status!= ''">
  status=#{status},
</if>
posted @ 2020-12-08 17:07  panchanggui  阅读(1422)  评论(0编辑  收藏  举报