MyBatis-07-動態SQL
阿新 • • 發佈:2020-12-06
MyBatis-07-動態SQL
1. if 語句
1、編寫介面類
//需求1
List<Blog> queryBlogIf(Map map);
2、編寫SQL語句
<!--需求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>
3、測試
@Test public void testQueryBlogIf(){ SqlSession session = MybatisUtils.getSession(); BlogMapper mapper = session.getMapper(BlogMapper.class); HashMap<String, String> map = new HashMap<String, String>(); map.put("title","Mybatis如此簡單"); map.put("author","狂神說"); List<Blog> blogs = mapper.queryBlogIf(map); System.out.println(blogs); session.close(); }
4、結果
reated connection 13803304. ==> Preparing: select * from blog where 1=1 and author = ? ==> Parameters: 狂神說(String) <== Columns: id, title, author, create_time, views <== Row: 475e50e7ed464b9c9a9434833e3510de, Mybatis如此簡單, 狂神說, 2020-12-06 12:36:13, 9999 <== Row: 3ec85eeb7552442c90144561d9798360, Java如此簡單, 狂神說, 2020-12-06 12:36:13, 9999 <== Row: b6b0359aa33e4bc5a267bf053704bdfa, Spring如此簡單, 狂神說, 2020-12-06 12:36:13, 9999 <== Row: c86df2d937cb4132809c0cd8d6ff7049, 微服務如此簡單, 狂神說, 2020-12-06 12:36:13, 9999 <== Total: 4 Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@d29f28] Returned connection 13803304 to pool.
這樣寫我們可以看到,如果 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 開頭的,則它會剔除掉。
3. set 語句
同理,上面的對於查詢 SQL 語句包含 where 關鍵字,如果在進行更新操作的時候,含有 set 關鍵詞,我們怎麼處理呢?
1、編寫介面方法
int updateBlog(Map map);
2、sql配置檔案
<!--注意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>
3、測試
@Test
public void testUpdateBlog(){
SqlSession session = MybatisUtils.getSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
HashMap<String, String> map = new HashMap<String, String>();
map.put("title","動態SQL");
map.put("author","秦疆");
map.put("id","9d6a763f5e1347cebda43e2a32687a77");
mapper.updateBlog(map);
session.close();
}
4. choose 語句
有時候,我們不想用到所有的查詢條件,只想選擇其中的一個,查詢條件有一個滿足即可,使用 choose 標籤可以解決此類問題,類似於 Java 的 switch 語句。
choose 與 if 的區別:
- if 如果條件都滿足會帶出所有滿足條件的語句
- choose 只會帶出最先滿足的條件的語句
- choose 如果沒有滿足的條件會執行 otherwise 語句
1、編寫介面方法
List<Blog> queryBlogChoose(Map map);
2、sql配置檔案
<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>
3、測試類
@Test
public void testQueryBlogChoose(){
SqlSession session = MybatisUtils.getSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title","Java如此簡單");
map.put("author","狂神說");
map.put("views",9999);
List<Blog> blogs = mapper.queryBlogChoose(map);
System.out.println(blogs);
session.close();
5. 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
6. Foreach 語句
需求:我們需要查詢 blog 表中 id 分別為1,2,3的部落格資訊
1、編寫介面
List<Blog> queryBlogForeach(Map map);
2、編寫SQL語句
<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>
3、測試
@Test
public void testQueryBlogForeach(){
SqlSession session = MybatisUtils.getSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
HashMap map = new HashMap();
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
ids.add(3);
map.put("ids",ids);
List<Blog> blogs = mapper.queryBlogForeach(map);
System.out.println(blogs);
session.close();
}
小結:
其實動態 sql 語句的編寫往往就是一個拼接的問題,為了保證拼接準確,我們最好首先要寫原生的 sql 語句出來,然後在通過 mybatis 動態sql 對照著改,防止出錯。多在實踐中使用才是熟練掌握它的技巧。