mybatis xml trim choose when 用法

最近遇到一个问题,需要sql 类似:

select * where  xx = 'xx'  and ( xx='xx' or xx='xx') order by id asc

使用方法1:

  可以去掉 前面 ” and (“     和后面的 ”)“

<trim prefix="AND(" suffix=")" prefixoverride="AND|OR">
    <if test="name != null and name.length()>0"> AND name=#{name}</if>
    <if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>
</trim>



<trim prefix="AND(" suffix=")" prefixoverride="AND|OR"> 

使用方法2:

SELECT cb_p FROM xxx WHERE  app_source = #{appsource}  AND channel = #{channel} AND model = #{model} AND (

        <choose>
            <when test=" (imei != null and imei != '' ) or (androidid != null and androidid != '') or (idfa != null and idfa != '' )  or (oaid != null and oaid != '') or (mac != null and mac != '')">
                <trim prefix="" prefixOverrides ="OR"  >
                    <if test="imei != null and imei != ''">
                        OR  imei = #{imei}
                    </if>
                    <if test="androidid != null and androidid != ''  ">
                        OR  android_id = #{androidid}
                    </if>
                    <if test="idfa != null and idfa != '' ">
                        OR  idfa = #{idfa}
                    </if>
                    <if test="oaid != null and oaid != '' ">
                        OR  oaid = #{oaid}
                    </if>
                    <if test="mac != null and mac != '' ">
                        OR  mac = #{mac}
                    </if>

                </trim>

            </when>
            <otherwise>
                 uip = #{ip} and  uua = #{uua}
            </otherwise>

        </choose>

            ) ORDER BY ts  DESC LIMIT 1

  

原文地址:https://www.cnblogs.com/chen-msg/p/15412145.html