mybatis中常见的动态SQL标签

在xml中写动态SQL的的时候,有一些常见的,如if 、foreach

复制代码
        SELECT a.*,c.product_name
        FROM work_order a LEFT JOIN product c ON a.product_code = c.product_code AND c.del_flag = 0
        where a.del_flag = 0 
        <if test="orderQueryReq.productCode != null and orderQueryReq.productCode != ''">
            AND a.product_code like concat('%',#{orderQueryReq.productCode},'%')
        </if>
        <if test="orderQueryReq.productName != null and orderQueryReq.productName != ''">
            AND c.product_Name like concat('%',#{orderQueryReq.productName},'%')
        </if>
        <if test="orderQueryReq.orderStatus != null">
            AND a.order_status = #{orderQueryReq.orderStatus}
        </if>
        <if test="orderQueryReq.auditStatus != null">
            AND a.audit_status = #{orderQueryReq.auditStatus}
        </if>
        <if test="orderQueryReq.startTimeStart != null and orderQueryReq.startTimeStart != ''">
            AND a.start_time <![CDATA[ >= ]]> #{orderQueryReq.startTimeStart}
        </if>
        <if test="orderQueryReq.startTimeEnd != null and orderQueryReq.startTimeEnd != ''">
            AND a.start_time <![CDATA[ <= ]]> #{orderQueryReq.startTimeEnd}
        </if>
        <if test="orderQueryReq.endTimeStart != null and orderQueryReq.endTimeStart != ''">
            AND a.end_time <![CDATA[ >= ]]> #{orderQueryReq.endTimeStart}
        </if>
        <if test="orderQueryReq.endTimeEnd != null and orderQueryReq.endTimeEnd != ''">
            AND a.end_time <![CDATA[ <= ]]> #{orderQueryReq.endTimeEnd}
        </if>
        <if test="orderQueryReq.orderIds != null and orderQueryReq.orderIds.size() > 0">
            AND a.id IN
            <foreach collection="orderQueryReq.orderIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ORDER BY field(a.audit_status, 10,30,20) , field(a.order_status,30,20,10,40) ,a.start_time ASC
复制代码

如上涉及到if标签内,如果为string的时候判断,Integer时候判断,时间字段的判断,遍历集合时候写法

order by如果不是按照某一个字段规律的顺序来的,可以使用field关键字,里面第一项填写的是属性字段,后面就按照想排序的值进行排序,也就不用填desc和asc

 

posted @   多多指教~  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示