MyBatis动态语句if与choose的区别

Posted JAGNIG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis动态语句if与choose的区别相关的知识,希望对你有一定的参考价值。

if(通过“title”和“author”两个参数进行可选搜索):

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

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>

 

以上是关于MyBatis动态语句if与choose的区别的主要内容,如果未能解决你的问题,请参考以下文章

mybatis--MyBatis动态SQL语句

数据库_MysqlMySQL动态语句 if set choose where foreach trim

mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

MyBatis动态sql和分页

MyBatis动态SQL

Mybatis超强大的动态SQL大全