欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis中where標簽與if標簽結合使用詳細說明

 更新時間:2023年03月03日 09:34:56   作者:鱷魚兒  
mybatis中if和where用于動態(tài)sql的條件拼接,在查詢語句中如果缺失某個條件,通過if和where標簽可以動態(tài)的改變查詢條件,下面這篇文章主要給大家介紹了關于Mybatis中where標簽與if標簽結合使用的詳細說明,需要的朋友可以參考下

前言

由于不小心將and或者or寫在了語句后面,導致mybatis無法自主判別,這種問題在新上手的同學中很是常見。下面我們探討一下,在哪些情況下Mybatis無法判斷動態(tài)SQL語句中的and或者or

使用<where>標簽

select篩選出視圖對象的參數(shù),用于給前端返回頁面參數(shù)使用。

	<sql id="selectFileVo">
        select file_id,
               uuid,
               file_name,
               file_url,
               status,
               create_time,
               update_time
        from file
    </sql>

以下代碼格式是正確,我們先觀察下and或者or的位置。

    <select id="selectFileList" parameterType="File" resultMap="FileResult">
        <include refid="selectFileVo"/>
        <where>
            <if test="fileName != null  and fileName != ''">
                and file_name like concat('%', #{fileName}, '%')
            </if>
            <if test="status != null  and status != ''">
                and status = #{status}
            </if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
                and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
            </if>
        </where>
    </select>

再看一下錯誤的寫法;

    <select id="selectFileList" parameterType="File" resultMap="FileResult">
        <include refid="selectFileVo"/>
        <where>
            <if test="fileName != null  and fileName != ''">
                file_name like concat('%', #{fileName}, '%') and
            </if>
            <if test="status != null  and status != ''">
                status = #{status} and 
            </if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
                create_time between #{params.beginCreateTime} and #{params.endCreateTime}
            </if>
        </where>
    </select>

這時候運行該代碼,當beginCreateTimeendCreateTime為空時,我們會發(fā)現(xiàn)報錯SQL執(zhí)行異常,原因是where多了一個and。

總結

<if>標簽判斷失敗后, <where> 標簽關鍵字可以自動去除掉庫表字段賦值前面的and,不會去掉語句后面的and關鍵字,即<where> 標簽只會去掉<if> 標簽語句中的最開始的and關鍵字。所以上面的寫法(and寫在后面)是不符合mybatis規(guī)范的。

不使用<where>標簽

當不使用<where>標簽時,正確的寫法可以參考以下代碼:

<select id="selectFileList" parameterType="File" resultMap="FileResult">
        <include refid="selectFileVo"/>
        where 1=1 
        <if test="fileName != null  and fileName != ''">
            and file_name like concat('%', #{fileName}, '%')
        </if>
        <if test="status != null  and status != ''">
            and status = #{status}
        </if>
        <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
            and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
        </if>
    </select>

此時我們發(fā)現(xiàn)and是寫在前面的,同時增加了1=1條件。

如果我們去掉1=1條件,同時去掉第一個<if>標簽的and

<select id="selectFileList" parameterType="File" resultMap="FileResult">
        <include refid="selectFileVo"/>
        where 
        <if test="fileName != null  and fileName != ''">
            file_name like concat('%', #{fileName}, '%')
        </if>
        <if test="status != null  and status != ''">
            and status = #{status}
        </if>
        <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
            and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
        </if>
    </select>

這種情況下,當fileName為空時,sql語句中會出現(xiàn)where and這種錯誤的語法,最終導致sql執(zhí)行異常。所以正確的代碼中,使用1=1條件,當fileName為空時,sql語句就會變成where 1=1 ,后面接不接and都能正確執(zhí)行。

在不使用<where>標簽的情況下,and寫在后面,在where條件最后增加1=1判斷,原理和上面一樣,這里就不再贅述了。

總結

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

相關文章

  • Spring Security之LogoutSuccessHandler注銷成功操作方式

    Spring Security之LogoutSuccessHandler注銷成功操作方式

    這篇文章主要介紹了Spring Security之LogoutSuccessHandler注銷成功操作方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • OpenJDK源碼調試圖文教程

    OpenJDK源碼調試圖文教程

    這篇文章主要介紹了OpenJDK源碼調試,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Lombok的詳細使用及優(yōu)缺點總結

    Lombok的詳細使用及優(yōu)缺點總結

    最近在學Mybatis,接觸到了Lombok的使用,所以寫一篇文章記錄一下,包括lombok的安裝及使用優(yōu)缺點,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • @Scheduled定時器使用注意事項及說明

    @Scheduled定時器使用注意事項及說明

    這篇文章主要介紹了@Scheduled定時器使用注意事項及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • IDEA?2022.1.4用前注意事項

    IDEA?2022.1.4用前注意事項

    這篇文章主要介紹了IDEA?2022.1.4用前注意事項,本文通過圖文并茂的形式給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • Java集合之Map接口的實現(xiàn)類精解

    Java集合之Map接口的實現(xiàn)類精解

    Map提供了一種映射關系,其中的元素是以鍵值對(key-value)的形式存儲的,能夠實現(xiàn)根據(jù)key快速查找value;Map中的鍵值對以Entry類型的對象實例形式存在;鍵(key值)不可重復,value值可以重復,一個value值可以和很多key值形成對應關系,每個建最多只能映射到一個值
    2021-09-09
  • RepeatSubmit若依框架如何防止表單重復提交注解

    RepeatSubmit若依框架如何防止表單重復提交注解

    若依框架中的@RepeatSubmit注解用于防止表單重復提交,通過在控制器方法上添加該注解,并在前端頁面和JavaScript代碼中實現(xiàn)雙重校驗,可以確保同一用戶在短時間內不會重復提交相同的表單
    2024-11-11
  • SpringAOP+RabbitMQ+WebSocket實戰(zhàn)詳解

    SpringAOP+RabbitMQ+WebSocket實戰(zhàn)詳解

    這篇文章主要介紹了SpringAOP+RabbitMQ+WebSocket實戰(zhàn)詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • springboot application.yml使用@@pom文件配置問題

    springboot application.yml使用@@pom文件配置問題

    這篇文章主要介紹了springboot application.yml使用@@pom文件配置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Mybatis 中的一對一,一對多,多對多的配置原則示例代碼

    Mybatis 中的一對一,一對多,多對多的配置原則示例代碼

    這篇文章主要介紹了 Mybatis 中的一對一,一對多,多對多的配置原則示例代碼,需要的朋友可以參考下
    2017-03-03

最新評論