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

    1 基于OGNL表达式(类似jstl表达式)

    2 完成多条件查询等逻辑实现

    3 实现动态SQL的元素

    <if>

    <where>

    <set>

    <trim>

    <foreach>

    <choose>

    <when>

    <otherwise>

    4 if

    语法:

    <if test="条件"></if>

    注意:如果判断变量的类型是字符串时,要注意是否需要判断空串.

        :<if test="userName!=null and userName !=''"></if>

    5 where

    智能处理and或者or.(会去掉where后面的第一个and或者or)

    :

    select * from smbms_user

    <where>

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

    and username  like CONCAT('%',#{uName},'%')

    </if>

    <if test="uRole != null">

    and userRole = #{uRole}

     </if>

    </where>

    6 set

    智能处理update时的逗号.(会去掉set后面最后个set值的逗号)

    :

    update smbms_user

    <set>

    <if test="userCode!=null">

    userCode = #{userCode},

    </if>

    <if test="userName!=null">

      userName = #{userName},

    </if>

    <if test="userPassword!=null">

     userPassword = #{userPassword}

    </if>

                 </set>

                 where id = #{id}

    7 trim

    a.更灵活地去除多余关键字

    b.属性:

    prefix(前缀)

    suffix(后缀)

    prefixOverrides(前缀覆盖)

    suffixOverrides(后缀覆盖)

    c.替换where

    <trim prefix="where" prefixOverrides="or|and">

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

    and username  like CONCAT('%',#{uName},'%')

    </if>

    <if test="uRole != null">

    and userRole = #{uRole}

     </if>

    </trim>

    d.替换set

     update smbms_user

            <trim prefix="set" suffixOverrides=","  

             suffix="where id = #{id}">

             <if test="userCode!=null">

    userCode = #{userCode},

    </if>

    <if test="userName!=null">

      userName = #{userName},

    </if>

    <if test="userPassword!=null">

     userPassword = #{userPassword}

    </if>

            </trim>

    8 foreach

    a)迭代一个集合,通常用于in条件

    b)属性

    item  :集合参数

    index :下标

    collection:必须指定

    list  (集合)

    array (数组)

    map-key(map)

    open  :开始"("

    separator: 分割符:,

    close: 结尾:")"

    :

    <select id="getUserByRoles" resultType="User">

    select * from smbms_user  

    where  gender= #{gender} and userRole in

    <foreach collection="uRole" item="roles"

    open="(" close=")" separator=",">

    #{roles}

    </foreach>

    </select>

    9 choose

    a.相当于Javaswitch语句

    b.when有条件满足的时候,就跳出choose

    <choose>

    <when test ="条件1"> </when>

    <when test ="条件2"> </when>

    <when test ="条件3"> </when>

    <otherwise></otherwise>

    </choose>

  • 相关阅读:
    Maximum Depth of Binary Tree
    Single Number
    Merge Two Sorted Lists
    Remove Nth Node From End of List
    Remove Element
    Remove Duplicates from Sorted List
    Add Two Numbers
    编译视频直播点播平台EasyDSS数据排序使用Go 语言 slice 类型排序的实现介绍
    RTMP协议视频直播点播平台EasyDSS在Linux系统中以服务启动报错can’t evaluate field RootPath in type*struct排查
    【解决方案】5G时代RTMP推流服务器/互联网直播点播平台EasyDSS实现360°全景摄像机VR直播
  • 原文地址:https://www.cnblogs.com/yang82/p/7899898.html
Copyright © 2011-2022 走看看