有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。而使用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包起来,放置关键字多于错误。 <!--  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 中的内容。

choose ,when ,otherwise相关推荐

  1. jstl c:choose、c:when和c:otherwise标签

    在用spring mvc中,页面前端老用jstl,记录一下. <c:choose>.<c:when>和<c:otherwise>在一起连用,可以实现Java语言中的 ...

  2. 聊聊RibbonLoadBalancerClient的choose方法

    为什么80%的码农都做不了架构师?>>>    序 本文主要研究一下RibbonLoadBalancerClient的choose方法 RibbonLoadBalancerClien ...

  3. mssql 查询当前自增序号_查询函数Choose、Lookup、Hlookup、Vlookup应用技巧解读

    Excel中的查找和引用函数主要用于查找工作表中的所需内容,还可以获得工作表中的单元格位置或表格大小等信息,如果将查找和引用函数配合其他的Excel函数使用,将会发挥更强大的功能.常用的查询表中的数据 ...

  4. What are HANA's models of cloud computing, and which should I choose?

    What are HANA's models of cloud computing, and which should I choose? http://searchsap.techtarget.co ...

  5. We can all choose freedom over a job

    We can all choose freedom over a job By Luke Johnson  人人都能创业? Can everyone be an entrepreneur? At fi ...

  6. 安装android studio出现choose an account with administrator

    安装android studio出现choose an account with administrator Windows安装时候会出现的问题 确保你是管理员账号登录,并且拥有管理员权限 确保安装包 ...

  7. 【错误记录】FFmpeg 推流报错 ( FLV does not support sample rate 8000, choose from (44100, 22050, 11025) )

    文章目录 一.报错信息 二.解决方案 一.报错信息 在 Android 平台中 , 先使用 MediaRecorder 将从 麦克风 采集到的 音频压缩到文件中 , 使用 交叉编译的 FFmpeg 可 ...

  8. mybaits十六:使用choose标签实现分支选择

    <!-- 分支选择<choose><when></when><otherwise></otherwise></choose> ...

  9. Choose unique values for the 'webAppRootKey' context-param in your web.xml files!

    报错日志 tomcat里面跑了多个项目,启动时报如下错误: 严重: The web application [/drc] registered the JDBC driver [com.mysql.j ...

  10. machine learning (6)---how to choose features, polynomial regression

    how to choose features, polynomial regression:通过定义更适合我们的feature,选择更好的模型,使我们的曲线与数据更好的拟合(而不仅仅是一条直线) 可以 ...

最新文章

  1. 《Apache Common CLI官方文档
  2. java中jar打包的方法
  3. Swift开发:仿Clear手势操作(拖拽、划动、捏合)UITableView
  4. 结构体为什么要4字节对齐
  5. java 多线程(Callable,Future)
  6. Pytorch预训练模型下载并加载(以VGG为例)自定义路径
  7. 【深入Java虚拟机】之一:Java内存区域与内存溢出
  8. ups一直响是什么原因_UPS的完整形式是什么?
  9. 学会这些 Python 美图技巧,就等着女朋友夸你吧!| 原力计划
  10. 循环渐进NsDoor(三)
  11. 1.thinkphp6配置文件详解
  12. java jettison_java – 使用Jettison进行JSON解析
  13. 数据结构之SWUSTOJ1038: 顺序表中重复数据的删除
  14. 【Linux】swp文件
  15. 带你了解微信代运营公众号到底怎么做
  16. 意大利或将立法 禁止中小学校园携带手机
  17. python获取模块的名称_Python获取模块名称
  18. chrome 未连接到互联网 代理服务器出现问题,或者地址有误
  19. 机器学习——线性模型之Softmax回归
  20. 修复 重装 远程桌面工具(mstsc.exe)完美解决方案

热门文章

  1. PCIe to CAN Linux 驱动详细说明
  2. 清华某面霸收集的世界500强公司面试题目和点评
  3. python基础编程:jenkins配置python脚本定时任务过程图解
  4. 使用JDK自带keytool生成证书
  5. 微软“咸鱼翻身”:股价沉沦17年后创历史新高
  6. java中di,初识Spring的DI及其基本用法
  7. 隐形降权是什么, 什么行为会导致店铺被隐形降权,如何避免降权
  8. URAL - Questions(约瑟夫环)
  9. zcmu-1133: 第九章:致我们终将逝去的青春(dfs)
  10. QSS 自定义QMessageBox