动态SQL

作者: Yanl__ | 来源:发表于2019-12-27 08:56 被阅读0次

    动态SQL: Mybatis中可以在写mapper.xml时添加逻辑判断
    常用关键字:if、where、「choose when otherwise」、set、trim、bind、foreach、「sql include」

    if

    • 要加if的逻辑判断默认要在select * from log后加where 1=1
    • 条件写在test属性中
    <select id="selByAccinAccout" resultType="log">
        select * from log where 1=1
    <!-- OGNL 表达式,直接写key 或对象的属性.不需要添加任
    何特字符号-->
    <if test="accin!=null and accin!=''">
        and accin=#{accin}
    </if>
    <if test="accout!=null and accout!=''">
        and accout=#{accout}
    </if>
    </select>
    

    where

    1. 当编写where标签时,如果内容中第一个是and就去掉第一个and
    2. 如果<where>中有内容会生成where关键字,如果没有,则不生成
    3. 比直接使用if关键字少一个where 1=1
    <select id="selByAccinAccout" resultType="log">
        select * from log
        <where>
            <if test="accin!=null and accin!=''">
                and accin=#{accin}
            </if>
            <if test="accout!=null and accout!=''">
                and accout=#{accout}
            </if>
        </where>
    </select>
    

    choose、when

    只有一个条件成立,其他的都不执行

    <select id="selByAccinAccout" resultType="log">
        select * from log
        <where>
            <choose>
                <when test="accin!=null and accin!=''">
                    and accin=#{accin}
                </when>
                <when test="accout!=null and accout!=''">
                    and accout=#{accout}
                </when>
            </choose>
        </where>
    </select>
    

    set

    • 如果set中有内容就生成set关键字,如果没有就不生成。由于SQL中修改语句没有set的话,会报语法错误,所以默认在set下添加一个id=#{id}
    • set会去掉最后一个逗号
    <update id="upd" parameterType="log" >
        update log
        <set>
            id=#{id},
            <if test="accIn!=null and accIn!=''">
                accin=#{accIn},
            </if>
            <if test="accOut!=null and accOut!=''">
                accout=#{accOut},
            </if>
        </set>
        where id=#{id}
    </update>
    

    trim

    trim的四个属性

    1. prefix : 在前面添加内容
    2. prefixOverrides :去掉前面内容
    3. suffix : 在后面添加内容
    4. suffixOverrieds : 去掉后面内容

    执行顺序:去掉内容后添加内容

    <update id="upd" parameterType="log">
        update log
        <trim prefix="set" suffixOverrides=",">
            a=a,
        </trim>
        where id=100
    </update>
    

    bind

    作用:给参数重新赋值
    使用场景:模糊查询、在原内容前或后添加内容

    模糊查询:例如用户要查询张姓的人名,输入“张”,我们可以通过bind来替换。“张”-->“张%” or “%张%”

    <select id="selByLog" parameterType="log" resultType="log">
        <bind name="accin" value="'%'+accin+'%'"/>
            #{money}
    </select>
    

    foreach

    作用:循环参数内容,还具备在内容的前后添加内容,还具备添加分隔符功能.
    使用场景:in查询中、批量新增

    foreach的三个属性:

    1. open:循环后左侧添加的内容
    2. close:循环后右侧添加的内容
    3. separator:每次循环时,元素之间的分隔符
    <select id="selIn" parameterType="list"
    resultType="log">
        select * from log where id in
        <foreach collection="list" item="abc" open="(" close=")" separator=",">
            #{abc}
        </foreach>
    </select>
    

    sql和include

    某些sql片段,如果希望复用,可以使用<sql id=“id值”>定义这个片段的内容,使用<include refid="id值">来引用

    <sql id="mysql">
        id,accin,accout,money
    </sql>
    
    <select id="">
        select <include refid="mysql"></include>
        from log
    </select>
    

    相关文章

      网友评论

          本文标题:动态SQL

          本文链接:https://www.haomeiwen.com/subject/joxloctx.html