mybatis中的otherwise怎么使用

   2024-10-25 3200
核心提示:在MyBatis中,otherwise是choose元素中的一个条件分支,用于指定当其他条件不满足时的默认分支。通常情况下,choose元素用于在多

在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,可以指定在没有其他条件满足时的默认分支逻辑。

 
举报打赏
 
更多>同类网点查询
推荐图文
推荐网点查询
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号