Mybatis動(dòng)態(tài)SQL之where標(biāo)簽用法說(shuō)明
關(guān)于where標(biāo)簽用法
xml映射文件部分內(nèi)容:
<select id="selectIfCondition" resultType="com.heiketu.testpackage.pojo.Product">
? ? SELECT
? ? ? ? prod_id prodId,
? ? ? ? vend_id vendId,
? ? ? ? prod_name prodName,
? ? ? ? prod_desc prodDesc
? ? FROM Products
? ? <where>
? ? ? ? <if test="prodId != null and prodId != ''">
? ? ? ? ? ? AND prod_id = #{prodId}
? ? ? ? </if>
? ? ? ? <if test="prodName != null and prodName != ''">
? ? ? ? ? ? AND prod_name = #{prodName}
? ? ? ? </if>
? ? </where>
</select>映射文件中的where標(biāo)簽可以過(guò)濾掉條件語(yǔ)句中的第一個(gè)and或or關(guān)鍵字。以上SQL當(dāng)prodId!=null時(shí)會(huì)被mybatis的where標(biāo)簽去除掉多余的and關(guān)鍵字,生成的sql如下:
SELECT ? ? prod_id prodId, ? ? vend_id vendId, ? ? prod_name prodName, ? ? prod_desc prodDesc FROM Products WHERE ? ? prod_id = ? AND ? ? prod_name = ?
注: where標(biāo)簽只能去除第一個(gè)條件中出現(xiàn)的前置 and 關(guān)鍵字。像以下情況,where就無(wú)能為力了:
<select id="selectIfCondition" resultType="com.heiketu.testpackage.pojo.Product">
? ? SELECT
? ? ? ? prod_id prodId,
? ? ? ? vend_id vendId,
? ? ? ? prod_name prodName,
? ? ? ? prod_desc prodDesc
? ? FROM Products
? ? <where>
? ? ? ? <if test="prodId != null and prodId != ''">
? ? ? ? ? ? prod_id = #{prodId} AND
? ? ? ? </if>
? ? ? ? <if test="prodName != null and prodName != ''">
? ? ? ? ? ? prod_name = #{prodName} AND
? ? ? ? </if>
? ? </where>
</select>Mybatis會(huì)把次SQL語(yǔ)句拼接成:
SELECT ? ? prod_id prodId, ? ? vend_id vendId, ? ? prod_name prodName, ? ? prod_desc prodDesc FROM Products WHERE ? ? prod_id = ? AND ? ? prod_name = ? AND
where無(wú)法去除掉后面的and關(guān)鍵字,此時(shí)sql語(yǔ)句出現(xiàn)語(yǔ)法錯(cuò)誤。
要想解決以上where標(biāo)簽無(wú)法處理的問(wèn)題,可以考慮使用trim標(biāo)簽。
使用where標(biāo)簽及一些注意點(diǎn)
where標(biāo)簽簡(jiǎn)單使用
自己習(xí)慣性配上映射對(duì)象 這個(gè)大家根據(jù)自己習(xí)慣去寫(xiě),可以直接使用map去接返回參數(shù)
// 返回對(duì)象映射 <resultMap id="ResultMap" type="com.xxx.root.ConnRoot"> ? ? ? ? <result column="CONN_ID" property="connId" jdbcType="NUMERIC"/> ? ? ? ? <result column="CONN_NO" property="connNo" jdbcType="VARCHAR"/> ? ? ? ? <result column="STA_ID" property="staId" jdbcType="NUMERIC"/> ? ? ? ? <result column="RUN_MODE" property="runMode" jdbcType="VARCHAR"/> ? ? ? ? <result column="SRV_ID" property="srvId" jdbcType="NUMERIC"/> ? ? ? ? <result column="LINE_ID" property="lineId" jdbcType="NUMERIC"/> </resultMap>
// 此處為使用 where相關(guān)SQL
<select id="listConnection" parameterType="java.util.Map" resultMap="ResultMap">
? ? ? ? SELECT
? ? ? ? A.CONN_ID,
? ? ? ? A.CONN_NO,
? ? ? ? A.STA_ID,
? ? ? ? A.RUN_MODE,
? ? ? ? A.SRV_ID,
? ? ? ? A.LINE_ID
? ? ? ? FROM CONN A?
? ? ? ? where 1=1?
?? ??? ?<if test="srvId!= null" >
?? ??? ?AND SRV_ID= #{srvId,jdbcType=DECIMAL}
?? ??? ?</if>
</select>jdbcType 加上可以避免在映射時(shí)出現(xiàn)某些錯(cuò)誤
jdbc出現(xiàn)的需求及 解決什么問(wèn)題:當(dāng)mybatis執(zhí)行mapping文件時(shí),如果某個(gè)映射的參數(shù)為空,無(wú)法確定他的類(lèi)型時(shí)需要用到j(luò)dbcType來(lái)確定類(lèi)型。
// 此處為使用 where 標(biāo)簽SQL
<select id="listConnection" parameterType="java.util.Map" resultMap="ResultMap">
? ? ? ? SELECT
? ? ? ? A.CONN_ID,
? ? ? ? A.CONN_NO,
? ? ? ? A.STA_ID,
? ? ? ? A.RUN_MODE,
? ? ? ? A.SRV_ID,
? ? ? ? A.LINE_ID
? ? ? ? FROM CONN A
?? ??? ?<where>?
?? ??? ??? ?<if test="srvId!= null">
?? ??? ??? ? A.SRV_ID= #{srvId,jdbcType=DECIMAL}
?? ??? ??? ?</if>?
?? ??? ??? ?<if test="runMode!= null">
?? ??? ??? ?AND ?A.RUN_MODE= #{runMode,jdbcType=VARCHAR}
?? ??? ??? ?</if>
?? ??? ?</where>
</select>到這里大家可以看到where標(biāo)簽使用和不適用的一些區(qū)別
首先是 where后面加 1=1,在使用where關(guān)鍵字時(shí),因?yàn)闂l件不確定存在不存在,要加上 1=1這個(gè)垃圾條件來(lái)解決條件為空時(shí)where失去存在的意義;但是< where >標(biāo)簽中不需要加 1=1這個(gè)垃圾條件,因?yàn)閣here標(biāo)簽會(huì)自動(dòng)處理只有在一個(gè)以上的< if >標(biāo)簽有值時(shí)才去插入“WHERE”子句到SQL中。
然后是另一個(gè)問(wèn)題條件AND 條件,where標(biāo)簽會(huì)自動(dòng)去除首個(gè)條件中的AND,這樣大家就可以不用去管第一個(gè)條件是否為空可以直接給所有if標(biāo)簽中的條件都加上AND,但是自己還是習(xí)慣的將第一個(gè)條件去掉AND ,但是如果遇到前邊已經(jīng)有一個(gè)存在的條件的情況下,那接下來(lái)所有的if中的條件開(kāi)頭AND是必須要加的,如:
<select id="listConnection" parameterType="java.util.Map" resultMap="ResultMap">
? ? ? ? SELECT
? ? ? ? A.CONN_ID,
? ? ? ? A.CONN_NO,
? ? ? ? A.STA_ID,
? ? ? ? A.RUN_MODE,
? ? ? ? A.SRV_ID,
? ? ? ? A.LINE_ID
? ? ? ? FROM CONN A
?? ??? ?<where>?
?? ??? ??? ?A.CONN_ID=1
?? ??? ??? ?<if test="srvId!= null">
?? ??? ??? ?AND ? A.SRV_ID= #{srvId,jdbcType=DECIMAL}
?? ??? ??? ?</if>?
?? ??? ??? ?<if test="runMode!= null">
?? ??? ??? ?AND ?A.RUN_MODE= #{runMode,jdbcType=VARCHAR}
?? ??? ??? ?</if>
?? ??? ?</where>
</select>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Security基本架構(gòu)與初始化操作流程詳解
這篇文章主要介紹了Spring Security基本架構(gòu)與初始化操作流程,Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問(wèn)控制解決方案的安全框架2023-03-03
使用cmd根據(jù)WSDL網(wǎng)址生成java客戶端代碼的實(shí)現(xiàn)
這篇文章主要介紹了使用cmd根據(jù)WSDL網(wǎng)址生成java客戶端代碼的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
利用EasyExcel導(dǎo)出帶有選擇校驗(yàn)框的excel
EasyExcel是一個(gè)輕量級(jí)的Excel處理工具,支持Excel?2003(xls)和Excel?2007及以上版本(xlsx)的文件格式,本文將利用EasyExcel導(dǎo)出帶有選擇校驗(yàn)框的excel,需要的可以參考下2024-12-12
RabbitMQ中的Connection和Channel信道詳解
這篇文章主要介紹了RabbitMQ中的Connection和Channel信道詳解,信道是建立在 Connection 之上的虛擬連接,RabbitMQ 處理的每條 AMQP 指令都是通過(guò)信道完成的,需要的朋友可以參考下2023-08-08
java實(shí)現(xiàn)MD5加密的方法小結(jié)
這篇文章主要介紹了java實(shí)現(xiàn)MD5加密的方法,結(jié)合具體實(shí)例形式總結(jié)分析了java實(shí)現(xiàn)md5加密的常用操作技巧與使用方法,需要的朋友可以參考下2017-10-10
springmvc無(wú)法訪問(wèn)/WEB-INF/views下的jsp的解決方法
本篇文章主要介紹了springmvc無(wú)法訪問(wèn)/WEB-INF/views下的jsp的解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
Java報(bào)錯(cuò)Non-terminating?decimal?expansion解決分析
這篇文章主要為大家介紹了Java報(bào)錯(cuò)Non-terminating?decimal?expansion解決方案及原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

