Mybatis4 之Mybatis動態(tài)sql的實現(xiàn)代碼
1.什么是動態(tài)SQL
傳統(tǒng)的使用JDBC的方法,相信大家在組合復雜的的SQL語句的時候,需要去拼接,稍不注意哪怕少了個空格,都會導致錯誤。Mybatis的動態(tài)SQL功能正是為了解決這種問題, 其通過 if, choose, when, otherwise, trim, where, set, foreach標簽,可組合成非常靈活的SQL語句,從而提高開發(fā)人員的效率。
SQL語句不固定, 會根據(jù)前臺用戶的操作而進行變化的SQL語句, 可以被稱之為動態(tài)SQL. 在MyBatis中, 提供了一組標簽, 用于方便的實現(xiàn)動態(tài)SQL, 不需要通過java代碼拼接字符串了.
###2.動態(tài)sql中的標簽
1. <if>
用于條件判斷, test屬性表示判斷結(jié)果, 要求是一個boolean.
2.<where>
用于維護where子句, 通常配合一起使用. 如下功能:
a)當沒有條件時, 不會創(chuàng)建WHERE關(guān)鍵字;
b)當有條件時, 會自動生成WHERE關(guān)鍵字;
c)會自動去掉第一個條件的and/or關(guān)鍵字.
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 <> #{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>
對參數(shù)進行加工, 通常用于模糊查詢給參數(shù)加通配符
<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>
遍歷集合(數(shù)組, List, Set, Map), 通常用于in操作或批量新增. 屬性簡介:
a)collection: 要遍歷的集合
b)item: 迭代項
c)open: 以什么字符開頭
d)close: 以什么字符結(jié)束
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>
知識點補充:靜態(tài)sql與動態(tài)sql有什么區(qū)別
SQL 語句從編譯和運行的角度可以分為兩種,靜態(tài) SQL和 動態(tài) SQL,這兩種 SQL 在使用方式、運行機制和性能表現(xiàn)等方面各有特點 :
靜態(tài) SQL:靜態(tài) SQL 語句一般用于嵌入式 SQL 應用中,在程序運行前,SQL 語句必須是確定的,例如 SQL 語句中涉及的列名和表名必須是存在的。靜態(tài) SQL 語句的編譯是在應用程序運行前進行的,編譯的結(jié)果會存儲在數(shù)據(jù)庫內(nèi)部。而后程序運行時,數(shù)據(jù)庫將直接執(zhí)行編譯好的 SQL 語句,降低運行時的開銷。
動態(tài) SQL:動態(tài) SQL 語句是在應用程序運行時被編譯和執(zhí)行的,例如,使用 DB2 的交互式工具 CLP 訪問數(shù)據(jù)庫時,用戶輸入的 SQL 語句是不確定的,因此 SQL 語句只能被動態(tài)地編譯。動態(tài) SQL 的應用較多,常見的 CLI 和 JDBC 應用程序都使用動態(tài) SQL。
靜態(tài)sql:語句類型在編程時候必須是確定好的。比如
select * from employee where empno='abc' select * from employee where empno='12'
都必須是確定的,唯一可以變化的是abc的值。
動態(tài)sql:語句類型可以在運行期間指定,比如clp就是最典型的動態(tài)sql程序,你可以輸入任何命令。
靜態(tài)sql的存取路徑是在運行前就確定好的,而動態(tài)sql的存取路徑是在運行時動態(tài)生成的。因此生成的存取計劃相對更優(yōu),但考慮到生成存取路徑的開銷,有可能應用程序的運行時間相對會比靜態(tài)sql長些。
總結(jié)
到此這篇關(guān)于Mybatis4 之Mybatis動態(tài)sql的實現(xiàn)代碼的文章就介紹到這了,更多相關(guān)mybatis動態(tài)sql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SQL語句過濾條件放在on與where子句中的區(qū)別和聯(lián)系淺析
在寫SQL語句的時候,我們經(jīng)常會用到各種表連接,還有各種分組聚合函數(shù),下面這篇文章主要給大家介紹了關(guān)于SQL語句過濾條件放在on與where子句中的區(qū)別和聯(lián)系,需要的朋友可以參考下2022-09-09
SQL SERVER數(shù)據(jù)庫表記錄只保留N天圖文教程
本篇向大家介紹SQL Server 2008 R2數(shù)據(jù)庫中數(shù)據(jù)表保留10天記錄,需要的朋友可以參考下2015-09-09
SQL?Server數(shù)據(jù)庫判斷最近一次的備份執(zhí)行結(jié)果(最新推薦)
這篇文章主要介紹了SQL?Server數(shù)據(jù)庫判斷最近一次的備份執(zhí)行結(jié)果,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
系統(tǒng)存儲過程sp_MSforeachtable和sp_MSforeachdb使用說明
系統(tǒng)存儲過程sp_MSforeachtable和sp_MSforeachdb2009-09-09
MS SQL Server排查多列之間的值是否重復的功能實現(xiàn)
在日常的應用中,排查列重復記錄是經(jīng)常遇到的一個問題,但某些需求下,需要我們排查一組列之間是否有重復值的情況,本文給大家介紹了MS SQL Server排查多列之間的值是否重復的功能實現(xiàn),需要的朋友可以參考下2024-09-09
sqlserver數(shù)據(jù)庫最大Id沖突問題解決方法之一
在一個特定的表中保存最大Id,通過記錄來取每次的唯一最大值2011-12-12

