一、需求

后台使用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. ETCD&Flannel安装

    .ETCD 安装: nohup etcd --name etcd0 \ --advertise-client-urls http://172.31.24.246:2379,http://127.0.0 ...

  2. 使用InstallUtil发布windows服务

    发布Windows Service的方法 1. 在默认窗口Service1中右键Add Install,自动生成两个类. 2. 修改serviceInstaller1的属性:  a)ServiceNa ...

  3. EntityFramework使用总结(与MVC4.0实现CURD操作)

    本篇文介绍一下Entity Framework Code First的简单用法,通过一个学生信息的增删查改来学习Entity Framework的使用及与存储过程的交互.我也是学习Entity Fra ...

  4. 人生第一次成功的sql注入

    看了一些学习视频,按着大神们的教程,搜索inurl:asp?id=,结果一个可以注入的站点都找不到,绝望. 不放弃,又找啊找,找啊找,终于找到了一个! 啦啦啦,注入点!ヾ(o◕∀◕)ノヾ!! 再来查数 ...

  5. vuejs深入浅出—基础篇

    一.从HelloWorld说起 任何语言的都是从Hello World开始的,VueJs也不例外,直接上代码: <script src="https://unpkg.com/vue/d ...

  6. [HEOI2017] 寿司餐厅 + 最大权闭合子图的总结

    Description 太长了自己看叭 点这里! Solution 先学一波什么叫最大权闭合子图. 先要明白什么是闭合子图,闭合子图就是给定一个有向图,从中选择一些点组成一个点集V.对于V中任意一个点 ...

  7. EF 中多个数据库迁移

    Entity Framework 允许用户基于实体来创建和更新数据库,当项目中只有一个DBContext 时使用默认的参数就可以实现数据库的一些操作.当有多个DbContext 时需要制定参数才能完成 ...

  8. 还需要注册的是我们还有一个是“交差集” cross join, 这种Join没有办法用文式图表示,因为其就是把表A和表B的数据进行一个N*M的组合,即笛卡尔积。表达式如下:

             还需要注册的是我们还有一个是"交差集" cross join, 这种Join没有办法用文式图表示,因为其就是把表A和表B的数据进行一个N*M的组合,即笛卡尔积.表 ...

  9. Nullable<System.DateTime>日期格式转换 (转载)

    一.问题 1.html页面中时间显示出错,数据库中时间是正确的. 原因:没有把DateTime转成String类型. 2.  在C#中,发现不能直接使用ToString("yyyy-MM-d ...

  10. Matlab function lorenzgui

    function lorenzgui %LORENZGUI Plot the orbit around the Lorenz chaotic attractor. % This function an ...