亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

1. If 語句

需求:根據作者名字和博客名字來查詢博客!如果作者名字為空,那么只根據博客名字查詢,反之,則根據作者名字來查詢

<!--需求1:
根據作者名字和博客名字來查詢博客!
如果作者名字為空,那么只根據博客名字查詢,反之,則根據作者名來查詢
select * from blog where title = #{title} and author = #{author}
-->
<select id="queryBlogIf" parameterType="map" resultType="blog">
 select * from blog where
 <if test="title != null">
  title = #{title}
 </if>
 <if test="author != null">
  and author = #{author}
 </if>
</select>

這樣寫我們就可以看到,如果 author 等于 null,那么查詢語句為 select * from user where title=#{title},但是如果title為空呢?那么查詢語句為 select * from user where and author=#{author},這是錯誤的SQL 語句,如何解決呢?請看下面的 where 語句!

2. Where語句

修改上面的SQL語句:

<select id="queryBlogIf" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <if test="title != null">
   title = #{title}
  </if>
  <if test="author != null">
   and author = #{author}
  </if>
 </where>
</select>

where 元素只會在子元素返回任何內容的情況下才插入 “WHERE” 子句。而且,若子句的開頭為 “AND” 或 “OR”,where 元素也會將它們去除。

如果 where 元素與你期望的不太一樣,你也可以通過自定義 trim 元素來定制 where 元素的功能。

2.1 和 where 元素等價的自定義 trim 元素

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>

3. Set語句

同理,上面的對于查詢 SQL 語句包含 where 關鍵字,如果在進行更新操作的時候,含有 set 關鍵詞,我們怎么處理呢?

<!--注意set是用的逗號隔開-->
<update id="updateBlog" parameterType="map">
 update blog
 <set>
  <if test="title != null">
   title = #{title},
  </if>
  <if test="author != null">
   author = #{author}
  </if>
 </set>
 where id = #{id};
</update>

這個例子中,set 元素會動態地在行首插入 SET 關鍵字,并會刪掉額外的逗號(這些逗號是在使用條件語句給列賦值時引入的)

3.1 與 set 元素等價的自定義 trim 元素

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

4. Choose語句

有時候,我們不想用到所有的查詢條件,只想選擇其中的一個,查詢條件有一個滿足即可,使用 choose 標簽可以解決此類問題,類似于 JAVA 的 switch 語句

<select id="queryBlogChoose" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <choose>
   <when test="title != null">
    title = #{title}
   </when>
   <when test="author != null">
    and author = #{author}
   </when>
   <otherwise>
    and views = #{views}
   </otherwise>
  </choose>
 </where>
</select>

5. Foreach語句

將數據庫中前三個數據的id修改為1,2,3;

需求:我們需要查詢 blog 表中 id 分別為1,2,3的博客信息

<select id="queryBlogForeach" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <!--
  collection:指定輸入對象中的集合屬性
  item:每次遍歷生成的對象
  open:開始遍歷時的拼接字符串
  close:結束時拼接的字符串
  separator:遍歷對象之間需要拼接的字符串
  select * from blog where 1=1 and (id=1 or id=2 or id=3)
  -->
  <foreach collection="ids" item="id" open="and (" close=")"
  separator="or">
   id=#{id}
  </foreach>
 </where>
</select>

6. SQL片段

有時候可能某個 sql 語句我們用得特別多,為了增加代碼的重用性,簡化代碼,我們需要將這些代碼抽取出來,然后使用時直接調用。

提取SQL片段:

<sql id="if-title-author">
 <if test="title != null">
  title = #{title}
 </if>
 <if test="author != null">
  and author = #{author}
 </if>
</sql>

引用SQL片段:

<select id="queryBlogIf" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <!-- 引用 sql 片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace-->
  <include refid="if-title-author"></include>
  <!-- 在這里還可以引用其他的 sql 片段 -->
 </where>
</select>

注意:

  • 最好基于 單表來定義 sql 片段,提高片段的可重用性
  • 在 sql 片段中不要包括 where

7. Bind元素

bind 元素允許你在 OGNL 表達式以外創建一個變量,并將其綁定到當前的上下文。比如:

<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>

分享到:
標簽:MyBatis
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定