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屬性表示判斷結果, 要求是一個boolean.
2.<where>
用于維護where子句, 通常配合一起使用. 如下功能:
a)當沒有條件時, 不會創(chuàng)建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 <> #{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: 以什么字符結束
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 語句的編譯是在應用程序運行前進行的,編譯的結果會存儲在數(shù)據(jù)庫內部。而后程序運行時,數(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長些。
總結
到此這篇關于Mybatis4 之Mybatis動態(tài)sql的實現(xiàn)代碼的文章就介紹到這了,更多相關mybatis動態(tài)sql內容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網友推薦、互聯(lián)網收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。