您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“mybatis的where標(biāo)簽怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“mybatis的where標(biāo)簽怎么使用”吧!
我們經(jīng)常在動(dòng)態(tài)構(gòu)造sql時(shí),為防止注入或防止語句不當(dāng)時(shí)會(huì)使用where 1=1
<select id="selectGroupByEmployeeNum" resultMap="BaseResultMap" parameterType="com.dao.impl.ZcChatGroup"> select * from zc_chat_group WHERE 1=1 <if test="id!=null"> id= #{id} </if> <if test="leaderNum!=null"> and leader_num = #{leaderNum} </if> <if test="groupType!=null"> and group_type = #{groupType} </if> </select>
但在使用where標(biāo)簽可以簡(jiǎn)化這條語句
<select id="selectGroupByEmployeeNum" resultMap="BaseResultMap" parameterType="com.dao.impl.ZcChatGroup"> select * from zc_chat_group <where> <if test="id!=null"> id= #{id} </if> <if test="leaderNum!=null"> and leader_num = #{leaderNum} </if> <if test="groupType!=null"> and group_type = #{groupType} </if> </where> </select>
這條sql執(zhí)行時(shí),如果id這個(gè)參數(shù)為null,則這條語句的執(zhí)行結(jié)果為
select * from zc_chat_group where leader_num = ‘xx' and group_type = ‘xx'
這個(gè)‘where’標(biāo)簽會(huì)知道如果它包含的標(biāo)簽中有返回值的話,它就會(huì)插入一個(gè)‘where’。此外,如果標(biāo)簽返回的內(nèi)容是以AND 或OR開頭的,則會(huì)把它去除掉。
為了能達(dá)到MySQL性能的調(diào)優(yōu),我們可以基于Mybatis的where標(biāo)簽來進(jìn)行實(shí)現(xiàn)。where標(biāo)簽是頂層的遍歷標(biāo)簽,需要配合if標(biāo)簽使用,單獨(dú)使用無意義。通常有下面兩種實(shí)現(xiàn)形式。
方式一:
<select id="selectSelective" resultType="com.secbro.entity.User"> select * from t_user <where> <if test="username != null and username != ''"> username = #{username} </if> <if test="idNo != null and idNo != ''"> and id_no = #{idNo} </if> </where> </select>
方式二:
<select id="selectSelective" resultType="com.secbro.entity.User"> select * from t_user <where> <if test="username != null and username != ''"> and username = #{username} </if> <if test="idNo != null and idNo != ''"> and id_no = #{idNo} </if> </where> </select>
仔細(xì)觀察會(huì)發(fā)現(xiàn),這兩種方式的區(qū)別在于第一if條件中的SQL語句是否有and。
這里就涉及到where標(biāo)簽的兩個(gè)特性:
第一,只有if標(biāo)簽有內(nèi)容的情況下才會(huì)插入where子句;
第二,若子句的開通為 “AND” 或 “OR”,where標(biāo)簽會(huì)將它替換去除;
所以說,上面的兩種寫法都是可以了,Mybatis的where標(biāo)簽會(huì)替我們做一些事情。
但需要注意的是:where標(biāo)簽只會(huì) 智能的去除(忽略)首個(gè)滿足條件語句的前綴。所以建議在使用where標(biāo)簽時(shí),每個(gè)語句都最好寫上 and 前綴或者 or 前綴,否則像以下寫法就會(huì)出現(xiàn)問題:
<select id="selectSelective" resultType="com.secbro.entity.User"> select * from t_user <where> <if test="username != null and username != ''"> username = #{username} </if> <if test="idNo != null and idNo != ''"> id_no = #{idNo} </if> </where> </select>
生成的SQL語句如下:
select * from t_user WHERE username = ? id_no = ?
很顯然,語法是錯(cuò)誤的。
因此,在使用where標(biāo)簽時(shí),建議將所有條件都添加上and或or;
上面使用where標(biāo)簽可以達(dá)到拼接條件語句時(shí),自動(dòng)去掉首個(gè)條件的and或or,那么如果是其他自定義的關(guān)鍵字是否也能去掉呢?
此時(shí),where標(biāo)簽就無能為力了,該trim標(biāo)簽上場(chǎng)了,它也可以實(shí)現(xiàn)where標(biāo)簽的功能。
<select id="selectSelective" resultType="com.secbro.entity.User"> select * from t_user <trim prefix="where" prefixOverrides="and | or "> <if test="username != null and username != ''"> and username = #{username} </if> <if test="idNo != null and idNo != ''"> and id_no = #{idNo} </if> </trim> </select>
將上面基于where標(biāo)簽的寫改寫為trim標(biāo)簽,發(fā)現(xiàn)執(zhí)行效果完全一樣。而且trim標(biāo)簽具有了更加靈活的自定義性。
另外,在使用where語句或其他語句時(shí)一定要注意一個(gè)地方,那就是:注釋的使用。
先來看例子:
<select id="selectSelective" resultType="com.secbro.entity.User"> select * from t_user <where> <if test="username != null and username != ''"> and username = #{username} </if> <if test="idNo != null and idNo != ''"> /* and id_no = #{idNo}*/ and id_no = #{idNo} </if> </where> </select>
上述SQL語句中添加了 /**/的注釋,生成的SQL語句為:
select * from t_user WHERE username = ? /* and id_no = ?*/ and id_no = ?
執(zhí)行時(shí),直接報(bào)錯(cuò)。
還有一個(gè)示例:
<select id="selectSelective" resultType="com.secbro.entity.User"> select * from t_user <where> <if test="username != null and username != ''"> -- and username = #{username} and username = #{username} </if> <if test="idNo != null and idNo != ''"> and id_no = #{idNo} </if> </where> </select>
生成的SQL語句為:
select * from t_user WHERE -- and username = ? and username = ? and id_no = ?
同樣會(huì)導(dǎo)致報(bào)錯(cuò)。
這是因?yàn)槲覀兪褂?XML 方式配置 SQL 時(shí),如果在 where 標(biāo)簽之后添加了注釋,那么當(dāng)有子元素滿足條件時(shí),除了 < !-- --> 注釋會(huì)被 where 忽略解析以外,其它注釋例如 // 或 /**/ 或 -- 等都會(huì)被 where 當(dāng)成首個(gè)子句元素處理,導(dǎo)致后續(xù)真正的首個(gè) AND 子句元素或 OR 子句元素沒能被成功替換掉前綴,從而引起語法錯(cuò)誤。
同時(shí),個(gè)人在實(shí)踐中也經(jīng)常發(fā)現(xiàn)因?yàn)樵赬ML中使用注釋不當(dāng)導(dǎo)致SQL語法錯(cuò)誤或執(zhí)行出錯(cuò)誤的結(jié)果。強(qiáng)烈建議,非必要,不要在XML中注釋掉SQL,可以通過版本管理工具來追溯歷史記錄和修改。
到此,相信大家對(duì)“mybatis的where標(biāo)簽怎么使用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。