zoukankan      html  css  js  c++  java
  • MyBatis的 or 和and 问题

    今天排查一个mybatis查询的问题,用的动态sql语句结果发现个问题。在mybatis中  or 的位置不同也会影响查询结果。上代码:

    这是有问题的代码

        <select id="queryComissionRecord" resultType="ComissionRecordResult">
            select
            sum(t.amount) as transferAmount,
            t.agentName as agentName,
            t.tillNumber as
            tillNumber,
            t.agentRealName as realName,
            count(*) as transactionNumber,
            sum(t.comission) as
            agentComissionAmount,
            sum(t.parentComission) as
            superAgentComissionAmount
    
            from comission_record t
    
            where
            1=1
            <if test="name != null and name != ''">
                and t.agentName=#{name} or t.agentRealName=#{name}
            </if>
            <if test="tillNumber != null and tillNumber != ''">
                and
                t.tillNumber=#{tillNumber}
            </if>
            and t.parentTillNumber=#{supAgentTillNumber}
            and DATE_FORMAT(
            startTime, '%Y%m' ) = DATE_FORMAT(#{month} , '%Y%m' )
            and t.status in (0)
    
            group by agentName
            order by tillNumber desc
    
        </select>

    这是没问题的代码1

        <select id="queryComissionRecord" resultType="ComissionRecordResult">
            select
            sum(t.amount) as transferAmount,
            t.agentName as agentName,
            t.tillNumber as
            tillNumber,
            t.agentRealName as realName,
            count(*) as transactionNumber,
            sum(t.comission) as
            agentComissionAmount,
            sum(t.parentComission) as
            superAgentComissionAmount
    
            from comission_record t
    
            where
            1=1
            <if test="tillNumber != null and tillNumber != ''">
                and
                t.tillNumber=#{tillNumber}
            </if>
            and t.parentTillNumber=#{supAgentTillNumber}
            and DATE_FORMAT(
            startTime, '%Y%m' ) = DATE_FORMAT(#{month} , '%Y%m' )
            and t.status in (0)
            <if test="name != null and name != ''">
                and t.agentName=#{name} or t.agentRealName=#{name}
            </if>
            group by agentName
            order by tillNumber desc
    
        </select>

    没问题代码2

        <select id="queryComissionRecord" resultType="ComissionRecordResult">
            select
            sum(t.amount) as transferAmount,
            t.agentName as agentName,
            t.tillNumber as
            tillNumber,
            t.agentRealName as realName,
            count(*) as transactionNumber,
            sum(t.comission) as
            agentComissionAmount,
            sum(t.parentComission) as
            superAgentComissionAmount
    
            from comission_record t
    
            where
            1=1
            <if test="name != null and name != ''">
                and (t.agentName=#{name} or t.agentRealName=#{name})
            </if>
            <if test="tillNumber != null and tillNumber != ''">
                and
                t.tillNumber=#{tillNumber}
            </if>
            and t.parentTillNumber=#{supAgentTillNumber}
            and DATE_FORMAT(
            startTime, '%Y%m' ) = DATE_FORMAT(#{month} , '%Y%m' )
            and t.status in (0)
            group by agentName
            order by tillNumber desc
    
        </select>

    两份代码的不同之处就在于,下划线部分的代码的位置不一样,如果放在前面,一旦name不为空的时候,条件开启,or 就会把后面所有的条件都当成or的一部分。结果会有很大差异。

    或者也可以和第三种一样,括号优先一下。

    在此小记一下,也帮助新手填坑吧。

    原创博文,未经许可不得转载,转载请注明出处。
  • 相关阅读:
    Vue 使用百度地图 实现搜索 定位
    VUE npm run dev 启动时,报了一大堆错误 Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 7.x
    git 更换push 提交地址
    vue 拖拽框架 draggable
    VUE axios请求 封装 get post Http
    关于git 远程仓库账户密码错误的问题
    输入交互(一)
    8.实战交付一套dubbo微服务到k8s集群(1)之Zookeeper部署
    7.kubernetes集群版本升级
    6.kubernetes的GUI资源管理插件-dashboard
  • 原文地址:https://www.cnblogs.com/Think-007/p/7566988.html
Copyright © 2011-2022 走看看