最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis where 標簽使用

 更新時間:2022年03月03日 11:09:24   作者:潘甜甜!  
where標記的作用類似于動態(tài)sql中的set標記,本文主要介紹了mybatis where 標簽使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

我們經(jīng)常在動態(tài)構造sql時,為防止注入或防止語句不當時會使用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標簽可以簡化這條語句

<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í)行時,如果id這個參數(shù)為null,則這條語句的執(zhí)行結果為

select * from zc_chat_group where leader_num = ‘xx' and group_type = ‘xx'

這個‘where’標簽會知道如果它包含的標簽中有返回值的話,它就會插入一個‘where’。此外,如果標簽返回的內(nèi)容是以AND 或OR開頭的,則會把它去除掉。

Mybatis where標簽的使用

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

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

所以說,上面的兩種寫法都是可以了,Mybatis的where標簽會替我們做一些事情。
但需要注意的是:where標簽只會 智能的去除(忽略)首個滿足條件語句的前綴。所以建議在使用where標簽時,每個語句都最好寫上 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標簽時,建議將所有條件都添加上and或or;

進階:自定義trim標簽

上面使用where標簽可以達到拼接條件語句時,自動去掉首個條件的and或or,那么如果是其他自定義的關鍵字是否也能去掉呢?
此時,where標簽就無能為力了,該trim標簽上場了,它也可以實現(xiàn)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標簽,發(fā)現(xiàn)執(zhí)行效果完全一樣。而且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 = ? 

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

同樣會導致報錯。

這是因為我們使用 XML 方式配置 SQL 時,如果在 where 標簽之后添加了注釋,那么當有子元素滿足條件時,除了 < !-- --> 注釋會被 where 忽略解析以外,其它注釋例如 // 或 /**/ 或 -- 等都會被 where 當成首個子句元素處理,導致后續(xù)真正的首個 AND 子句元素或 OR 子句元素沒能被成功替換掉前綴,從而引起語法錯誤。
同時,個人在實踐中也經(jīng)常發(fā)現(xiàn)因為在XML中使用注釋不當導致SQL語法錯誤或執(zhí)行出錯誤的結果。強烈建議,非必要,不要在XML中注釋掉SQL,可以通過版本管理工具來追溯歷史記錄和修改。

小結

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

到此這篇關于mybatis where 標簽使用的文章就介紹到這了,更多相關mybatis where標簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 從零開始讓你的Spring?Boot項目跑在Linux服務器

    從零開始讓你的Spring?Boot項目跑在Linux服務器

    這篇文章主要給大家介紹了如何從零開始讓你的Spring?Boot項目跑在Linux服務器的相關資料,由于springboot是內(nèi)嵌了tomcat,所以可以直接將項目打包上傳至服務器上,需要的朋友可以參考下
    2021-11-11
  • Java編程實現(xiàn)服務器端支持斷點續(xù)傳的方法(可支持快車、迅雷)

    Java編程實現(xiàn)服務器端支持斷點續(xù)傳的方法(可支持快車、迅雷)

    這篇文章主要介紹了Java編程實現(xiàn)服務器端支持斷點續(xù)傳的方法,涉及Java文件傳輸?shù)南嚓P技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • 網(wǎng)絡爬蟲案例解析

    網(wǎng)絡爬蟲案例解析

    本文主要介紹了網(wǎng)絡爬蟲的小案例。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • java 實現(xiàn)DES 加密解密的示例

    java 實現(xiàn)DES 加密解密的示例

    這篇文章主要介紹了java 實現(xiàn)DES 加密解密的示例代碼,幫助大家更好的理解和使用Java進行加解密,感興趣的朋友可以了解下
    2020-12-12
  • Spring bean注冊到容器的總結

    Spring bean注冊到容器的總結

    這篇文章主要介紹了Spring bean注冊到容器的總結,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • Java中將File轉化為MultipartFile的操作

    Java中將File轉化為MultipartFile的操作

    這篇文章主要介紹了Java中將File轉化為MultipartFile的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Spring bean的實例化和IOC依賴注入詳解

    Spring bean的實例化和IOC依賴注入詳解

    這篇文章主要介紹了Spring bean的實例化和IOC依賴注入詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Java使用BouncyCastle加密

    Java使用BouncyCastle加密

    本文主要介紹了Java使用BouncyCastle加密,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • springboot+camunda實現(xiàn)工作流的流程分析

    springboot+camunda實現(xiàn)工作流的流程分析

    Camunda是基于Java語言,支持BPMN標準的工作流和流程自動化框架,并且還支持CMMN規(guī)范,DMN規(guī)范,本文給大家介紹springboot+camunda實現(xiàn)工作流的流程分析,感興趣的朋友一起看看吧
    2021-12-12
  • SpringBoot項目中連接SQL Server的三種方式

    SpringBoot項目中連接SQL Server的三種方式

    連接SQL Server是許多Spring Boot項目中常見的需求之一,本文主要介紹了SpringBoot項目中連接SQL Server的三種方式,具有一定的參考價值 ,感興趣的可以了解一下
    2023-09-09

最新評論

荣昌县| 宣城市| 淳化县| 汝州市| 来宾市| 晋城| 卫辉市| 翁源县| 渝中区| 务川| 汉中市| 奎屯市| 双城市| 桃园市| 务川| 翁牛特旗| 余干县| 犍为县| 万载县| 进贤县| 漳平市| 绥芬河市| 镇远县| 南阳市| 安仁县| 济阳县| 探索| 浦北县| 龙井市| 金湖县| 仙游县| 松原市| 中山市| 安新县| 军事| 揭东县| 仪征市| 南木林县| 赤峰市| 会宁县| 横山县|