mybatis 中 if else 用法

mybaits 中没有 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>
复制代码

 

 

 

参考

https://mybatis.org/mybatis-3/zh/dynamic-sql.html

https://www.cnblogs.com/a8457013/p/8033263.html

 

原文地址:

https://www.cnblogs.com/poterliu/p/11582605.html

 

posted @ 2023-07-19 13:54  甜菜波波  阅读(302)  评论(0编辑  收藏  举报