• Mybatis动态xml中sql语句拼接参数#和$使用


    背景

    在开发过程中一些sql语句需要在xml中进行书写,同时需要拼接一些参数,用于动态查询,例如where语句,排序字段动态排序等,涉及到了sql参数和字段注入的情况。

    使用#和$

    以上两种符号适用于参数占位作用,但是使用有一定的区别。
    #用于参数占位,例如字符串、数字型的变量参数占位,where name=#{name} 他在sql中执行语句是 where name=‘*',属于参数变量。
    $用于注入参数,一般用于字段名称的注入,例如在排序中使用 order by ${name},不能使用 order by #{name}(因为他等同于 order by '
    ’)。
    代码示例

    
    
    • 1
    <select id="getSecDataAnalysisEmsList" resultType="java.util.Map">
            select di.*,cl.categoryname,x,y,z, count(0) over() total from (
            select enterprisename,enterprisecode,sum(watervolume) watervolume,sum(electricityvolume) electricityvolume,sum(gasvolume) gasvolume FROM sec_data_input
            where 1=1
            <if test="region != null and region != ''">
                AND parkname =#{region}
            </if>
            <if test="yearvalue != null and yearvalue != ''">
                AND year =#{yearvalue}
            </if>
            <if test="monthvalue != null and monthvalue != ''">
                AND month =#{monthvalue}
            </if>
            group by enterprisename,enterprisecode
            ) di
            LEFT JOIN eam_enterprise em ON di.enterprisecode = em.enterprisecode
            LEFT JOIN oas_economic_classification ec ON em.industrycode = ec.subclass
    
            <if test="level == 0">
                left join
                oas_economic_classification cl  on cl.category=ec.category
                where cl.largecategory is null
            </if>
            <if test="level == 1">
                left join
                oas_economic_classification cl  on cl.largecategory=ec.largecategory
                where   ec.category = #{code} and cl.middlecategories is null
            </if>
            <if test="level == 2">
                left join
                oas_economic_classification cl  on cl.middlecategories=ec.middlecategories
                where   ec.largecategory = #{code} and cl.subclass is null
            </if>
            <if test="level == 3">
                left join
                oas_economic_classification cl  on cl.subclass=ec.subclass
                where  ec.middlecategories = #{code}
            </if>
            <if test="level == 4">
                left join
                oas_economic_classification cl  on cl.subclass=ec.subclass
                where  ec.subclass = #{code}
            </if>
            --注入字段名称
            order by  ${orderStr} desc
            --变量替换字段对应的值
            LIMIT #{pageSize} OFFSET #{rowIndex}
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    
    
    • 1

    【腾讯云】11.11云上盛惠,云服务器首年1.8折起,买1年送3个月!境外云服务器15元/月起,买更多省更多!
    https://cloud.tencent.com/act/cps/redirect?redirect=5559&cps_key=3e0f3ba5c4cc727403e88457eb5c4914&from=console

  • 相关阅读:
    Java Apache Commons Collection3.2.1 理解Transformer 接口
    virtio代码分析(一)-qemu部分
    【django】django面试题总结
    08 【Props 组件事件】
    机器学习第二章 感知机和支持向量机
    期末前端web大作业——我的家乡陕西介绍网页制作源码HTML+CSS+JavaScript
    【定义】行阶梯形矩阵、行最简形矩阵和标准形
    Leetcode 155. 最小栈
    RunnerGo:性能测试领域的领跑者
    Python 中的随机 IP 地址生成器
  • 原文地址:https://blog.csdn.net/m0_38004177/article/details/133064170