mybatis的動(dòng)態(tài)sql之if test的使用說(shuō)明
參數(shù)為String,if test讀取該參數(shù)代碼
<select id="getMaxDepartId" parameterType="java.lang.String" resultType="java.lang.String"> SELECT MAX(DEPART_ID) FROM T_P_DEPART <where> <if test="_parameter!=null and _parameter!=''"> AND DEPART_PID = #{departId,jdbcType=VARCHAR} </if> <if test="_parameter==null or _parameter==''"> AND DEPART_PID IS NULL </if> </where> </select>
參數(shù)為pojo , if test讀取該參數(shù)代碼
<select id="findShopByName" parameterType="ShopVo" resultType="ShopCustomer"> select * from shop <where> <if test="shopCustomer.shopname!=null and shopCustomer.shopname!=''"> shop.shopname like '%${shopCustomer.shopname}%' </if> <if test="shopCustomer.shopname==null or shopCustomer.shopname==''"> AND shop.shopname is null </if> </where> </select>
補(bǔ)充:關(guān)于mybatis中 if test的條件怎么寫
1.mybatis 中 的 if test寫法
1.1官方文檔上對(duì)于if是這么寫的
<if test="title != null"> AND title like #{title} </if>
參考官方文檔:
實(shí)際項(xiàng)目中會(huì)有這種情況: 頁(yè)面上title字段輸入某個(gè)值進(jìn)行查詢,手動(dòng)將輸入框中的值刪除,然后再次查詢,發(fā)現(xiàn)結(jié)果不正確,究其原因是應(yīng)為title傳入了空串" " 這樣在mybatis配置文件中就會(huì)用空串進(jìn)行查詢,導(dǎo)致出現(xiàn)錯(cuò)誤結(jié)果
1.2建議寫法
<if test="title != null and title != ''" > AND title like #{title} </if>
2.使用mybatis 做修改時(shí)將字段置空
if中如果傳入的參數(shù)如果為空,那么將不會(huì)執(zhí)行if中的語(yǔ)句
解決辦法:
<update id="updateObject" parameterType="*.*.Object" > update table <set> <if test="Object.fullName == null or Object.fullName ==''"> full_name = null, </if> <if test="Object.fullName != null and Object.fullName !=''"> full_name = #{companyOrg.fullName}, </if> <if test="Object.level == null or Object.level ==''"> level = null, </if> <if test="Object.level == 0 "> level = null, </if> <if test="Object.level != null and Object.level !='' and Object.level != 0 "> level = #{companyOrg.level}, </if> </set> where 1=1 and id =#{companyOrg.id} </update>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- mybatis的動(dòng)態(tài)sql詳解(精)
- MyBatis 動(dòng)態(tài)拼接Sql字符串的問(wèn)題
- MyBatis 執(zhí)行動(dòng)態(tài) SQL語(yǔ)句詳解
- Mybatis中動(dòng)態(tài)SQL,if,where,foreach的使用教程詳解
- MyBatis執(zhí)行動(dòng)態(tài)SQL的方法
- mybatis動(dòng)態(tài)sql之Map參數(shù)的講解
- MyBatis動(dòng)態(tài)Sql之if標(biāo)簽的用法詳解
- MyBatis if choose 動(dòng)態(tài) SQL的實(shí)現(xiàn)
相關(guān)文章
Java Socket編程簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java Socket編程簡(jiǎn)介的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05Struts2學(xué)習(xí)筆記(4)-通配符的使用
本文主要介紹Struts2中通配符的使用,簡(jiǎn)單實(shí)用,希望能給大家做一個(gè)參考。2016-06-06五分鐘教你手寫 SpringBoot 本地事務(wù)管理實(shí)現(xiàn)
這篇文章主要介紹了五分鐘教你手寫 SpringBoot 本地事務(wù)管理實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02SpringBoot項(xiàng)目解決跨域的四種方案分享
在用SpringBoot開(kāi)發(fā)后端服務(wù)時(shí),我們一般是提供接口給前端使用,但前端通過(guò)瀏覽器調(diào)我們接口時(shí),瀏覽器會(huì)有個(gè)同源策略的限制,即協(xié)議,域名,端口任一不一樣時(shí)都會(huì)導(dǎo)致跨域,這篇文章主要介紹跨域的幾種常用解決方案,希望對(duì)大家有所幫助2023-05-05java?Long類型轉(zhuǎn)為json后數(shù)據(jù)損失精度的處理方式
這篇文章主要介紹了java?Long類型轉(zhuǎn)為json后數(shù)據(jù)損失精度的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01