zoukankan      html  css  js  c++  java
  • mybatis使用<choose> <when>

    一、需求

       后台使用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。

  • 相关阅读:
    [转]Windows visio2019破解激活
    KMP模式匹配算法
    【蓝桥杯2016_C++】t3:方格填数
    【蓝桥杯2015_C++】t4:格子中输出
    【蓝桥杯2015_C++】t3:奇妙的数字
    【蓝桥杯2014_C++】t6:扑克序列
    【蓝桥杯2014_C++】t4:史丰收速算
    【蓝桥杯2014_C++】t3:神奇算式
    【蓝桥杯2017_C++】t1:迷宫
    【蓝桥杯】买不到的数目
  • 原文地址:https://www.cnblogs.com/gczmn/p/8305153.html
Copyright © 2011-2022 走看看