您的位置:宽带测速网 > 编程知识 > mybatis中的otherwise怎么使用

mybatis中的otherwise怎么使用

2025-06-23 19:33来源:互联网 [ ]

在MyBatis中,otherwise是choose元素中的一个条件分支,用于指定当其他条件不满足时的默认分支。通常情况下,choose元素用于在多个条件中选择一个满足条件的分支执行。

下面是一个使用otherwise的示例:

<select id="selectBlog" parameterType="int" resultType="Blog">SELECT * FROM blogWHERE id = #{id}<choose><when test="author != null">AND author = #{author}</when><when test="title != null">AND title = #{title}</when><otherwise>AND views > 100</otherwise></choose></select>

在上面的示例中,如果author和title都不为null,则根据author和title查询blog;如果只有author不为null,则根据author查询blog;如果只有title不为null,则根据title查询blog;如果author和title都为null,则查询views大于100的blog。

通过使用otherwise,可以指定在没有其他条件满足时的默认分支逻辑。