一、需求

后台使用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. leetcode — generate-parentheses

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  2. 源码编译安装MySQL8.0

    源码编译安装MySQL8.0 0.前期准备条件 查看linux的版本 [root@mysql etc]# cat /etc/redhat-release CentOS Linux release 7. ...

  3. 图解Docker容器和镜像

    图解Docker容器和镜像 这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(container)和镜像(image)之间的区别,并深入探讨容器和运行中的容器之间的区别. 当我对Docke ...

  4. 菜鸟入门【ASP.NET Core】11:应用Jwtbearer Authentication、生成jwt token

    准备工作 用VSCode新建webapi项目JwtAuthSample,并打开所在文件夹项目 dotnet new webapi --name JwtAuthSample 编辑JwtAuthSampl ...

  5. [PHP] 数据结构-二叉树的创建PHP实现

    1.利用递归的原理,只不过在原来打印结点的地方,改成了生成结点,给结点赋值的操作if(ch=='#'){*T=NULL;}else{malloc();(*T)->data=ch;createFu ...

  6. Intellij Idea乱码解决方案

    使用Intellij Idea经常遇到的三种乱码问题: 1.工程代码乱码 2.main方法运行,控制台乱码 3.tomcat运行,控制台乱码 解决方案: 1.工程代码乱码 Settings > ...

  7. 将javaWeb项目转maven项目

    不经常做此类转换,所以总是忘记转换方法,特此,记录下转换步骤 1.首先从SVN检出项目 2.找到导出项目路径 3.按住Shift+鼠标右键,打开控制台 3.输入命令mvn eclipse:eclips ...

  8. Bell(hdu4767+矩阵+中国剩余定理+bell数+Stirling数+欧几里德)

    Bell Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  9. ngx-echart地图

    一.运行截图 二.代码 html代码: <div style="padding:24px;"> <p style="font-size: 16px;ma ...

  10. jQuery点击页面其他部分隐藏下拉菜单

    一.开发小要点 web页面中,我们一般不用select.option来实现下拉菜单效果,因为下拉框的样式丑且难以美化,所以我们选择控制ul显示隐藏来实现同样且高大上的效果,但是不能像下拉框那样点击页面 ...