一、需求

后台使用orcale数据库,mybatis做持久层,前台搜索功能,根据类型搜索,但是数据库中没有类型字段,

所以需要在where条件语句中进行判断,当type == x1 时和type == x2时where中的判断条件不同

二、解决

<select id = "" resultMap = "">

select * from table

<where>

<if test="type == 'x1' ">

and  条件1;

</if>

<if test="type == 'x2' ">

and  条件2;

</if>

</where>

</select>

或者

<select id = "" resultMap = "">

select * from table

<choose>

<when test=" type == 'x1' '">

where   条件1;

</when >

<when test=" type == 'x2' '">

where  条件2;

</when >

<otherwise>

条件3;   // 可以为空

</otherwise>

</choose>

<if test="type == 'x2' ">   //如果除了以上条件外还有判断的条件,放在chose标签外,不用再写where

and  条件2;

</if>

</select>

再或者

<select id = "" resultMap = "">

select * from table

<where>

<choose>

<when test=" type == 'x1' '">

条件1;

</when >

<when test=" type == 'x2' '">

条件2;

</when >

<otherwise>

条件3;   // 可以为空

</otherwise>

</choose>

<if test="type == 'x2' ">   //如果除了以上条件外还有判断的条件,放在chose标签外,不用再写where

and  条件2;

</if>

</where>

</select>

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

mybatis使用<choose> <when>的更多相关文章

  1. MyBatis:choose标签的用法

    <!-- 4.2 choose用法 需求: 在已有的sys_user表中,除了主键id外,我们认为user_name也是唯一的, 所有的用户名都不可以重复.现在进行如下查询:当参数id有值的时候 ...

  2. mybatis xml <choose>标签使用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  3. Mybatis的choose when otherwise

    <select id="getCount" resultType="int"> select count(1) from <choose> ...

  4. mybatis:choose when otherwise标签

    choose标签是按顺序判断其内部when标签中的test条件是否成立,如果有一个成立,则 choose 结束. 当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的 ...

  5. MyBatis中choose when正确写法

    <choose> <when test="scoreRange!=null and scoreRange eq 1"> AND sc.score <! ...

  6. mybatis的<choose>和<when>、<otherwise>标签

    SELECT<choose> <when test='timeType=="yy"'> TO_CHAR(REPORT_TIME,'yyyy') </w ...

  7. mybatis框架-choose when otherwise 的使用

    需求:模拟实际业务情况,传入多条件进行查询 /** * 需求:模拟实际业务,用户传入多个条件,进行用户列表信息的查询 * @param roleids * @return */ public List ...

  8. 深入浅出Mybatis系列(九)---强大的动态SQL

    上篇文章<深入浅出Mybatis系列(八)---mapper映射文件配置之select.resultMap>简单介绍了mybatis的查询,至此,CRUD都已讲完.本文将介绍mybatis ...

  9. MyBatis动态SQL语法

    [注:摘自MyBatis官网 ] 1.动态SQL的元素: if choose (when, otherwise) trim (where, set) foreach bind 2.if语句:   &l ...

随机推荐

  1. Myeclipse--jBPM4.3插件

    http://www.baidupcs.com/file/c7f3b8fc57b056567b37d081b1bcd21e?xcode=3966699596a0e8ec88581bd8407457f9 ...

  2. python虚拟环境 | virtualenv 的简单使用 (图文)

    一.创建virtualenv虚拟环境 mkvirtualenv -p 版本号 虚拟名 mkvirtualenv -p python3 env_1 python3:版本号 env_1: 虚拟环境名称 创 ...

  3. Spring-IOC实现【02-其他实现方式】

    接上文Spring-IOC实现[01-XML配置方式] Java配置方式 SpringBoot流行之后,Java 配置开始被广泛使用. Java配置本质上,就是使用一个Java类去代替xml配置,这种 ...

  4. [Noip2015PJ] 求和

    Description 一条狭长的纸带被均匀划分出了 \(n\) 个格子,格子编号从 \(1\) 到 \(n\) .每个格子上都染了一种颜色 \(color_i\) 用 \([1,m]\) 当中的一个 ...

  5. CentOS7下查看系统环境(内存CPU磁盘使用率)

    1.方法一 yum install atop --安装atop sudo atop--开启监视 2.方法二 top 3.方法三 free --查看没存情况 ps ux --查看CPU 情况 磁盘 df

  6. IdentityServer4-客户端定义-翻译

    客户端定义(Defining Client) 客户端可以从你的IDS服务器请求tokens. 通常,客户端需要遵循下面的通用设置: 一个唯一的Client ID 如果需要还可以提供密码 允许与toke ...

  7. 【Java深入研究】9、HashMap源码解析(jdk 1.8)

    一.HashMap概述 HashMap是常用的Java集合之一,是基于哈希表的Map接口的实现.与HashTable主要区别为不支持同步和允许null作为key和value.由于HashMap不是线程 ...

  8. MVCmoduleExample.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 网络编程学习二(IP与端口)

    InetAddress类 封装计算机的ip地址,没有端口 // 使用getLocalHost方法创建InetAddress对象 InetAddress addr = InetAddress.getLo ...

  10. 2017-10-10 都市传说: "部分"中文出现乱码

    知乎原链, 作者亦本人 事情起源于项目另一开发者在中文Windows下构建时遇到的部分中文出现乱码问题. 当时很不解的是, 为什么会只有部分出现乱码. 第一感觉是, 如果编码转换不正确, 要么全乱码, ...