在mybatis的xml文件中编写sql语句有时候需要判断是否为空或者判断某些值的情况,比如:
select * from user
<if test = " id != null ">
where id =#{id}
</if>
这种写法是可以的,不过还有一种方法能加上else条件,这时候就用到了choose, when, otherwise这三个。
比如:
select * from user
<choose>
<when test="id !=null">
and id = #{id}
</when>
<otherwise>
and id is null
</otherwise>
</choose>