【备忘】mybatis的条件判断用

时间:2022-06-01 18:56:20
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里的等号用==,而不是=。