zoukankan      html  css  js  c++  java
  • Mybatis choose (when, otherwise)标签

    choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

    例如下面例子,同样把所有可以限制的条件都写上,方面使用。choose会从上到下选择一个when标签的test为true的sql执行。安全考虑,我们使用where将choose包起来,放置关键字多于错误。

    <!--  choose(判断参数) - 按顺序将实体类 User 第一个不为空的属性作为:where条件 -->  
    <select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">  
        SELECT *  
          FROM User u   
        <where>  
            <choose>  
                <when test="username !=null ">  
                    u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')  
                </when >  
                <when test="sex != null and sex != '' ">  
                    AND u.sex = #{sex, jdbcType=INTEGER}  
                </when >  
                <when test="birthday != null ">  
                    AND u.birthday = #{birthday, jdbcType=DATE}  
                </when >  
                <otherwise>  
                </otherwise>  
            </choose>  
        </where>    
    </select>  
  • 相关阅读:
    队列的定义与实现(C语言实现)
    在CTime类中重载&lt;&lt;和&gt;&gt;
    华为OJ:统计大写字母个数
    sql server smo
    应用服务器负载平衡集群
    存储过程如何执行的快速
    sql server 分布式事务
    代理服务器
    怎样实现数据库负载均衡集群
    多层插件开发框架
  • 原文地址:https://www.cnblogs.com/caoyc/p/5574948.html
Copyright © 2011-2022 走看看