有下面这样一个查询:

下面标紫色的查询条件,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. Validate Binary Search Tree——体现二查搜索树思想的一道题

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  2. booklist for machine learning

    Recommended Books Here is a list of books which I have read and feel it is worth recommending to fri ...

  3. jquery.query.js 插件(示例及简单应用) —— html之间传值

    帮助文档 var url = location.search; > "?action=view&section=info&id=123&debug&te ...

  4. Mysql学习之order by的工作原理

    在你开发应用的时候,一定会经常碰到需要根据指定的字段排序来显示结果的需求.假设你要查询城市是“杭州”的所有人名字,并且按照姓名排序返回前 1000 个人的姓名.年龄. 查询语句为: ; 全字段排序 为 ...

  5. angular.js 验证码注册登录

    css部分 header{ height: 50px; line-height: 50px; display: flex; } .callback{ text-align: left; display ...

  6. 十五oracle 触发器

    一.触发器简介 触发器的定义就是说某个条件成立的时候,触发器里面所定义的语句就会被自动的执行.因此触发器不需要人为的去调用,也不能调用.然后,触发器的触发条件其实在你定义的时候就已经设定好了.这里面需 ...

  7. BZOJ4327 [JSOI2012] 玄武密码 [AC自动机]

    题目传送门 玄武密码 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神 ...

  8. docker chown: changing ownership of '/var/lib/XXX': Permission denied

    Links: 1.entos7下docker Permission denied 2.查看 SELinux状态及关闭SELinux 方法: 1.查看SELinux状态sestatus -vgetenf ...

  9. aiohttp

    发起请求 async def fetch(): async with aiohttp.ClientSession() as session: async with session.get('https ...

  10. 【BZOJ 3133】 3133: [Baltic2013]ballmachine (线段树+倍增)

    3133: [Baltic2013]ballmachine Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 148  Solved: 66 Descri ...