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>

  • 相关阅读:
    Centos 实现SSH证书登录
    Spring Boot 拦截器的使用
    Spring Boot 内置定时器的使用
    Spring Boot 整合Druid数据源(一)
    Spring Boot 整合滑动验证
    asp.ent core swagger
    Dapper.Contrib
    SonarQube
    Endpoint.Binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport)
    wcf messageEncoding="Mtom"
  • 原文地址:https://www.cnblogs.com/yang82/p/7899898.html
Copyright © 2011-2022 走看看