• 动态SQL语句


    动态SQL语句


    if标签
    例子:
    <select id="listProduct" resultType="Product">
          select * from product
          <if test="name!=null">
                 where name like concat('%',#{name},'%')
          </if>        
    </select>

    where标签
    <where>标签会进行自动判断
    如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
    如果有任何条件成立,会自动去掉多出来的 and 或者 or。
    例子:
    <select id="listProduct" resultType="Product">
        select * from product
        <where>
            <if test="name!=null">
                and name like concat('%',#{name},'%')
            </if>             
            <if test="price!=null and price!=0">
                and price > #{price}
            </if>    
        </where>         
    </select>

    set标签
    与where标签类似,在update语句里也会碰到多个字段相关的问题
    如果任何条件都不成立,sql语句就不会出现set关键字
    如果有任何条件成立,set标签会自动去掉最后一个逗号
    <update id="updateProduct" parameterType="Product" >
            update product
            <set>
                <if test="name != null">name=#{name},</if>
                <if test="price != null">price=#{price}</if>
            </set>
             where id=#{id}   
    </update>

    trim标签
    trim 用来定制想要的功能
    trim标签可以替换where和set标签:
    prefixOverrides:前缀覆盖(去掉多余的前缀)
    <trim prefix="where" prefixOverrides="and |or ">
          ... 
    </trim>
    suffixOverrides:后缀覆盖(去掉多余的后缀)
    <trim prefix="SET" suffixOverrides=",">
      ...
    </trim>
    例子:
        <select id="listProduct" resultType="Product">
            select * from product
            <trim prefix="WHERE" prefixOverrides="AND |OR ">
                <if test="name!=null">
                    and name like concat('%',#{name},'%')
                </if>        
                <if test="price!=null and price!=0">
                    and price > #{price}
                </if>
            </trim>      
        </select>
         
        <update id="updateProduct" parameterType="Product" >
            update product
            <trim prefix="SET" suffixOverrides=",">
                <if test="name != null">name=#{name},</if>
                <if test="price != null">price=#{price}</if>
                  
            </trim>
             
             where id=#{id}   
        </update>

    choose标签:(if else的效果)
    Mybatis里面没有else标签,但是可以使用when otherwise标签来达到这样的效果。
    任何when条件成立,就进行条件查询,否则就使用otherwise条件查询
    例子:
    <select id="listProduct" resultType="Product">
          SELECT * FROM product 
          <where>
              <choose>
              <when test="name != null">
                and name like concat('%',#{name},'%')
              </when>              
              <when test="price !=null and price != 0">
                and price > #{price}
              </when>                      
                <otherwise>
                    and id >1
                </otherwise>
              </choose>
          </where>
    </select>

    foreach标签
    通常用于in 这样的语法里
    例子:
        <select id="listProduct" resultType="Product">
              select * from product where id in
                    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
                        #{item}
                    </foreach>
        </select>
    调用的时候传入一个list集合的对象为参数

    bind标签
    就像是对传入的参数做一次字符串拼接,方便后续使用
    例子:模糊查询,将传入的name前后拼接上%
            <select id="listProduct" resultType="Product">
                <bind name="likename" value="'%' + name + '%'" />
                select * from   product  where name like #{likename}
            </select>

  • 相关阅读:
    XenServer 7 上Linux单用户模式下修改密码
    python pysnmp使用
    处理一则MySQL Slave环境出现ERROR 1201 (HY000): Could not initialize master info structure的案例。
    H3C交换机端口聚合
    H3C交换机密码策略
    使用Chrome远程调试GenyMotion上的WebView程序
    CefGlue在WinXP下闪退的排查方法
    Visual Studio 2015 开发Android Cordova出现unsupported major minor version 52.0错误的解决方法
    股票涨跌预测器
    javascript模块化编程-详解立即执行函数表达式IIFE
  • 原文地址:https://www.cnblogs.com/snzd9958/p/10099028.html
Copyright © 2020-2023  润新知