Mybatis的mapper.xml中if標簽test判斷的用法說明
mapper.xml中if標簽test判斷的用法
1. 字符串等于條件的兩種寫法
① 將雙引號和單引號的位置互換
<if test=' testString != null and testString == "A" '>
? ?AND 表字段 = #{testString}
</if>② 加上.toString()
<if test=" testString != null and testString == 'A'.toString() ">
AND 表字段 = #{testString}
</if>2. 非空條件的判斷
長久以來,我們判斷非空非null的判斷條件都是如下所示:
<if test="xxx !=null and xxx !=''">
但是這樣的判斷只是針對String的,如果是別的類型,這個條件就不一定成立了,比如最經(jīng)典的:當是數(shù)字0時,這個判斷就會把0過濾掉,所以如果要判斷數(shù)字,我們一般會再加上一個0的判斷(這和mybatis的源碼邏輯有關,有興趣的可以去看看源碼)
<if test="xxx !=null and xxx !='' or xxx == 0">
但是如果傳進來的是數(shù)組或者集合呢?我們要再寫別的判斷嗎?能不能封裝個方法呢?
答案是可以的。
if標簽里面的test判斷是可以使用工具類來做判斷的,畢竟test后面跟的也是一個布爾值,其用法是:
<if test="@完整的包名類名@方法名(傳參)">
例如:
<if test="@com.xxx.util.MybatisTestUtil@isNotEmpty(obj)">
下面是我寫的一個簡陋的工具類,不是很全面,拋磚引玉,各位可以根據(jù)需要補充。
import java.util.Collection;
import java.util.Map;
/**
?* @description: mybatis的<if test="">標簽中使用的非空判斷工具類
?* ? ? ?使用方式:<if test="@com.xxx.xxx.util.MybatisTsetUtil@isNotEmpty(obj)">
?* @author: singleDog
?* @date: 2020/7/20
?*/
public class MybatisTestUtil {
? ? public static boolean isEmpty(Object o) {
? ? ? ? if (o == null) {
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? if (o instanceof String) {
? ? ? ? ? ? return ((String) o).trim().length() == 0;
? ? ? ? } else if (o instanceof Collection) {
? ? ? ? ? ? return ((Collection) o).isEmpty();
? ? ? ? } else if (o instanceof Map) {
? ? ? ? ? ? return ((Map) o).isEmpty();
? ? ? ? } else if (o.getClass().isArray()) {
? ? ? ? ? ? return ((Object[]) o).length == 0;
? ? ? ? } else {
? ? ? ? ? ? return false;
? ? ? ? }
? ? }
? ? public static boolean isNotEmpty(Object o) {
? ? ? ? return !isEmpty(o);
? ? }
}3. 判斷數(shù)組是否包含某個元素
<if test="list.contains(xxx)"> ?? ?//... </if>
注意,元素類型是字符串的話,參考1中的寫法,一般這樣寫
<!--包含-->
<if test="list.contains('示例元素'.toString())">
?? ?//...
</if>
<!--不包含-->
<if test="!list.contains('示例元素'.toString())">
?? ?//...
</if>mapper.xml <if test>書寫時候的一些坑
1. 分頁
map中添加了兩個int類型的數(shù)據(jù),
map.put("startNum",(page.getPageNum()-1)*page.getNumPerPage());
map.put("pageSize",page.getNumPerPage());錯誤的SQL書寫:
<if test="startNum != null ?and startNum != '' ">
? ? ? ?LIMIT #{startNum},#{pageSize}
</if>正確的SQL書寫
<if test="startNum != null">
? ? ? ?LIMIT #{startNum},#{pageSize}
</if>因為startNum里存的是int數(shù)據(jù),所以不能與空字符串進行比較,強行比較時會報錯。
2. 字符串形式的數(shù)據(jù)比較
map中添加的是一個字符串形式的“1”
map.put("uploadFlag",upload.getUploadFlag());如果想在XML中比較,以下兩種方式都可以:
2.1 test使用雙引號
比較的對象使用單引號點toString()方法:
<if test="uploadFlag=='1'.toString()"> ? ? ?and pw.id in (select pw_id from t_contract_upload) </if>
2.2 test使用單引號
比較的對象直接使用雙引號:
<if test='uploadFlag=="2" '> ? ? ?and pw.id in (select pw_id from t_contract_upload) </if>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot實現(xiàn)郵件發(fā)送的示例代碼
電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應用最廣的服務。本文詳細為大家介紹了SpringBoot實現(xiàn)發(fā)送電子郵件功能的示例代碼,需要的可以參考一下2022-04-04
Springboot從配置文件properties讀取字符串亂碼的解決
這篇文章主要介紹了Springboot從配置文件properties讀取字符串亂碼的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
springboot集成spring cache緩存示例代碼
本篇文章主要介紹了springboot集成spring cache示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

