一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Mybatis的where標簽詳解

Mybatis的where標簽詳解

2022-03-03 22:36程序新視界二師兄 Java教程

本文基于Mybatis中where標簽的使用,展開講了它的使用方式、特性以及拓展到trim標簽的替代作用,同時,也提到了在使用時可能會出現的坑。內容雖然簡單,但如果能夠很好地實踐、避免踩坑也是能力的體現。

背景

在上篇文章,我們系統地學習了where 1=1 相關的知識點,大家可以回看《不要再用where 1=1了!有更好的寫法!》這篇文章。文章中涉及到了Mybatis的替代方案,有好學的朋友在評論區有朋友問了基于Mybatis寫法的問題。

于是,就有了這篇文章。本篇文章會將Mybatis中where標簽的基本使用形式、小技巧以及容易踩到的坑進行總結梳理,方便大家更好地實踐運用。

原始的手動

拼接在不使用Mybatis的where標簽時,我們通常是根據查詢條件進行手動拼接,也就是用到了上面提到的where 1=1的方式,示例如下:

  <select id="selectSelective" resultType="com.secbro.entity.User"> select * from t_user where 1=1 <if test="username != null and username != ''"> and username = #{username} if> <if test="idNo != null and idNo != ''"> and id_no = #{idNo} if> select>

這種方式主要就是為了避免語句拼接錯誤,出現類似如下的錯誤SQL:

select * from t_user where and username = 'Tom' and id = '1001'; select * from t_user where and id = '1001';

當添加上1=1時,SQL語句便是正確的了:

select * from t_user where 1=1 and username = 'Tom' and id = '1001'; select * from t_user where 1=1 and id = '1001';

這個我們之前已經提到過,多少對MySQL數據庫的有一定的壓力。因為1=1條件的優化過濾是需要MySQL做的。如果能夠將這部分放到應用程序來做,就減少了MySQL的壓力。畢竟,應用程序是可以輕易地橫向擴展的。

Mybatis where標簽的使用

為了能達到MySQL性能的調優,我們可以基于Mybatis的where標簽來進行實現。where標簽是頂層的遍歷標簽,需要配合if標簽使用,單獨使用無意義。通常有下面兩種實現形式。

方式一:

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

仔細觀察會發現,這兩種方式的區別在于第一if條件中的SQL語句是否有and。

這里就涉及到where標簽的兩個特性:

  • 第一,只有if標簽有內容的情況下才會插入where子句;
  • 第二,若子句的開通為 “AND” 或 “OR”,where標簽會將它替換去除;

所以說,上面的兩種寫法都是可以了,Mybatis的where標簽會替我們做一些事情。

但需要注意的是:where標簽只會智能的去除(忽略)首個滿足條件語句的前綴。所以建議在使用where標簽時,每個語句都最好寫上 and 前綴或者 or 前綴,否則像以下寫法就會出現問題:

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

很顯然,語法是錯誤的。

因此,在使用where標簽時,建議將所有條件都添加上and或or;

進階:自定義trim標簽

上面使用where標簽可以達到拼接條件語句時,自動去掉首個條件的and或or,那么如果是其他自定義的關鍵字是否也能去掉呢?

此時,where標簽就無能為力了,該trim標簽上場了,它也可以實現where標簽的功能。

 <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標簽的寫改寫為trim標簽,發現執行效果完全一樣。而且trim標簽具有了更加靈活的自定義性。

where語句的坑

另外,在使用where語句或其他語句時一定要注意一個地方,那就是:注釋的使用。

先來看例子:

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

執行時,直接報錯。

還有一個示例:

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

同樣會導致報錯。

這是因為我們使用 XML 方式配置 SQL 時,如果在 where 標簽之后添加了注釋,那么當有子元素滿足條件時,除了 < !-- --> 注釋會被 where 忽略解析以外,其它注釋例如 // 或 /**/ 或 -- 等都會被 where 當成首個子句元素處理,導致后續真正的首個 AND 子句元素或 OR 子句元素沒能被成功替換掉前綴,從而引起語法錯誤。

同時,個人在實踐中也經常發現因為在XML中使用注釋不當導致SQL語法錯誤或執行出錯誤的結果。強烈建議,非必要,不要在XML中注釋掉SQL,可以通過版本管理工具來追溯歷史記錄和修改。

小結

本文基于Mybatis中where標簽的使用,展開講了它的使用方式、特性以及拓展到trim標簽的替代作用,同時,也提到了在使用時可能會出現的坑。內容雖然簡單,但如果能夠很好地實踐、避免踩坑也是能力的體現。

原文地址:https://mp.weixin.qq.com/s/rkH9KkwnEXF3vzoPvQ72VA

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 暖暖的视频完整视频韩国免费 | 日韩国产成人精品视频人 | 国产99精品 | 丝袜捆绑调教视频免费区 | 美女翘臀跪床被打屁股作文 | 动漫美女被羞羞产奶 | 美女bbxx美女bbb | 女人张开腿让男人桶视频免费大全 | 岛国在线播放v片免费 | 我的青梅竹马是消防员2季未增删免费 | 免费观看在线观看 | 成年人免费在线看 | bt国产| jazz中国在线视频 | 99久久综合精品免费 | 国产卡一卡二卡四卡无卡 | 美女光屁股网站 | 亚洲欧美一区二区久久 | 国产一区二区视频在线 | 亚洲日本久久一区二区va | 猛吸奶水的老汉 | 法国贵妇一级伦理hd | 农夫69小说小雨与农村老太 | 欧美午夜寂寞影院安卓列表 | 日本一区二区三区国产 | 男女一级簧色带 | 国产成人精品第一区二区 | 红楼影视h38bar在线线播放 | 人人爽人人草 | 激情婷婷综合久久久久 | 国产精品久久久久久久人人看 | 久草青青在线 | 高h短篇辣肉各种姿势bl | 成人女人天堂午夜视频 | 国产美女屁股直流白浆视频无遮挡 | 波多野结衣一区 | 22sihu国产精品视频影视资讯 | 黄瓜视频黄版 | 91肥熟国产老肥熟在线 | 国产成人久久精品推最新 | 4hc44四虎永久地址链接 |