if else 语句是我们在编写程序中常用的判断语句,用来判断是否满足条件从而执行不同的执行语句。但是,在 mybaits 中,只有 if 标签,并没有 else 标签,可以使用 chose when otherwise 代替。

下面就是MyBatis中的if....else...的表示方法:

<choose>
    <when test="">
        //...
    </when>
    <otherwise>
        //...
    </otherwise>
</choose>

具体应用例子,如下所示:

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

原文链接: http://www.mybatis.cn/mybatis/1964.html

标签: none

[网站公告]-[2024年兼职介绍]


添加新评论