<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Mybatis的where標(biāo)簽,竟然還有這么多不知道的!

          共 3197字,需瀏覽 7分鐘

           ·

          2022-03-07 18:42

          背景

          在上篇文章,我們系統(tǒng)地學(xué)習(xí)了where 1=1 相關(guān)的知識(shí)點(diǎn),大家可以回看《不要再用where 1=1了!有更好的寫法!》這篇文章。文章中涉及到了Mybatis的替代方案,有好學(xué)的朋友在評(píng)論區(qū)有朋友問了基于Mybatis寫法的問題。

          于是,就有了這篇文章。本篇文章會(huì)將Mybatis中where標(biāo)簽的基本使用形式、小技巧以及容易踩到的坑進(jìn)行總結(jié)梳理,方便大家更好地實(shí)踐運(yùn)用。

          原始的手動(dòng)拼接

          在不使用Mybatis的where標(biāo)簽時(shí),我們通常是根據(jù)查詢條件進(jìn)行手動(dòng)拼接,也就是用到了上面提到的where 1=1的方式,示例如下:

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

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

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

          當(dāng)添加上1=1時(shí),SQL語句便是正確的了:

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

          這個(gè)我們之前已經(jīng)提到過,多少對(duì)MySQL數(shù)據(jù)庫(kù)的有一定的壓力。因?yàn)?=1條件的優(yōu)化過濾是需要MySQL做的。如果能夠?qū)⑦@部分放到應(yīng)用程序來做,就減少了MySQL的壓力。畢竟,應(yīng)用程序是可以輕易地橫向擴(kuò)展的。

          Mybatis where標(biāo)簽的使用

          為了能達(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)形式。

          方式一:

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

          方式二:

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

          仔細(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)問題:

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

          生成的SQL語句如下:

          select?*?from?t_user??????WHERE?username?=????id_no?=??

          很顯然,語法是錯(cuò)誤的。

          因此,在使用where標(biāo)簽時(shí),建議將所有條件都添加上and或or

          進(jìn)階:自定義trim標(biāo)簽

          上面使用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)簽的功能。

          ??"selectSelective"?resultType="com.secbro.entity.User">
          ????select?*?from?t_user
          ????"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>
          ????
          ??

          將上面基于where標(biāo)簽的寫改寫為trim標(biāo)簽,發(fā)現(xiàn)執(zhí)行效果完全一樣。而且trim標(biāo)簽具有了更加靈活的自定義性。

          where語句的坑

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

          先來看例子:

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

          上述SQL語句中添加了 /**/的注釋,生成的SQL語句為:

          select?*?from?t_user?WHERE?username?=???/*?and?id_no?=??*/?and?id_no?=???

          執(zhí)行時(shí),直接報(bào)錯(cuò)。

          還有一個(gè)示例:

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

          生成的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,可以通過版本管理工具來追溯歷史記錄和修改。

          小結(jié)

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


          往期推薦

          不要再用where 1=1了!有更好的寫法!

          15個(gè)必知的Mysql索引失效場(chǎng)景,別再踩坑了!

          神奇了,當(dāng)Mysql查詢條件為“>=”時(shí),竟然不走索引?!

          雪花算法:分布式唯一ID生成利器

          Maven的幾個(gè)黑科技命令,你知道嗎



          如果你覺得這篇文章不錯(cuò),那么,下篇通常會(huì)更好。添加微信好友,可備注“加群”(微信號(hào):zhuan2quan)

          一篇文章就看透技術(shù)本質(zhì)的人,
          ? 和花一輩子都看不清的人,
          ? 注定是截然不同的搬磚生涯。
          ▲?長(zhǎng)按關(guān)注”程序新視界“,洞察技術(shù)內(nèi)幕
          瀏覽 109
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产乱人乱偷精品 | 操逼视频污 | 国产欧美日韩视频在线 | 操逼的操逼的操逼的操逼的 | 天堂综合 |