Mybatis if test 动态判断数字时需要注意的问题

来源:互联网 发布:根据package.json安装 编辑:程序博客网 时间:2024/06/05 18:29

一 错误案例

mapper 代码

<if test="filter == 1">//filter类型为Integer    and r.provider_code  != #{providerCode}</if>

结果:将条件去掉,查询条件放入数据库桌面应用Navicat中查询,发现没有错误。

推想:if test动态判断数字时出现的错误。

二 例子详解(参考)

mybatis做if判断注意

  mybatis做if 判断 注意:下面这种写法只适用于 id 类型为字符串.

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

  如果id类型为int 当id=0时 这个判断不会进入.可以这样写
  

<if test="id != null and id != '' or id==0">

三 解决方案

<if test="filter != null and filter != '' or filter==1">      ...</if>
原创粉丝点击