不學無數——Mybatis解析判斷表達式源碼分析

Mybatis解析判斷表達式源碼分析

在咱們開發過程當中用 Mybatis 常常會用到下面的例子css

Mapper以下java

Map<String ,String > testArray(@Param("array") String [] array);

XMl中的sql以下sql

<select id="testArray" resultType="map">    select * from t_ams_ac_pmt_dtl where  cpt_pro=#{cptProp}
    <if test="array!=null and array != '' ">        and cpt_pro=#{cptProp}
    </if>
</select>

剛看上面的代碼會以爲數組怎麼能和空字符串進行一塊兒比較呢,一開始會以爲這個代碼運行起來絕對報錯,可是寫單元測試運行了一遍發現成功運行了。所以想是否是 Mybatis 在內部對數組類型的數據進行了封裝。因而有了這一次的源碼解析之旅。上網查了查發現 Mybatis 解析使用了 OGNL 。至於什麼是 OGNL 摘抄了百度百科中的一段話數組

OGNL是Object-Graph Navigation Language的縮寫,它是一種功能強大的表達式語言,經過它簡單一致的表達式語法,能夠存取對象的任意屬性,調用對象的方法,遍歷整個對象的結構圖,實現字段類型轉化等功能。它使用相同的表達式去存取對象的屬性。這樣能夠更好的取得數據。app

單元測試類以下less

@Test    public void testArray(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TBapCheckPtsTranscdMapper mapper = sqlSession.getMapper(TBapCheckPtsTranscdMapper.class);        String str= "1,2,3";        String [] strings = str.split(",");
        mapper.testArray(strings);
    }

首先咱們先來看一下 DynamicSqlSource 這個類,這個類中有個方法以下ide

@Override
  public BoundSql getBoundSql(Object parameterObject) {
    DynamicContext context = new DynamicContext(configuration, parameterObject);
    rootSqlNode.apply(context);
    SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
    Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
    SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);    for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
      boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
    }    return boundSql;
  }

其中源碼分析

rootSqlNode.apply(context);

這段代碼對SQL進行了動態的拼接,而後點進去看一下post

@Override
  public boolean apply(DynamicContext context) {    for (SqlNode sqlNode : contents) {
      sqlNode.apply(context);
    }    return true;
  }

這裏的SQL拼接運用了 組合模式 不一樣的 sqlNode 調用的方法不同,可是最後的想要結果都是同樣的:拼接SQL。例如咱們第一次進 apply 這個方法中的時候他跳轉到了單元測試

StaticTextSqlNode 這個類中調用了下面的方法

@Override
  public boolean apply(DynamicContext context) {
    context.appendSql(text);    return true;
  }

直接將SQL拼接爲

select * from t_ams_ac_pmt_dtl where  cpt_pro=#{cptProp}

而後咱們第二次循環執行發現它跳轉到了 IfSqlNode 這個類中,這是標籤爲 <if> 的判斷類,

@Override
  public boolean apply(DynamicContext context) {    if (evaluator.evaluateBoolean(test, context.getBindings())) {
      contents.apply(context);      return true;
    }    return false;
  }

在解析語句中傳了兩個參數進去

evaluator.evaluateBoolean(test, context.getBindings())
  • test :就是要解析的表達式,在此場景下就是 array!=null and array != ''

  • context.getBindings() :得到的是一個Map,其中存儲了參數 array 的所對應的值,以下所示

image

而後接下來就到了 OGNL 解析表達式了,發現最後到了 ASTNotEq 這類中

protected Object getValueBody(OgnlContext context, Object source) throws OgnlException {
        Object v1 = this._children[0].getValue(context, source);
        Object v2 = this._children[1].getValue(context, source);        return OgnlOps.equal(v1, v2) ? Boolean.FALSE : Boolean.TRUE;
    }

這裏解析分爲了兩步進行解析,上面的表達式爲 array!=null and array != '' 那麼他會根據and 進行分組將其放入 Node 數組中。

  • Node[0] : array!=null

  • Node[1] : array != ''

而後這裏面的兩個參數 v1 和 v2 分別爲左邊和右邊的參數,此時先解析 Node[0] 中的參數

  • v1 :就是參數 array 對應的數組的值

  • v2 :就是null

此時到這應該就知道爲何 String 數組爲何能和空字符串進行比較了,由於他將數組轉化爲了 Object 而後用本身寫的 equal 方法進行比較。而後進去他寫的 equal 方法中看了之後發現他對數組比較是特殊的。

  • 若是左邊是數組右邊是字符串:兩個都轉換爲 Object 而後進行 v1.getClass()==v2.getClass() 判斷

  • 若是左邊是數組右邊也是數組:先判斷兩個數組的長度是否相同,若是相同,那麼循環遍歷兩個數組進行裏面的值的比較

相關文章
相關標籤/搜索