【備忘】mybatis的條件判斷用

mybatis並無if..else,在mybatis的sql mapper文件中,條件判斷要用choose..when..otherwise。
 
<choose>
  <when test="status == 'PROCES' or status == 'PENDNG'"> and batcol.status in('PROCES','PENDNG')</when>
  <when test="status == 'PREAUD'"> and batcol.status = '--'</when>
  <otherwise> and batcol.status = #{status,jdbcType=CHAR}</otherwise>
</choose>
 
<if test="status != null and status != 'all'" >
  <choose>
    <when test="status == 'PROCES' or status == 'PENDNG'"> and batcol.status in('PROCES','PENDNG')</when>
    <when test="status == 'PREAUD'"> and batcol.status = '--'</when>
    <otherwise> and batcol.status = #{status,jdbcType=CHAR}</otherwise>
  </choose>

</if>
 
choose爲一個總體,
when表示if ,(when可重複,即實現if..else if..else if..
otherwise表示else。
注意: test裏的等號用==,而不是=。
相關文章
相關標籤/搜索