Mybatis—動態sql拼接問題
背景:使用Mybatis的最近半年,經常發現一些小坑,現在總結回顧下,記個小本本,不讓它再來欺負我!
if判斷語句
一、註意??事項
1、不支持 && , 用 and or || 來做邏輯與或的判斷
2、支持以下操作符
== (對應特殊操作符 eq)
!= (對應特殊操作符 neq)
> (對應特殊操作符 gt)
>= (對應特殊操作符 gte)
< (對應特殊操作符 lt)
<= (對應特殊操作符 lte)
二、數據類型
1、字符串類型
1.1 如果不需要過濾空串的情況 僅僅判斷null即可
例如:<if test="username != null"></if>
1.1 如果需要過濾空串,添加空串判斷即可
例如:<if test="username != null and ‘‘ != username"></if>
1.2 支持String的JDK自帶方法:如果判斷字符串是否已某個特俗字符開頭,結尾等
例如:<if test="username != null and username.indexOf(‘ji‘) == 0"> </if> <!-- 是否以什麽開頭 -->
<if test="username != null and username.indexOf(‘ji‘) >= 0"> </if> <!-- 是否包含某字符 -->
<if test="username != null and username.lastIndexOf(‘ji‘) > 0"></if> <!-- 是否以什麽結尾 -->
1.3* 是否是某個特定字符串
例如:<if test="username != null and ‘hello‘ == username"></if>
或者<if test="username != null and ‘hello‘ eq username"></if>
或者<if test="username != null and ‘hello‘.toString() == username.toString()"></if>
或者<if test="‘xiaohong‘ eq username or ‘xiao‘ eq username ">
2、數字類型
2.1 僅作null判斷
例如:<if test=‘id != null‘>
2.2 數字的大小於判斷
例如:<if test=‘id != null and id > 27 ‘>
或者 <if test=‘id != null and id gt 27 ‘>
3、集合類型
3.1 判斷list是否為空
例如:例如:<if test="userList != null and userList.isEmpty()"></if>
或者<if test="userList != null and userList.size()>0"></if>
Mybatis—動態sql拼接問題