有下面这样一个查询:

下面标紫色的查询条件,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. 通过复制(拷贝)新建javaweb项目报错无法访问

    我们有时候为了方便,用eclipse在原来动态web项目上直接复制,粘贴项目,来形成以一个新的项目.可是运行的时候,它显示的url地址,还是原来的项目地址.初步判定,有可能是eclipse配置的问题. ...

  2. initialization 与 finalization 执行顺序 研究

    看GIF: 第二个GIF: DEMO下载:http://files.cnblogs.com/files/del88/InitOrderDemo.zip

  3. 读书笔记|Windows 调试原理学习|持续更新

    关于调试方面的学习笔记,主要来源于<软件调试>的读书笔记和梦织未来论坛的视频教程 1.调试器使用一个死循环监听调试信息. DebugActiveProcess(PID);while(TRU ...

  4. qt调用仪器驱动库dll实现程控

    在<使用qt+visa实现程控>中实现了qt调用visa库的简单Demo本文将尝试使用qt调用仪器驱动库来实现对仪器仪表的程控 开发环境 系统: windows 10 环境: qt 5.8 ...

  5. Gitlab基本管理(二)

    一. Gitlab分支 1. 切换到项目位置. 2. 创建一个项目的一新分支. mike@win10-001 MINGW64 ~/cookbook/cookbook (master) $ git br ...

  6. 在C#中使用CURL

    private string args = "";          /// <summary>         /// 参数         /// </sum ...

  7. 洛谷——P2559 [AHOI2002]哈利·波特与魔法石

    P2559 [AHOI2002]哈利·波特与魔法石 题目描述 输入输出格式 输入格式: 文件中第一行有七个数,分别是 S1. S2 . …. S7 :第二行有两个数,依次分别是起点城市 i 和终点城市 ...

  8. 磁盘镜像分析工具Autopsy

    磁盘镜像分析工具Autopsy   Autopsy是Kali Linux预安装的一款磁盘镜像分析工具.该工具可以对磁盘镜像的卷和文件系统进行分析,支持Unix和Windows系统.Autopsy是一个 ...

  9. 给出一列数a1,a2,a3....an,求它们的逆序对数,即有多少个有序对(i,j) 使得iaj,n高达10的6次方

    //归并排序 //#include<stdio.h> //#include<string.h> //#include<algorithm> //#include&l ...

  10. 基础数据类型汇总补充;集合set ;深浅copy

    首先回顾: 小数据池:int -5~256str 特殊字符,*数字20 ascii : 8位 1字节 表示1个字符unicode 32位 4个字节 表示一个字符utf- 8 1个英文 8位,1个字节 ...