mybatis choose 标签使用

发布时间 2023-09-12 10:36:55作者: 種瓜得豆

choose when otherwise 标签可以帮我们实现 if else 的逻辑。

一个 choose 标签至少有一个 when, 最多一个otherwise

/**
* - 当 type 有值时, 使用 type 进行查询;
* - 当 type 没有值时, 使用 type = 1 进行查询;
* - 否则返回空
*/
<choose>
    <when test="type != null">
        and type = #{type}
    </when>
    <when test="type == null">
        and type = 1
    </when>
    <otherwise>
        and 1 = 2
    </otherwise>
</choose>