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