MyBatis choose(when, otherwise)标签
来源:互联网 发布:网络mg老虎机大奖 编辑:程序博客网 时间:2024/04/28 13:50
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包起来,放置关键字多于错误。
<!-- choose(判断参数) - 按顺序将实体类第一个不为空的属性作为:where条件 --> <choose> <when test="entity.city1 != null and entity.city1 != '' and entity.city2 != null and entity.city2 != '' and entity.city3 != null and entity.city3 != '' "> AND (FIND_IN_SET(#{entity.city1},t3.`city`) OR FIND_IN_SET(#{entity.city2},t3.`city`) OR FIND_IN_SET(#{entity.city3},t3.`city`) ) </when> <when test="entity.city1 != null and entity.city1 != '' and entity.city2 != null and entity.city2 != '' "> AND (FIND_IN_SET(#{entity.city1},t3.`city`) OR FIND_IN_SET(#{entity.city2},t3.`city`) </when> <when test="entity.city1 != null and entity.city1 != '' "> AND FIND_IN_SET(#{entity.city1},t3.`city`) </when> </choose><!--when & otherwise--><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>when元素表示当 when 中的条件满足的时候就输出其中的内容,跟 JAVA 中的 switch 效果差不多的是按照条件的顺序,当 when 中有条件满足的时候,就会跳出 choose,即所有的 when 和 otherwise 条件中,只有一个会输出,当所有的我很条件都不满足的时候就输出 otherwise 中的内容。所以上述语句的意思非常简单, 当 title!=null 的时候就输出 and titlte = #{title},不再往下判断条件,当title为空且 content!=null 的时候就输出 and content = #{content},当所有条件都不满足的时候就输出 otherwise 中的内容。
0 0
- MyBatis choose(when, otherwise)标签
- Mybatis choose (when, otherwise)标签
- Mybatis choose (when, otherwise)标签
- mabatis标签 choose (when, otherwise)
- 自定义choose,when,otherwise标签
- [c.y.j]mybatis choose when otherwise
- JSTL:--choose,when,otherwise标签实例
- <c:choose>, <c:when>, <c:otherwise> 标签
- <c:choose>, <c:when>, <c:otherwise> 标签
- jstl <c:choose></c:choose>,<c:when></c:when>和<c:otherwise></c:otherwise>标签用法
- 【c标签】 c:choose、c:when和c:otherwise标签
- JSTL标签之<c:choose><c:when><c:otherwise>标签
- JSTL标签库:choose、when、otherwise、set标签
- Mybatis最入门---动态查询(choose,when,otherwise)
- Mybatis最入门---动态查询(choose,when,otherwise)
- mybatis语句中使用choose when和otherwise
- Mybatis最入门---动态查询(choose,when,otherwise)
- mybatis学习之路----动态sql之choose when otherwise
- Test running failed: Unable to find instrumentation info for: ComponentInfo{xxxxxxx}
- User agent switcher插件百度云盘不用百度云管家下载大文件
- 深度学习算法实践12---卷积神经网络(CNN)实现
- zynq-7000学习笔记(十四)——移植openCV
- handler机制
- MyBatis choose(when, otherwise)标签
- python logging 模块 程序结束后并没有终止进程
- 电子邮件的传输过程
- 0035-mybatis相关配置
- 日期转化方法 2014/12/31 to 20141231
- centos 6.3x64 安装saltstack
- iOS获取当前所连接的WIFI名称
- Swift - Array基本使用
- (1)多态