zoukankan      html  css  js  c++  java
  • mybatis动态SQL

    1.sql片段:sql语句中重复使用的可以提取成sql片段供调用(如数据表中的字段)

    sql标签是sql片段,id是被调用时用的

    <sql id="all">     u_id,u_username,u_password,u_sex,u_createtime,u_createtime,u_delete

    </sql>

    sql片段用include标签调用

    <include refid="all"></include>

    2.where+if

    where标签会在返回的sql语句前加where再去除and或or

    <select id="selectByWhere" resultMap="BaseMap">

                  select

                  <include refid="all"></include>

                  from tb_user

                  <where>

                         <if test="uid!=0">

                                and u_id=#{uid}

                         </if>

                         <if test="username!=null and username!=''">

                                <!-- 模糊查询 -->

                                and u_username like concat('%',#{username},'%')

                         </if>

                  </where>

           </select>

    以上还用了模糊查询:用concat函数拼接%

    3.set+if

    set标签会在返回的sql前加set再去除最后的,(逗号)

    <update id="update">

                  update tb_user

                  <set>

                         <if test="username!=null and username!=''">

                                u_username=#{username},

                         </if>

                         <if test="password!=null and password!=''">

                                u_password=#{password},

                         </if>

                         <if test="usex!=null and usex!=''">

                                u_sex=#{usex},

                         </if>

                         <if test="udelete!=null and udelete!=''">

                                u_delete=#{udelete},

                         </if>

                  </set>

                  where u_id=#{uid}

           </update>

    4.trim+if:里面有属性:

    a)     prefix:前缀,在sql语句前加xxx

    b)     suffix:后缀,在sql语句后加xxx

    c)      prefixOverride:替换sql语句前的xxx

    d)     suffixOverride:替换sql语句后的xxx

    5.choose+when+otherwise

    从上往下选择,有一个符合就跳出

    <!-- 根据一个不限定的条件查询 -->

           <select id="select" resultMap="BaseMap">

                  select

                  <include refid="all"></include>

                  from tb_user

                  <where>

                         <choose>

                                <when test="uid!=0">

                                       and u_id=#{uid}

                                </when>

                                <when test="username!=null and username!=''">

                                       and u_username like concat('%',#{username},'%')

                                </when>

                                <otherwise>

                                </otherwise>

                         </choose>

                  </where>

           </select>

    6.foreach

    遍历数组或集合(根据多个id查询用in或or)

    <!-- 根据多个id查询 -->

           <select id="selectByIds" resultMap="BaseMap">

                  select

                  <include refid="all"></include>

                  from tb_user

                  <where>

                         <foreach collection="ids" open="u_id in(" close=")" item="id" separator=",">

                                #{id}

                         </foreach>

                  </where>

           </select>

  • 相关阅读:
    Java 给Word指定字符串添加批注
    Java 打印Word文档
    Java 在PDF中添加页面跳转按钮
    C# 添加、修改、删除Excel图表数据标签
    C# 添加、读取、删除Excel文档属性
    Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行
    【Luogu5348】密码解锁(莫比乌斯反演,数论)
    AtCoder Grand Contest 015
    AtCoder Grand Contest 014
    Codeforces Round #556 (Div. 1)
  • 原文地址:https://www.cnblogs.com/kfsrex/p/11439588.html
Copyright © 2011-2022 走看看