zoukankan      html  css  js  c++  java
  • 动态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>

  • 相关阅读:
    003 01 Android 零基础入门 01 Java基础语法 01 Java初识 03 Java程序的执行流程
    002 01 Android 零基础入门 01 Java基础语法 01 Java初识 02 Java简介
    001 01 Android 零基础入门 01 Java基础语法 01 Java初识 01 导学
    001 Android Studio 首次编译执行项目过程中遇到的几个常见问题
    Dora.Interception,为.NET Core度身打造的AOP框架 [2]:以约定的方式定义拦截器
    Dora.Interception,为.NET Core度身打造的AOP框架 [1]:更加简练的编程体验
    监视EntityFramework中的sql流转你需要知道的三种方式Log,SqlServerProfile, EFProfile
    轻量级ORM框架——第二篇:Dapper中的一些复杂操作和inner join应该注意的坑
    轻量级ORM框架——第一篇:Dapper快速学习
    CF888G Xor-MST(异或生成树模板)
  • 原文地址:https://www.cnblogs.com/snzd9958/p/10099028.html
Copyright © 2011-2022 走看看