Mybatis中where標(biāo)簽與if標(biāo)簽結(jié)合使用詳細(xì)說明
前言
由于不小心將and
或者or
寫在了語句后面,導(dǎo)致mybatis無法自主判別,這種問題在新上手的同學(xué)中很是常見。下面我們探討一下,在哪些情況下Mybatis無法判斷動(dòng)態(tài)SQL語句中的and
或者or
。
使用<where>標(biāo)簽
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>
再看一下錯(cuò)誤的寫法;
<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>
這時(shí)候運(yùn)行該代碼,當(dāng)beginCreateTime
或endCreateTime
為空時(shí),我們會(huì)發(fā)現(xiàn)報(bào)錯(cuò)SQL執(zhí)行異常,原因是where多了一個(gè)and
。
總結(jié)
當(dāng)<if>
標(biāo)簽判斷失敗后, <where>
標(biāo)簽關(guān)鍵字可以自動(dòng)去除掉庫表字段賦值前面的and
,不會(huì)去掉語句后面的and
關(guān)鍵字,即<where>
標(biāo)簽只會(huì)去掉<if>
標(biāo)簽語句中的最開始的and
關(guān)鍵字。所以上面的寫法(and
寫在后面)是不符合mybatis規(guī)范的。
不使用<where>標(biāo)簽
當(dāng)不使用<where>
標(biāo)簽時(shí),正確的寫法可以參考以下代碼:
<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>
此時(shí)我們發(fā)現(xiàn)and
是寫在前面的,同時(shí)增加了1=1
條件。
如果我們?nèi)サ?code>1=1條件,同時(shí)去掉第一個(gè)<if>
標(biāo)簽的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>
這種情況下,當(dāng)fileName
為空時(shí),sql語句中會(huì)出現(xiàn)where and
這種錯(cuò)誤的語法,最終導(dǎo)致sql執(zhí)行異常。所以正確的代碼中,使用1=1
條件,當(dāng)fileName
為空時(shí),sql語句就會(huì)變成where 1=1
,后面接不接and
都能正確執(zhí)行。
在不使用<where>
標(biāo)簽的情況下,and
寫在后面,在where
條件最后增加1=1
判斷,原理和上面一樣,這里就不再贅述了。
總結(jié)
到此這篇關(guān)于Mybatis中where標(biāo)簽與if標(biāo)簽結(jié)合使用的文章就介紹到這了,更多相關(guān)Mybatis使用where標(biāo)簽與if標(biāo)簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Security之LogoutSuccessHandler注銷成功操作方式
這篇文章主要介紹了Spring Security之LogoutSuccessHandler注銷成功操作方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Lombok的詳細(xì)使用及優(yōu)缺點(diǎn)總結(jié)
最近在學(xué)Mybatis,接觸到了Lombok的使用,所以寫一篇文章記錄一下,包括lombok的安裝及使用優(yōu)缺點(diǎn),感興趣的朋友跟隨小編一起看看吧2021-07-07@Scheduled定時(shí)器使用注意事項(xiàng)及說明
這篇文章主要介紹了@Scheduled定時(shí)器使用注意事項(xiàng)及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08RepeatSubmit若依框架如何防止表單重復(fù)提交注解
若依框架中的@RepeatSubmit注解用于防止表單重復(fù)提交,通過在控制器方法上添加該注解,并在前端頁面和JavaScript代碼中實(shí)現(xiàn)雙重校驗(yàn),可以確保同一用戶在短時(shí)間內(nèi)不會(huì)重復(fù)提交相同的表單2024-11-11SpringAOP+RabbitMQ+WebSocket實(shí)戰(zhàn)詳解
這篇文章主要介紹了SpringAOP+RabbitMQ+WebSocket實(shí)戰(zhàn)詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11springboot application.yml使用@@pom文件配置問題
這篇文章主要介紹了springboot application.yml使用@@pom文件配置問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Mybatis 中的一對一,一對多,多對多的配置原則示例代碼
這篇文章主要介紹了 Mybatis 中的一對一,一對多,多對多的配置原則示例代碼,需要的朋友可以參考下2017-03-03