有下面这样一个查询:

下面标紫色的查询条件,type的类型为Integer

<select
id="findDealerInfo"
parameterType="com.pisen.cloud.luna.ms.dealer.api.beans.DealerInfoBean"
resultType="com.pisen.cloud.luna.ms.dealer.api.beans.DealerInfoBean"> SELECT
dea.uid uid,
dea.enabled_flag enabledFlag,
dea.delete_flag deleteFlag,
dea.tenement_id tenementId,
dea.parent_id parentId,
pd.name parentName,
dea.name name,
dea.type type,
dea.bar_code barCode,
dea.outer_code outerCode,
dea.outer_id outerId,
dea.mne_code mneCode,
dea.address address,
dea.address_xy addressXy,
dea.business_area businessArea,
dea.business_area_xy businessAreaXy,
con.uid cantactUid,
con.name contactName,
con.mobile mobile,
dpo.uid depotUid,
dpo.name depotName,
dpo.address depotAddress,
dpo.depot_code depotCode,
dpo.ship_scope shipScope FROM
dealer AS dea
LEFT JOIN (SELECT a.* FROM contact AS a where tenement_id = #{tenementId} and main_contact = ${@com.pisen.cloud.luna.ms.dealer.base.domain.Contact@IS_MAIN} AND a.delete_flag = ${@com.pisen.cloud.luna.ms.dealer.base.common.BaseDomain@DELETE_FLAG_NO} AND a.enabled_flag = ${@com.pisen.cloud.luna.ms.dealer.base.common.BaseDomain@ENABLED_FLAG_EN}) con ON dea.uid = con.dealer_id
LEFT JOIN (SELECT b.* FROM depot AS b where b.delete_flag = ${@com.pisen.cloud.luna.ms.dealer.base.common.BaseDomain@DELETE_FLAG_NO} AND b.enabled_flag = ${@com.pisen.cloud.luna.ms.dealer.base.common.BaseDomain@ENABLED_FLAG_EN}) dpo on dpo.dealer_id = dea.uid
LEFT JOIN dealer pd on pd.uid = dea.parent_id WHERE
dea.tenement_id = #{tenementId}
AND
dea.delete_flag = ${@com.pisen.cloud.luna.ms.dealer.base.common.BaseDomain@DELETE_FLAG_NO} <if test="name != null and name != '' ">
AND
dea.name LIKE '%' #{name} '%'
</if>
<if test="type != null and type != '' ">
AND
dea.type = #{type}
</if>

<if test="outerCode != null and outerCode != '' ">
AND
dea.outer_code = #{outerCode}
</if>
<if test="mneCode != null and mneCode != '' ">
AND
dea.mne_code = #{mneCode}
</if>
<if test="address != null and address != '' ">
AND
dea.address LIKE '%' #{address} '%'
</if>
<if test="businessArea != null and businessArea != '' ">
AND
dea.business_area LIKE '%' #{businessArea} '%'
</if>
<if test="parentName != null and parentName != '' ">
AND
pd.name LIKE '%' #{parentName} '%'
</if>
<if test="contactName != null and contactName != '' ">
AND
con.name LIKE '%' #{contactName} '%'
</if>
<if test="mobile != null and mobile != '' ">
AND
con.mobile = #{mobile}
</if> order by dea.parent_id ,dea.create_date </select>

后来经过排查,真实的原因是因为:

  integer类型的查询条件在判断是否为null的时候,只需要判断 !=null即可,否则本判断条件会出现问题,正确的写法如下:

<select
id="findDealerInfo"
parameterType="com.pisen.cloud.luna.ms.dealer.api.beans.DealerInfoBean"
resultType="com.pisen.cloud.luna.ms.dealer.api.beans.DealerInfoBean"> SELECT
dea.uid uid,
dea.enabled_flag enabledFlag,
dea.delete_flag deleteFlag,
dea.tenement_id tenementId,
dea.parent_id parentId,
pd.name parentName,
dea.name name,
dea.type type,
dea.bar_code barCode,
dea.outer_code outerCode,
dea.outer_id outerId,
dea.mne_code mneCode,
dea.address address,
dea.address_xy addressXy,
dea.business_area businessArea,
dea.business_area_xy businessAreaXy,
con.uid cantactUid,
con.name contactName,
con.mobile mobile,
dpo.uid depotUid,
dpo.name depotName,
dpo.address depotAddress,
dpo.depot_code depotCode,
dpo.ship_scope shipScope FROM
dealer AS dea
LEFT JOIN (SELECT a.* FROM contact AS a where tenement_id = #{tenementId} and main_contact = ${@com.pisen.cloud.luna.ms.dealer.base.domain.Contact@IS_MAIN} AND a.delete_flag = ${@com.pisen.cloud.luna.ms.dealer.base.common.BaseDomain@DELETE_FLAG_NO} AND a.enabled_flag = ${@com.pisen.cloud.luna.ms.dealer.base.common.BaseDomain@ENABLED_FLAG_EN}) con ON dea.uid = con.dealer_id
LEFT JOIN (SELECT b.* FROM depot AS b where b.delete_flag = ${@com.pisen.cloud.luna.ms.dealer.base.common.BaseDomain@DELETE_FLAG_NO} AND b.enabled_flag = ${@com.pisen.cloud.luna.ms.dealer.base.common.BaseDomain@ENABLED_FLAG_EN}) dpo on dpo.dealer_id = dea.uid
LEFT JOIN dealer pd on pd.uid = dea.parent_id WHERE
dea.tenement_id = #{tenementId}
AND
dea.delete_flag = ${@com.pisen.cloud.luna.ms.dealer.base.common.BaseDomain@DELETE_FLAG_NO} <if test="name != null and name != '' ">
AND
dea.name LIKE '%' #{name} '%'
</if>
<if test="type != null ">
AND
dea.type = #{type}
</if>

<if test="outerCode != null and outerCode != '' ">
AND
dea.outer_code = #{outerCode}
</if>
<if test="mneCode != null and mneCode != '' ">
AND
dea.mne_code = #{mneCode}
</if>
<if test="address != null and address != '' ">
AND
dea.address LIKE '%' #{address} '%'
</if>
<if test="businessArea != null and businessArea != '' ">
AND
dea.business_area LIKE '%' #{businessArea} '%'
</if>
<if test="parentName != null and parentName != '' ">
AND
pd.name LIKE '%' #{parentName} '%'
</if>
<if test="contactName != null and contactName != '' ">
AND
con.name LIKE '%' #{contactName} '%'
</if>
<if test="mobile != null and mobile != '' ">
AND
con.mobile = #{mobile}
</if> order by dea.parent_id ,dea.create_date </select>

【mybatis】mybatis查询发生条件传入值但是查询并没有这个条件的查询,Integer类型查询条件需要注意事项的更多相关文章

  1. MyBatis查询结果resultType返回值类型详细介绍

    一.返回一般数据类型 比如要根据 id 属性获得数据库中的某个字段值. mapper 接口: // 根据 id 获得数据库中的 username 字段的值 String getEmpNameById( ...

  2. 查询中mybatis的if判断里传入0

    1.传入的是long 或者 Integer类型 ,<if test="id != null "> 但是id传值为0时(前提是id对应的类型为long 或者 Intege ...

  3. MyBatis的参数,不能传入null

    今天在调试的过程中发现一个bug,把传入的参数写到查询分析器中执行没有问题,但是在程序中执行就报错:org.springframework.jdbc.UncategorizedSQLException ...

  4. 如果不空null并且不是空字符串才去修改这个值,但这样写只能针对字符串(String)类型,如果是Integer类型的话就会有问题了。 int i = 0; i!=''。 mybatis中会返回tr

    mybatis 参数为Integer型数据并赋值0时,有这样一个问题: mybatis.xml中有if判断条件判断参数不为空时,赋值为0的Integer参数被mybatis判断为空,因此不执行< ...

  5. mybatis mapper文件sql语句传入hashmap参数

    1.怎样在mybatis mapper文件sql语句传入hashmap参数? 答:直接这样写map就可以 <select id="selectTeacher" paramet ...

  6. 记录MyBatis text类型 查询 更新 数据是null

    数据库表里面存在text或者blob字段.逆向工程自动生成的MyBatis的xml中会多出几个以withBlobs结尾的方法和resultMap 此时查询数据或者更新数据的使用仍然使用selectBy ...

  7. [MyBatis] MyBatis理论入门

    什么是MyBatis iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAOs) 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. MyB ...

  8. (转)MyBatis & MyBatis Plus

    (二期)3.mybatis与mybatis plus [课程三]mybatis ...运用.xmind0.1MB [课程三]mybatis...机制.xmind0.2MB [课程三]mybatis与j ...

  9. [DB][mybatis]MyBatis mapper文件引用变量#{}与${}差异

    MyBatis mapper文件引用变量#{}与${}差异 默认,使用#{}语法,MyBatis会产生PreparedStatement中.而且安全的设置PreparedStatement參数,这个过 ...

随机推荐

  1. 关于VS2010的一些操作

    自动插入接口实现 1: class MyClass : IMyInterface 2: { 3:   4: } .csharpcode, .csharpcode pre { font-size: sm ...

  2. 完美解决doc、docx格式word转换为Html

    http://blog.csdn.net/renzhehongyi/article/details/48767597

  3. Ubuntu16.4 zookeeper-3.4.10 单机多实例部署

    上传 zookeeper-3.4.10.tar.gz 到服务器 root@temple-:/usr/local/temple/jar# ll total drwxr-xr-x root root 8月 ...

  4. QT构造函数中*parent

    程序写的多了,你会发现几乎所有的Qt类的构造函数都会有一个parent参数.这个参数通常是QObject* 或者是 QWidget* 类型的(定义新的类是通常首先初始化为0,在类的实现函数中赋值).很 ...

  5. python定制类(1):__getitem__和slice切片

    python定制类(1):__getitem__和slice切片 1.__getitem__的简单用法: 当一个类中定义了__getitem__方法,那么它的实例对象便拥有了通过下标来索引的能力. c ...

  6. Could not apply the stored configuration for monitors

    在用户目录下$user.home/.config/monitors.xml,要解决上面的问题,最简单的办法就是删除这个monitors.xml文件,重启一下电脑

  7. AngularJS核心01:如何启动

    启动 下面解释了AngularJS是如何运行下面Html的(用一张图和一个例子来解释): 浏览器载入HTML,然后把它解析成DOM. 浏览器载入angular.js脚本. AngularJS等到DOM ...

  8. (15) go 方法

  9. OutputStreamWriter API 以及源码解读

    OutputStreamWriter是字符流与字节流之间的桥梁. 通过它写入的字符流可以通过特殊的字符集转化为字节流.这个特殊的字符集可以指定,也可以采用平台默认的字符集. 每一次调用write()方 ...

  10. Java常用工具类之自定义访问对象

    package com.wazn.learn.util; import javax.servlet.http.HttpServletRequest; /** * 自定义访问对象工具类 * * 获取对象 ...