溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2022-05-25 13:45:05 來源:億速云 閱讀:518 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Mybatis的where標(biāo)簽如何使用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Mybatis的where標(biāo)簽如何使用”吧!

原始的手動拼接

在不使用Mybatis的where標(biāo)簽時,我們通常是根據(jù)查詢條件進行手動拼接,也就是用到了上面提到的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>

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

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

當(dāng)添加上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';

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

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

為了能達到MySQL性能的調(diào)優(yōu),我們可以基于Mybatis的where標(biāo)簽來進行實現(xiàn)。where標(biāo)簽是頂層的遍歷標(biāo)簽,需要配合if標(biāo)簽使用,單獨使用無意義。通常有下面兩種實現(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>

仔細觀察會發(fā)現(xiàn),這兩種方式的區(qū)別在于第一if條件中的SQL語句是否有and。

這里就涉及到where標(biāo)簽的兩個特性:

  • 第一,只有if標(biāo)簽有內(nèi)容的情況下才會插入where子句;

  • 第二,若子句的開通為 “AND” 或 “OR”,where標(biāo)簽會將它替換去除;

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

但需要注意的是:where標(biāo)簽只會 智能的去除(忽略)首個滿足條件語句的前綴。所以建議在使用where標(biāo)簽時,每個語句都最好寫上 and 前綴或者 or 前綴,否則像以下寫法就會出現(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 = ?

很顯然,語法是錯誤的。

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

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

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

此時,where標(biāo)簽就無能為力了,該trim標(biāo)簽上場了,它也可以實現(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語句的坑

另外,在使用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 = ?

執(zhí)行時,直接報錯。

還有一個示例:

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

同樣會導(dǎo)致報錯。

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

同時,個人在實踐中也經(jīng)常發(fā)現(xiàn)因為在XML中使用注釋不當(dāng)導(dǎo)致SQL語法錯誤或執(zhí)行出錯誤的結(jié)果。強烈建議,非必要,不要在XML中注釋掉SQL,可以通過版本管理工具來追溯歷史記錄和修改。

感謝各位的閱讀,以上就是“Mybatis的where標(biāo)簽如何使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Mybatis的where標(biāo)簽如何使用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI