• 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>
  • 相关阅读:
    jsp实现登陆功能小实验
    netty
    shiro
    mybatis
    spring MVC
    spring
    集合框架面试题
    Redis面试题
    Dubbo面试题汇总
    阿里面试题
  • 原文地址:https://www.cnblogs.com/Lxiaojiang/p/6233151.html
Copyright © 2020-2023  润新知