1.什么是動態SQL
傳統的使用JDBC的方法,相信大家在組合復雜的的SQL語句的時候,需要去拼接,稍不注意哪怕少了個空格,都會導致錯誤。Mybatis的動態SQL功能正是為了解決這種問題, 其通過 if, choose, when, otherwise, trim, where, set, foreach標簽,可組合成非常靈活的SQL語句,從而提高開發人員的效率。
SQL語句不固定, 會根據前臺用戶的操作而進行變化的SQL語句, 可以被稱之為動態SQL. 在MyBatis中, 提供了一組標簽, 用于方便的實現動態SQL, 不需要通過java代碼拼接字符串了.
###2.動態sql中的標簽
1. if>
用于條件判斷, test屬性表示判斷結果, 要求是一個boolean.
2.where>
用于維護where子句, 通常配合一起使用. 如下功能:
a)當沒有條件時, 不會創建WHERE關鍵字;
b)當有條件時, 會自動生成WHERE關鍵字;
c)會自動去掉第一個條件的and/or關鍵字.
3.choose>when>otherwise>
功能類似于switch…case…default, 表示多分支判斷, 只能成立一個條件
mapper namespace="com.bjsxt.mapper.UserMapper">
select id="selByCondition" resultType="user">
select * from tb_user
where>
if test="id != null">
and id=#{id}
/if>
if test="username != null and username != ''">
and username=#{username}
/if>
if test="age != null">
and age lt;gt; #{age}
/if>
choose>
when test="birthday != null and birthday != ''">
and birthday = #{birthday}
/when>
otherwise>
and birthday is null
/otherwise>
/choose>
/where>
/select>
/mapper>
4.bind>
對參數進行加工, 通常用于模糊查詢給參數加通配符
select id="sel2" resultType="user">
include refid="base_sql" />
where>
if test="realname != null and realname != ''">
bind name="realname" value="'%' + realname + '%'"/>
and realname like #{realname}
/if>
/where>
/select>
5.include>
配合使用, 用于提取通用sql語句片段, 用于引用SQL片段
sql id="base_sql">
select
id, username, password, realname, age, birthday, reg_time regTime
from tb_user
/sql>
select id="sel2" resultType="user">
include refid="base_sql" />
where>
if test="realname != null and realname != ''">
bind name="realname" value="'%' + realname + '%'"/>
and realname like #{realname}
/if>
/where>
/select>
6.set>
用于維護update語句中的set子句, 特點是可以刪除多余的逗號
update id="upd">
update
tb_user
set>
if test="username != null and username != ''">
username=#{username},
/if>
if test="age != null">
age=#{age}
/if>
/set>
where
id=#{id}
/update>
7.foreach>
遍歷集合(數組, List, Set, Map), 通常用于in操作或批量新增. 屬性簡介:
a)collection: 要遍歷的集合
b)item: 迭代項
c)open: 以什么字符開頭
d)close: 以什么字符結束
e)separator: 多個迭代項之間的分隔符
delete id="delBatch">
delete from tb_user
where>
id in
foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
/foreach>
/where>
/delete>
8.trim>
在語句的前后進行追加和去除指定的字符.
insert id="insBatch">
insert into tb_user values
foreach collection="users" item="user" separator=",">
trim prefix="(" prefixOverrides="," suffix=")" suffixOverrides=",">
,default, #{user.username}, #{user.password}, #{user.realname}, #{user.age}, #{user.birthday}, now(),
/trim>
/foreach>
/insert>
知識點補充:靜態sql與動態sql有什么區別
SQL 語句從編譯和運行的角度可以分為兩種,靜態 SQL和 動態 SQL,這兩種 SQL 在使用方式、運行機制和性能表現等方面各有特點 :
靜態 SQL:靜態 SQL 語句一般用于嵌入式 SQL 應用中,在程序運行前,SQL 語句必須是確定的,例如 SQL 語句中涉及的列名和表名必須是存在的。靜態 SQL 語句的編譯是在應用程序運行前進行的,編譯的結果會存儲在數據庫內部。而后程序運行時,數據庫將直接執行編譯好的 SQL 語句,降低運行時的開銷。
動態 SQL:動態 SQL 語句是在應用程序運行時被編譯和執行的,例如,使用 DB2 的交互式工具 CLP 訪問數據庫時,用戶輸入的 SQL 語句是不確定的,因此 SQL 語句只能被動態地編譯。動態 SQL 的應用較多,常見的 CLI 和 JDBC 應用程序都使用動態 SQL。
靜態sql:語句類型在編程時候必須是確定好的。比如
select * from employee where empno='abc'
select * from employee where empno='12'
都必須是確定的,唯一可以變化的是abc的值。
動態sql:語句類型可以在運行期間指定,比如clp就是最典型的動態sql程序,你可以輸入任何命令。
靜態sql的存取路徑是在運行前就確定好的,而動態sql的存取路徑是在運行時動態生成的。因此生成的存取計劃相對更優,但考慮到生成存取路徑的開銷,有可能應用程序的運行時間相對會比靜態sql長些。
總結
到此這篇關于Mybatis4 之Mybatis動態sql的實現代碼的文章就介紹到這了,更多相關mybatis動態sql內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- MyBatis的動態SQL語句實現
- MyBatis實現動態SQL的實現方法
- Mybatis中的動態SQL語句解析
- MyBatis動態Sql之if標簽的用法詳解
- mybatis動態sql之Map參數的講解
- Mybatis模糊查詢和動態sql語句的用法
- MyBatis動態SQL實現配置過程解析