mybatis使用

JAVA学习网 2018-01-18 15:59:01

一、需求

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

               如果when中的条件都不满足,使用   条件3;

       </otherwise>

 

   </choose>

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

               and  条件2;

    </if>

</select>

阅读(723) 评论(0)