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>
  • 相关阅读:
    POJ-2378 Tree Cutting
    ZOJ-3870 Team Formation
    POJ-1741 Tree (树上点分治)
    POJ-3107 Godfather
    HDU-3586 Information Disturbing(树形DP+删边)
    POJ 2796 (单调栈 + 前缀和)
    POJ 3250(单调栈)
    ATCoder 116 D (思维+贪心+栈)
    POJ2528 (离散化+线段树)
    HDU 2795(思维+线段树)
  • 原文地址:https://www.cnblogs.com/Lxiaojiang/p/6233151.html
Copyright © 2011-2022 走看看