Linux 拨号vps windows公众号手机端

mybatis中的otherwise怎么使用

lewis 7年前 (2018-03-13) 阅读数 9 #程序编程
文章标签 mybatis

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

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

<select id="selectBlog" parameterType="int" resultType="Blog">
  SELECT * FROM blog
  WHERE 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,可以指定在没有其他条件满足时的默认分支逻辑。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门