好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

解析Mybatis判断表达式源码分析

在我们开发过程中用 mybatis 经常会用到下面的例子

mapper如下

?

1

map<string ,string > testarray( @param ( "array" ) string [] array);

xml中的sql如下

?

1

2

3

4

5

6

<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的缩写,它是一种功能强大的表达式语言,通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。这样可以更好的取得数据。

单元测试类如下

?

1

2

3

4

5

6

7

8

@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 这个类,这个类中有个方法如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

@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进行了动态的拼接,然后点进去看一下

?

1

2

3

4

5

6

7

@override

  public boolean apply(dynamiccontext context) {

   for (sqlnode sqlnode : contents) {

    sqlnode.apply(context);

   }

   return true ;

  }

这里的sql拼接运用了 组合模式 不同的 sqlnode 调用的方法不一样,但是最后的想要结果都是一样的:拼接sql。例如我们第一次进 apply 这个方法中的时候他跳转到了

statictextsqlnode 这个类中调用了下面的方法

?

1

2

3

4

5

@override

  public boolean apply(dynamiccontext context) {

   context.appendsql(text);

   return true ;

  }

直接将sql拼接为

?

1

select * from t_ams_ac_pmt_dtl where cpt_pro=#{cptprop}

然后我们第二次循环执行发现它跳转到了 ifsqlnode 这个类中,这是标签为 <if> 的判断类,

?

1

2

3

4

5

6

7

8

@override

  public boolean apply(dynamiccontext context) {

   if (evaluator.evaluateboolean(test, context.getbindings())) {

    contents.apply(context);

    return true ;

   }

   return false ;

  }

在解析语句中传了两个参数进去

?

1

evaluator.evaluateboolean(test, context.getbindings())

test :就是要解析的表达式,在此场景下就是 array!=null and array != ''
context.getbindings() :获得的是一个map,其中存储了参数 array 的所对应的值,如下所示

image

然后接下来就到了 ognl 解析表达式了,发现最后到了 astnoteq 这类中

?

1

2

3

4

5

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 数组中。

?

1

2

node[ 0 ] : array!= null

node[ 1 ] : array != ''

然后这里面的两个参数 v1 和 v2 分别为左边和右边的参数,此时先解析 node[0] 中的参数

v1 :就是参数 array 对应的数组的值 v2 :就是null

此时到这应该就知道为什么 string 数组为什么能和空字符串进行比较了,因为他将数组转化为了 object 然后用自己写的 equal 方法进行比较。然后进去他写的 equal 方法中看了以后发现他对数组比较是特殊的。

如果左边是数组右边是字符串:两个都转换为 object 然后进行 v1.getclass()==v2.getclass() 判断 如果左边是数组右边也是数组:先判断两个数组的长度是否相同,如果相同,那么循环遍历两个数组进行里面的值的比较

总结

以上所述是小编给大家介绍的解析mybatis判断表达式源码分析,希望对大家有所帮助,如果大家有任何欢迎给我留言,小编会及时回复大家的!

原文链接:https://studygolang测试数据/articles/15449

查看更多关于解析Mybatis判断表达式源码分析的详细内容...

  阅读:11次