【mybatis】mybatis查询发生条件传入值但是查询并没有这个条件的查询,Integer类型查询条件需要注意事项
有下面这样一个查询:
下面标紫色的查询条件,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类型查询条件需要注意事项的更多相关文章
- MyBatis查询结果resultType返回值类型详细介绍
一.返回一般数据类型 比如要根据 id 属性获得数据库中的某个字段值. mapper 接口: // 根据 id 获得数据库中的 username 字段的值 String getEmpNameById( ...
- 查询中mybatis的if判断里传入0
1.传入的是long 或者 Integer类型 ,<if test="id != null "> 但是id传值为0时(前提是id对应的类型为long 或者 Intege ...
- MyBatis的参数,不能传入null
今天在调试的过程中发现一个bug,把传入的参数写到查询分析器中执行没有问题,但是在程序中执行就报错:org.springframework.jdbc.UncategorizedSQLException ...
- 如果不空null并且不是空字符串才去修改这个值,但这样写只能针对字符串(String)类型,如果是Integer类型的话就会有问题了。 int i = 0; i!=''。 mybatis中会返回tr
mybatis 参数为Integer型数据并赋值0时,有这样一个问题: mybatis.xml中有if判断条件判断参数不为空时,赋值为0的Integer参数被mybatis判断为空,因此不执行< ...
- mybatis mapper文件sql语句传入hashmap参数
1.怎样在mybatis mapper文件sql语句传入hashmap参数? 答:直接这样写map就可以 <select id="selectTeacher" paramet ...
- 记录MyBatis text类型 查询 更新 数据是null
数据库表里面存在text或者blob字段.逆向工程自动生成的MyBatis的xml中会多出几个以withBlobs结尾的方法和resultMap 此时查询数据或者更新数据的使用仍然使用selectBy ...
- [MyBatis] MyBatis理论入门
什么是MyBatis iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAOs) 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. MyB ...
- (转)MyBatis & MyBatis Plus
(二期)3.mybatis与mybatis plus [课程三]mybatis ...运用.xmind0.1MB [课程三]mybatis...机制.xmind0.2MB [课程三]mybatis与j ...
- [DB][mybatis]MyBatis mapper文件引用变量#{}与${}差异
MyBatis mapper文件引用变量#{}与${}差异 默认,使用#{}语法,MyBatis会产生PreparedStatement中.而且安全的设置PreparedStatement參数,这个过 ...
随机推荐
- PlantUML——2.中文乱码及解决
关于中文 参见: http://plantuml.sourceforge.net/unicode.html 问题描述 第一步:创建demo03.txt描述文档: @startuml Alice - ...
- POJ 1218 THE DRUNK JAILER(类开灯问题,完全平方数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2188 题目大意:n为5-100之间的一个数,代表有多少间牢房,刚开始所有房间打开,第一轮2的倍数的房间 ...
- Codeforces 375D - Tree and Queries(dfs序+莫队)
题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...
- mysql建立自增主键的插入,及自动插入当前时间
MYSQL里用这两个字段,几乎都是必须的. 以前都是自动建立的,现在手把手建立的时候,就要找资料来搞定了. 参考URL: http://blog.csdn.net/Weicleer/article/d ...
- 第六章:加载或保存JSON数据
加载或保存JSON数据 Knockout可以实现很复杂的客户端交互,但是几乎所有的web应用程序都要和服务器端交换数据(至少为了本地存储需要序列化数据),交换数据最方便的就是使用JSON格式 – 大多 ...
- SCU 4443 Range Query
二分图最大匹配,枚举. 可以计算出每一个位置可以放哪些数字,每个数字可以放在哪些位置,这样就可以建二分图了. 如果二分图最大匹配不到$n$,则无解.否则构造字典序最小的解,可以枚举每一位放什么数字,然 ...
- IntelliJ 、Pycharm、webstorm 2017 注册码及注册服务器
jetbrains 家的东西都非常好看,但是价格贵的令人发指,所以我搭建了一个 Pycharm激活服务器,可以用来激活 Pycharm,IntelliJ IDEA,WebStorm.避免频繁更换激活码 ...
- here with you
Here With You - Asher Book To all my friends对我所有好友来讲The night is young夜未央The music's loud乐未殇They pla ...
- Chris and Magic Square CodeForces - 711B
ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...
- redis_NoSql入门概述数据模型简介
以下面的背景去对比关系型数据库和非关系型数据库的差异(一个电商客户.订单.订购.地址模型来对比以下关系型数据库和非关系型数据库) 传统数据库一般设计会使用ER图(1:1/1:N/N:N,主键等) 而N ...