国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

Mybatis4 之Mybatis動(dòng)態(tài)sql的實(shí)現(xiàn)代碼

2020-07-25 11:52:01
字體:
供稿:網(wǎng)友

1.什么是動(dòng)態(tài)SQL

傳統(tǒng)的使用JDBC的方法,相信大家在組合復(fù)雜的的SQL語句的時(shí)候,需要去拼接,稍不注意哪怕少了個(gè)空格,都會(huì)導(dǎo)致錯(cuò)誤。Mybatis的動(dòng)態(tài)SQL功能正是為了解決這種問題, 其通過 if, choose, when, otherwise, trim, where, set, foreach標(biāo)簽,可組合成非常靈活的SQL語句,從而提高開發(fā)人員的效率。

SQL語句不固定, 會(huì)根據(jù)前臺(tái)用戶的操作而進(jìn)行變化的SQL語句, 可以被稱之為動(dòng)態(tài)SQL. 在MyBatis中, 提供了一組標(biāo)簽, 用于方便的實(shí)現(xiàn)動(dòng)態(tài)SQL, 不需要通過java代碼拼接字符串了.
###2.動(dòng)態(tài)sql中的標(biāo)簽

1. <if>

用于條件判斷, test屬性表示判斷結(jié)果, 要求是一個(gè)boolean.

2.<where>

用于維護(hù)where子句, 通常配合一起使用. 如下功能:
a)當(dāng)沒有條件時(shí), 不會(huì)創(chuàng)建WHERE關(guān)鍵字;
b)當(dāng)有條件時(shí), 會(huì)自動(dòng)生成WHERE關(guān)鍵字;
c)會(huì)自動(dòng)去掉第一個(gè)條件的and/or關(guān)鍵字.

3.<choose><when><otherwise>

功能類似于switch…case…default, 表示多分支判斷, 只能成立一個(gè)條件

<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>

對(duì)參數(shù)進(jìn)行加工, 通常用于模糊查詢給參數(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>

用于維護(hù)update語句中的set子句, 特點(diǎn)是可以刪除多余的逗號(hào)

<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: 迭代項(xiàng)

c)open: 以什么字符開頭

d)close: 以什么字符結(jié)束

e)separator: 多個(gè)迭代項(xiàng)之間的分隔符

<delete id="delBatch"> delete from tb_user <where> id in <foreach collection="ids" item="id" open="(" close=")" separator=",">  #{id} </foreach> </where></delete>

8.<trim>

在語句的前后進(jìn)行追加和去除指定的字符.

<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>

知識(shí)點(diǎn)補(bǔ)充:靜態(tài)sql與動(dòng)態(tài)sql有什么區(qū)別

SQL 語句從編譯和運(yùn)行的角度可以分為兩種,靜態(tài) SQL和 動(dòng)態(tài) SQL,這兩種 SQL 在使用方式、運(yùn)行機(jī)制和性能表現(xiàn)等方面各有特點(diǎn) :

靜態(tài) SQL:靜態(tài) SQL 語句一般用于嵌入式 SQL 應(yīng)用中,在程序運(yùn)行前,SQL 語句必須是確定的,例如 SQL 語句中涉及的列名和表名必須是存在的。靜態(tài) SQL 語句的編譯是在應(yīng)用程序運(yùn)行前進(jìn)行的,編譯的結(jié)果會(huì)存儲(chǔ)在數(shù)據(jù)庫內(nèi)部。而后程序運(yùn)行時(shí),數(shù)據(jù)庫將直接執(zhí)行編譯好的 SQL 語句,降低運(yùn)行時(shí)的開銷。

動(dòng)態(tài) SQL:動(dòng)態(tài) SQL 語句是在應(yīng)用程序運(yùn)行時(shí)被編譯和執(zhí)行的,例如,使用 DB2 的交互式工具 CLP 訪問數(shù)據(jù)庫時(shí),用戶輸入的 SQL 語句是不確定的,因此 SQL 語句只能被動(dòng)態(tài)地編譯。動(dòng)態(tài) SQL 的應(yīng)用較多,常見的 CLI 和 JDBC 應(yīng)用程序都使用動(dòng)態(tài) SQL。

靜態(tài)sql:語句類型在編程時(shí)候必須是確定好的。比如

select * from employee where empno='abc'select * from employee where empno='12'

都必須是確定的,唯一可以變化的是abc的值。

動(dòng)態(tài)sql:語句類型可以在運(yùn)行期間指定,比如clp就是最典型的動(dòng)態(tài)sql程序,你可以輸入任何命令。

靜態(tài)sql的存取路徑是在運(yùn)行前就確定好的,而動(dòng)態(tài)sql的存取路徑是在運(yùn)行時(shí)動(dòng)態(tài)生成的。因此生成的存取計(jì)劃相對(duì)更優(yōu),但考慮到生成存取路徑的開銷,有可能應(yīng)用程序的運(yùn)行時(shí)間相對(duì)會(huì)比靜態(tài)sql長些。

總結(jié)

到此這篇關(guān)于Mybatis4 之Mybatis動(dòng)態(tài)sql的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)mybatis動(dòng)態(tài)sql內(nèi)容請(qǐng)搜索武林網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持武林網(wǎng)!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 佛山市| 邳州市| 汝阳县| 沐川县| 祁东县| 双江| 西和县| 吉安市| 苏州市| 定西市| 新建县| 平谷区| 错那县| 泽州县| 抚州市| 罗平县| 永清县| 郯城县| 安陆市| 读书| 彰武县| 高青县| 铜陵市| 慈利县| 广汉市| 隆安县| 佛山市| 桦川县| 鄂托克旗| 南投市| 涪陵区| 横峰县| 邯郸县| 濮阳县| 会东县| 沿河| 海淀区| 泾阳县| 东源县| 随州市| 拜泉县|