不學無數——Mybatis解析判斷表示式原始碼分析
Mybatis解析判斷表示式原始碼分析
在我們開發過程中用 Mybatis
經常會用到下面的例子
Mapper如下
Map<String ,String > testArray(@Param("array") String [] array);
XMl中的sql如下
<select id="testArray" resultType="map"> select * from t_ams_ac_pmt_dtl wherecpt_pro=#{cptProp} <if test="array!=null and array != '' "> and cpt_pro=#{cptProp} </if> </select>
剛看上面的程式碼會覺得陣列怎麼能和空字串進行一起比較呢,一開始會覺得這個程式碼執行起來絕對報錯,但是寫單元測試運行了一遍發現成功運行了。因此想是不是 Mybatis
在內部對陣列型別的資料進行了封裝。於是有了這一次的原始碼解析之旅。上網查了查發現 Mybatis
解析使用了 OGNL
。至於什麼是 OGNL
摘抄了百度百科中的一段話
OGNL是Object-Graph Navigation Language的縮寫,它是一種功能強大的表示式語言,通過它簡單一致的表示式語法,可以存取物件的任意屬性,呼叫物件的方法,遍歷整個物件的結構圖,實現欄位型別轉化等功能。它使用相同的表示式去存取物件的屬性。這樣可以更好的取得資料。
單元測試類如下
@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
這個類,這個類中有個方法如下
@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進行了動態的拼接,然後點進去看一下
@Override public boolean apply(DynamicContext context) { for (SqlNode sqlNode : contents) { sqlNode.apply(context); } return true; }
這裡的SQL拼接運用了 ofollow,noindex">組合模式 不同的 sqlNode
呼叫的方法不一樣,但是最後的想要結果都是一樣的:拼接SQL。例如我們第一次進 apply
這個方法中的時候他跳轉到了
StaticTextSqlNode
這個類中呼叫了下面的方法
@Override public boolean apply(DynamicContext context) { context.appendSql(text); return true; }
直接將SQL拼接為
select * from t_ams_ac_pmt_dtl wherecpt_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()
判斷 - 如果左邊是陣列右邊也是陣列:先判斷兩個陣列的長度是否相同,如果相同,那麼迴圈遍歷兩個陣列進行裡面的值的比較