Mybatis: sql中if 判斷的坑

“原創(chuàng)精選,轉(zhuǎn)載注明出處,三克油”

前言

  • Mbatis 的script標(biāo)簽可以支持很多動(dòng)態(tài)SQL查詢、在使用if test判斷的時(shí)候、會(huì)有一些意向不到的坑

正文

  • 我們一般在Mbatis中做update更新時(shí)、都會(huì)加上使用if test判斷、防止更新空值、一般正常都是像name這種既判斷NULL又判斷''
  • 但是對(duì)于int類型的sex字段、如果加上 sex != ''條件時(shí)、當(dāng)sex值為0時(shí)、此sql不會(huì)被拼加上、也不會(huì)被執(zhí)行更新
<if test="name !=null and name !='' ">
    name = #{name},
</if>
<if test="sex !=null">
    sex = #{sex},
</if>
  • 以上是問題以及現(xiàn)象、現(xiàn)在讓我們來看一下Mbatis源碼、為什么會(huì)出現(xiàn)這種情況
  1. 首先在mbatis中找到啊以上包路徑、此路徑為解析script標(biāo)簽時(shí)用到的類
org.apache.ibatis.scripting
  1. 過程不在細(xì)說、對(duì)于mbatis來說、script標(biāo)簽的動(dòng)態(tài)SQL、都是遍歷條件做SQL拼接而成的
  2. 我們直接看org.apache.ibatis.scripting.xmltags.IfSqlNode這個(gè)類、此類是script標(biāo)簽中if標(biāo)簽的判斷方式、其中evaluateBoolean返回true時(shí)、if標(biāo)簽下的SQL拼接生效
public boolean apply(DynamicContext context) {
    if (evaluator.evaluateBoolean(test, context.getBindings())) {
        contents.apply(context);
        return true;
    }
    return false;
}
  1. 再具體看org.apache.ibatis.scripting.xmltags.ExpressionEvaluator :evaluateBoolean 這個(gè)方法、我們看到了如果是number類型、會(huì)與0做判斷比較
public boolean evaluateBoolean(String expression, Object parameterObject) {
    Object value = OgnlCache.getValue(expression, parameterObject);
    if (value instanceof Boolean) return (Boolean) value;
    if (value instanceof Number) return !new BigDecimal(String.valueOf(value)).equals(BigDecimal.ZERO);
    return value != null;
}
  1. 由此可以發(fā)現(xiàn)、當(dāng)動(dòng)態(tài)sql參數(shù)是int類型、并且傳入0時(shí)、是使用OgnlCache獲取的value、而OgnlCache是對(duì)OGNL的封裝
  2. OGNL的官網(wǎng)上面可以看到如下解釋
Any object can be used where a boolean is required. OGNL interprets objects as booleans like this:
· If the object is a Boolean, its value is extracted and returned;
· If the object is a Number, its double-precision floating-point value is compared with zero; non-zero is treated as true, zero as false;
· If the object is a Character, its boolean value is true if and only if its char value is non-zero;
Otherwise, its boolean value is true if and only if it is non-null.

結(jié)尾

For Future.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,993評(píng)論 0 0
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,916評(píng)論 0 33
  • "Innovation distinguishes between a leader and a follower...
    均不若閱讀 865評(píng)論 0 1
  • 終于,姥姥也走了 小的時(shí)候每次被爸媽罵的時(shí)候,我都會(huì)邊哭邊喊:我要告訴姥姥。總覺得有姥姥在,我什么都不怕。 可是以...
    wwwbb閱讀 470評(píng)論 0 2
  • 00 昨天加了初中同學(xué)的微信群,也得知了當(dāng)時(shí)班里最漂亮的女生小琪要結(jié)婚的消息,當(dāng)大家都在起哄著要喜糖的時(shí)候,我卻想...
    王阿萌閱讀 2,735評(píng)論 0 1

友情鏈接更多精彩內(nèi)容