mybatis使用


<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="queryList" resultType="com....."> select * from student WHERE 1=1 <if test="name != null and name != ''"> AND name LIKE concat('%',#{name},'%') </if> <if test="merchantId != null and merchantId != ''"> AND merchant_id =#{merchantId} </if> <choose> <when test="sidx != null and sidx.trim() != ''"> order by ${sidx} ${order} </when> <otherwise> order by id desc </otherwise> </choose>
</select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM