Mybatis超級強大的動態(tài)SQL語句大全
1. If 語句
需求:根據(jù)作者名字和博客名字來查詢博客!如果作者名字為空,那么只根據(jù)博客名字查詢,反之,則根據(jù)作者名來查詢
<!--需求1:
根據(jù)作者名字和博客名字來查詢博客!
如果作者名字為空,那么只根據(jù)博客名字查詢,反之,則根據(jù)作者名來查詢
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 元素只會在子元素返回任何內(nèi)容的情況下才插入 “WHERE” 子句。而且,若子句的開頭為 “AND” 或 “OR”,where 元素也會將它們?nèi)コ?/p>
如果 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 元素會動態(tài)地在行首插入 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語句
將數(shù)據(jù)庫中前三個數(shù)據(jù)的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 語句我們用的特別多,為了增加代碼的重用性,簡化代碼,我們需要將這些代碼抽取出來,然后使用時直接調(diào)用。
提取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 表達式以外創(chuàng)建一個變量,并將其綁定到當前的上下文。比如:
<select id="selectBlogsLike" resultType="Blog">
<bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
SELECT * FROM BLOG
WHERE title LIKE #{pattern}
</select>
附:MyBatis配置動態(tài)SQL語句
CREATE DATABASE `mybatis_study`; USE `mybatis_study`; CREATE TABLE `user`( ? `user_id` INT(20) NOT NULL PRIMARY KEY, ? `user_name` VARCHAR(30) DEFAULT NULL, ? `password` VARCHAR(30) DEFAULT NULL )ENGINE = INNODB DEFAULT CHARSET = utf8; INSERT INTO `user` (`user_id`,`user_name`,`password`) VALUES (1, '張三', '123456'), (2, 'admin', 'admin'), (3, 'root', 'root');
在 MyBatis 的 SQL映射文件中,有時候需要根據(jù)一些查詢條件,來選擇不同的SQL語句,如果每一個場景都重寫SQL,很顯然效率沒有很高,而 MyBatis 的動態(tài)SQL很好的解決了這種問題,根據(jù)條件動態(tài)的處理 SQL, 特別簡單的說就是,寫一次SQL,但是根據(jù)分支等的跳轉,在多個場景下也可以使用,例如:
- 當查詢條件由于參數(shù)不同而無法確定具體是什么,可以使用<where>標簽包含
- 在 <where> 可以使用 <if test="...."> 分條件進行處理,實現(xiàn)動態(tài)
- <foreach>遍歷標簽,在用戶中查詢尋多個id,例如(12,16,17)
在此之外,動態(tài)SQL同時結局了,在原生 JDBC 中需要拼接SQL語句時由于書寫問題,而導致報錯
總結
到此這篇關于Mybatis超級強大的動態(tài)SQL語句大全的文章就介紹到這了,更多相關Mybatis動態(tài)SQL語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于springboot2整合lettuce啟動卡住問題的解決方法
Lettuce和Jedis的都是連接Redis Server的客戶端程序,下面這篇文章主要給大家介紹了關于springboot2整合lettuce啟動卡住問題的解決方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-12-12
多個SpringBoot項目采用redis實現(xiàn)Session共享功能
這篇文章主要介紹了多個SpringBoot項目采用redis實現(xiàn)Session共享,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
idea中maven本地倉庫jar包打包失敗和無法引用的問題解決
本文主要介紹了idea中maven本地倉庫jar包打包失敗和無法引用的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06
Spring Cloud Data Flow初體驗以Local模式運行
這篇文章主要介紹了Spring Cloud Data Flow初體驗以Local模式運行,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Java中java.lang.ClassCastException異常原因及解決方法
大家好,本篇文章主要講的是Java中java.lang.ClassCastException異常原因及解決方法,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01

