在我們開(kāi)發(fā)過(guò)程中用 Mybatis 經(jīng)常會(huì)用到下面的例子
Mapper如下
Map<String ,String > testArray(@Param("array") String [] array);
XMl中的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>
剛看上面的代碼會(huì)覺(jué)得數(shù)組怎么能和空字符串進(jìn)行一起比較呢,一開(kāi)始會(huì)覺(jué)得這個(gè)代碼運(yùn)行起來(lái)絕對(duì)報(bào)錯(cuò),但是寫(xiě)單元測(cè)試運(yùn)行了一遍發(fā)現(xiàn)成功運(yùn)行了。因此想是不是 Mybatis 在內(nèi)部對(duì)數(shù)組類(lèi)型的數(shù)據(jù)進(jìn)行了封裝。于是有了這一次的源碼解析之旅。上網(wǎng)查了查發(fā)現(xiàn) Mybatis 解析使用了 OGNL 。至于什么是 OGNL 摘抄了百度百科中的一段話
OGNL是Object-Graph Navigation Language的縮寫(xiě),它是一種功能強(qiáng)大的表達(dá)式語(yǔ)言,通過(guò)它簡(jiǎn)單一致的表達(dá)式語(yǔ)法,可以存取對(duì)象的任意屬性,調(diào)用對(duì)象的方法,遍歷整個(gè)對(duì)象的結(jié)構(gòu)圖,實(shí)現(xiàn)字段類(lèi)型轉(zhuǎn)化等功能。它使用相同的表達(dá)式去存取對(duì)象的屬性。這樣可以更好的取得數(shù)據(jù)。
單元測(cè)試類(lèi)如下
@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); }
首先我們先來(lái)看一下 DynamicSqlSource 這個(gè)類(lèi),這個(gè)類(lèi)中有個(gè)方法如下
@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);
這段代碼對(duì)SQL進(jìn)行了動(dòng)態(tài)的拼接,然后點(diǎn)進(jìn)去看一下
@Override public boolean apply(DynamicContext context) { for (SqlNode sqlNode : contents) { sqlNode.apply(context); } return true; }
這里的SQL拼接運(yùn)用了 組合模式 不同的 sqlNode 調(diào)用的方法不一樣,但是最后的想要結(jié)果都是一樣的:拼接SQL。例如我們第一次進(jìn) apply 這個(gè)方法中的時(shí)候他跳轉(zhuǎn)到了
StaticTextSqlNode 這個(gè)類(lèi)中調(diào)用了下面的方法
@Override public boolean apply(DynamicContext context) { context.appendSql(text); return true; }
直接將SQL拼接為
select * from t_ams_ac_pmt_dtl where cpt_pro=#{cptProp}
然后我們第二次循環(huán)執(zhí)行發(fā)現(xiàn)它跳轉(zhuǎn)到了 IfSqlNode 這個(gè)類(lèi)中,這是標(biāo)簽為 <if> 的判斷類(lèi),
@Override public boolean apply(DynamicContext context) { if (evaluator.evaluateBoolean(test, context.getBindings())) { contents.apply(context); return true; } return false; }
在解析語(yǔ)句中傳了兩個(gè)參數(shù)進(jìn)去
evaluator.evaluateBoolean(test, context.getBindings())
test :就是要解析的表達(dá)式,在此場(chǎng)景下就是 array!=null and array != ''
context.getBindings() :獲得的是一個(gè)Map,其中存儲(chǔ)了參數(shù) array 的所對(duì)應(yīng)的值,如下所示
image
然后接下來(lái)就到了 OGNL 解析表達(dá)式了,發(fā)現(xiàn)最后到了 ASTNotEq 這類(lèi)中
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; }
這里解析分為了兩步進(jìn)行解析,上面的表達(dá)式為 array!=null and array != ''
那么他會(huì)根據(jù)and 進(jìn)行分組將其放入 Node 數(shù)組中。
Node[0] : array!=nullNode[1] : array != ''
然后這里面的兩個(gè)參數(shù) v1 和 v2 分別為左邊和右邊的參數(shù),此時(shí)先解析 Node[0] 中的參數(shù)
此時(shí)到這應(yīng)該就知道為什么 String 數(shù)組為什么能和空字符串進(jìn)行比較了,因?yàn)樗麑?shù)組轉(zhuǎn)化為了 Object 然后用自己寫(xiě)的 equal 方法進(jìn)行比較。然后進(jìn)去他寫(xiě)的 equal 方法中看了以后發(fā)現(xiàn)他對(duì)數(shù)組比較是特殊的。
v1.getClass()==v2.getClass()
判斷總結(jié)
以上所述是小編給大家介紹的解析Mybatis判斷表達(dá)式源碼分析,希望對(duì)大家有所幫助,如果大家有任何歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
新聞熱點(diǎn)
疑難解答
圖片精選