Mybatis_choose_when

choose (when, otherwise)标签

有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。而使用if标签时,只要test中的表达式为 true,就会执行 if 标签中的条件。MyBatis 提供了 choose 元素。if标签是与(and)的关系,而 choose 是或(or)的关系。

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

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

 

via: http://www.yiibai.com/mybatis/mybatis_choose.html

<!--  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>  

choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中 的 choose 很类似。

<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
        select * from t_blog where 1 = 1 
        <choose>
            <when test="title != null">
                and title = #{title}
            </when>
            <when test="content != null">
                and content = #{content}
            </when>
            <otherwise>
                and owner = "owner1"
            </otherwise>
        </choose>
    </select>

when元素表示当 when 中的条件满足的时候就输出其中的内容,跟 JAVA 中的 switch 效果差不多的是按照条件的顺序,当 when 中有条件满足的时候,就会跳出 choose,即所有的 when 和 otherwise 条件中,只有一个会输出,当所有的我很条件都不满足的时候就输出 otherwise 中的内容。所以上述语句的意思非常简单, 当 title!=null 的时候就输出 and titlte = #{title},不再往下判断条件,当title为空且 content!=null 的时候就输出 and content = #{content},当所有条件都不满足的时候就输出 otherwise 中的内容。

### MyBatis 中 `<choose>` 和 `<when>` 的用法 在 MyBatis 的动态 SQL 功能中,`<choose>`、`<when>` 和 `<otherwise>` 是一组重要的标签组合,用于实现类似于 Java 中 `switch-case-default` 或者嵌套 `if-else` 的条件判断逻辑。以下是它们的具体功能描述以及示例: #### 基本概念 - **`<choose>`**: 表示一个条件分支的开始,类似于 `switch`。 - **`<when>`**: 定义具体的条件分支,相当于 `case` 子句。可以有多个 `<when>` 标签来表示不同的条件。 - **`<otherwise>`**: 当所有的 `<when>` 条件都不成立时,默认执行的内容。 这些标签通过属性 `test` 进行条件测试,如果某个 `<when>` 的 `test` 属性返回 true,则会执行该部分对应的 SQL 片段;如果没有匹配到任何 `<when>`,则会执行 `<otherwise>` 部分(如果有定义的话)[^1]。 #### 示例代码 下面是一个完整的例子,展示如何使用 `<choose>` 和 `<when>` 构建动态查询语句: ```xml <select id="selectOrders" resultType="Order"> SELECT * FROM orders WHERE 1=1 <choose> <!-- 如果 condition 等于 1 --> <when test="condition == 1"> AND price > #{priceThreshold} /* 只筛选价格高于阈值的记录 */ </when> <!-- 如果 condition 等于 2 --> <when test="condition == 2"> AND type = #{orderType} /* 只筛选特定类型的订单 */ </when> <!-- 如果以上条件均不满足 --> <otherwise> AND status IN ('PENDING', 'PROCESSING') /* 默认情况下只显示待处理状态的订单 */ </otherwise> </choose> </select> ``` 在这个例子中: - 参数 `condition` 被用来决定应用哪个过滤条件。 - 如果 `condition` 等于 1,则仅保留价格大于指定阈值的记录; - 如果等于 2,则仅保留某种类型的订单; - 若两者都不是,则默认选择处于某些状态下的订单。 #### 注意事项 1. `<choose>` 结构中的各个子节点必须按照顺序书写:先写若干个 `<when>`,最后再跟一个可选的 `<otherwise>`。 2. 每个 `<when>` 的 `test` 属性应该提供布尔表达式作为其值。 3. `<otherwise>` 不需要也无需带有 `test` 属性,它总是代表“其他情况”。 #### 扩展说明 虽然 `<choose>` 提供了一种简洁的方式来构建复杂的条件逻辑,但在实际开发过程中需要注意性能优化问题。例如,在设计数据库索引时应考虑到可能被频繁使用的字段是否已建立合适的索引支持高效检索操作[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值