zoukankan      html  css  js  c++  java
  • mybatis精讲(七)--动态sql

    在我们传统的开发中我们会通过拼接sql达到数据库的操作。java中的拼接不仅效率低下而且代码很长不易维护。而Mybatis通过代理模式实现SQL语句的组装。简洁易懂。

    常用标签

    元素 作用 备注
    if 判断语句 条件分支
    choose switch 多条件分支
    trim 去除空字符 特殊处理
    foreach 集合循环 遍历

    if元素

    • if元素是常用的语句,常常在where内部和test结合使用。
    • 在大部分if使用简单。比如我们在查询的时候就可以动态添加name字段。
    
    <select id="selectUser">
        select id,name,user_sex from User
        <where>
            <if test="name!=null and name!=''">
                and name like concat('%',concat(#{name},'%'))
            </if>
        </where>
    </select>
    
    
    • 上面的sql实现的效果就是如果方法中传递了name参数则以name为条件查询数据。如果没有传递name 则是全表查询数据

    choose元素

    • choose类似于java中的switch。类似的标签还有when 、 otherwise元素
    
    <select id="selectUser">
    
        select id , name , user_sex from User
        <where>
            <choose>
                <when test="name!=null and name!=''">
                    and name like concat('%',concat(#{name},'%'))
                </when>
                <when test="id!=null and id!=''">
                    and id=#{id}
                </when>
                <otherwise>
                    and user_sex=#{userSex}
                </otherwise>
            </choose>
        </where>
    
    </select>
    
    

    trim元素

    • 上面我们已经使用了一个特殊标签 where . 这个标签加上就是产生条件语句的。如果没有条件语句name这个where 就没有。
    • 关于trim 标签也可以实现类似where的作用。
    
    <select id="selectUser">
        select id , name , user_sex from User
        <trim prefix="where" prefixOverrides="and">  
            <if test="name!=null and name!=''">
                and name like concat('%',concat(#{name},'%'))
            </if>
        </trim>
    </select>
    
    
    • trim元素就意味着我们需要去掉一些特殊字符串。prefix代表的语句的前缀。而prefexOverrides代表的是你需要去掉的字符串。上面的写法基本与where是等效的。update中使用set也是和trim是等效的。

    forearch

    • forearch元素就是一个集合遍历。在我们开发中也是常用的一种数据
    
    <select id="selectUser">
    
        select * from User where user_sex in
        <forearch item="sex" index="index" collection="sexList" open="(" close="}" separator=",">
            #{sex}
        </forearch>
    
    </select>
    
    
    • collection 配置的sexList是传递过来的集合
    • item配置的是循环中当前元素
    • index 配置的当前元素在集合中下标
    • open 和 close 配置的是已什么符号将这些元素包装起来
    • separator 是各个元素的间隔符

    bind元素

    • bind元素的作用是通过OGNL表达式自定义一个上下文变量。这样更方便我们使用。在我们进行模糊查询的时候,如果是Mysql数据库,我们常常用到的是一个concat用"%"和参数连接。然而Oracle数据库则是用连接符号"||",这样SQL就需要提供两种形式实现。但是有了bind元素,我们就完全不必使用数据库语言。只需要使用Mybaits语法了。
    
    <select id="selectUser">
    
        <bind name="pattern" value="'%'+_parameter+'%'"/>
        select id , name , user_sex from User
        where name like #{pattern}
    </select>
    
    
    • 上面的_parameter代表的就是传递进来的参数,他和通配符连接后,赋给了pattern。我们就可以在select语句中使用这个变量进行模糊查询了。不管是Mysql还是Oracle数据库都可以使用这样进行查询。
  • 相关阅读:
    poj1220(短除法实现任意进制转换)
    poj1061(扩展欧基里德定理)
    快速乘+快速幂(用于模数超过int范围)
    poj3761(反序表)
    Educational Codeforces Round 64 -C(二分)
    牛客小白月赛13-H(单调栈+树状数组)
    poj3321(dfs序+树状数组)
    Codeforces Round #598 (Div. 3)E(dp路径转移)
    记忆化dp博弈
    2-sat
  • 原文地址:https://www.cnblogs.com/zhangxinhua/p/13029427.html
Copyright © 2011-2022 走看看