码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis动态语句if与choose的区别

时间:2018-08-12 14:13:06      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:otherwise   参数   select   from   bat   red   res   find   区别   

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的区别

标签:otherwise   参数   select   from   bat   red   res   find   区别   

原文地址:https://www.cnblogs.com/jagng951014/p/9462582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!