MyBatis拦截器及分页插件

2019-05-18 12:48

Mybatis拦截器介绍

1.1 目录

1.1 目录 1.2 前言

1.3 Interceptor接口 1.4 注册拦截器

1.5 Mybatis可拦截的方法 1.6 利用拦截器进行分页

1.2 前言

拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法。Mybatis拦截器设计的一个初衷就是为了供用户在某些时候可以实现自己的逻辑而不必去动Mybatis固有的逻辑。打个比方,对于Executor,Mybatis中有几种实现:BatchExecutor、ReuseExecutor、SimpleExecutor和CachingExecutor。这个时候如果你觉得这几种实现对于Executor接口的query方法都不能满足你的要求,那怎么办呢?是要去改源码吗?当然不。我们可以建立一个Mybatis拦截器用于拦截Executor接口的query方法,在拦截之后实现自己的query方法逻辑,之后可以选择是否继续执行原来的query方法。

1.3 Interceptor接口

对于拦截器Mybatis为我们提供了一个Interceptor接口,通过实现该接口就可以定义我们自己的拦截器。我们先来看一下这个接口的定义: Java代码

1. package org.apache.ibatis.plugin; 2.

3. import java.util.Properties; 4.

5. public interface Interceptor { 6.

7. Object intercept(Invocation invocation) throws Throwable; 8.

9. Object plugin(Object target); 10.

11. void setProperties(Properties properties); 12. 13. }

我们可以看到在该接口中一共定义有三个方法,intercept、plugin和setProperties。plugin方法是拦截器用于封装目标对象的,通过该方法我们可以返回目标对象本身,也可以返回一个它的代理。当返回的是代理的时候我们可以对其中的方法进行拦截来调用intercept方法,当然也可以调用其他方法,这点将在后文讲解。setProperties方法是用于在Mybatis配置文件中指定一些属性的。

定义自己的Interceptor最重要的是要实现plugin方法和intercept方法,在plugin方法中我们可以决定是否要进行拦截进而决定要返回一个什么样的目标对象。而intercept方法就是要进行拦截的时候要执行的方法。

对于plugin方法而言,其实Mybatis已经为我们提供了一个实现。Mybatis中有一个叫做Plugin的类,里面有一个静态方法wrap(Object target,Interceptor interceptor),通过该方法可以决定要返回的对象是目标对象还是对应的代理。这里我们先来看一下Plugin的源码: Java代码

1. package org.apache.ibatis.plugin; 2.

3. import java.lang.reflect.InvocationHandler; 4. import java.lang.reflect.Method; 5. import java.lang.reflect.Proxy; 6. import java.util.HashMap; 7. import java.util.HashSet; 8. import java.util.Map; 9. import java.util.Set;

10.

11. import org.apache.ibatis.reflection.ExceptionUtil; 12.

13. public class Plugin implements InvocationHandler { 14.

15. private Object target;

16. private Interceptor interceptor;

17. private Map, Set> signatureMap; 18.

19. private Plugin(Object target, Interceptor interceptor, Map, Set

od>> signatureMap) { 20. this.target = target;

21. this.interceptor = interceptor; 22. this.signatureMap = signatureMap; 23. } 24.

25. public static Object wrap(Object target, Interceptor interceptor) {

26. Map, Set> signatureMap = getSignatureMap(interceptor)

;

27. Class type = target.getClass();

28. Class[] interfaces = getAllInterfaces(type, signatureMap); 29. if (interfaces.length > 0) { 30. return Proxy.newProxyInstance( 31. type.getClassLoader(), 32. interfaces,

33. new Plugin(target, interceptor, signatureMap)); 34. }

35. return target; 36. } 37.

38. public Object invoke(Object proxy, Method method, Object[] args) throws Thro

wable { 39. try {

40. Set methods = signatureMap.get(method.getDeclaringClass()); 41. if (methods != null && methods.contains(method)) {

42. return interceptor.intercept(new Invocation(target, method, args)); 43. }

44. return method.invoke(target, args); 45. } catch (Exception e) {

46. throw ExceptionUtil.unwrapThrowable(e); 47. } 48. } 49.

50. private static Map, Set> getSignatureMap(Interceptor inte

rceptor) {

51. Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Interce

pts.class);

52. if (interceptsAnnotation == null) { // issue #251

53. throw new PluginException(\

ptor \54. }

55. Signature[] sigs = interceptsAnnotation.value();

56. Map, Set> signatureMap = new HashMap, Se

t>();

57. for (Signature sig : sigs) {

58. Set methods = signatureMap.get(sig.type()); 59. if (methods == null) {

60. methods = new HashSet(); 61. signatureMap.put(sig.type(), methods); 62. } 63. try {

64. Method method = sig.type().getMethod(sig.method(), sig.args()); 65. methods.add(method);

66. } catch (NoSuchMethodException e) {

67. throw new PluginException(\

med \68. } 69. }

70. return signatureMap; 71. } 72.

73. private static Class[] getAllInterfaces(Class type, Map, Set<

Method>> signatureMap) {

74. Set> interfaces = new HashSet>();

75. while (type != null) {

76. for (Class c : type.getInterfaces()) { 77. if (signatureMap.containsKey(c)) { 78. interfaces.add(c); 79. } 80. }

81. type = type.getSuperclass(); 82. }

83. return interfaces.toArray(new Class[interfaces.size()]); 84. } 85. 86. }

我们先看一下Plugin的wrap方法,它根据当前的Interceptor上面的注解定义哪些接口需要拦截,然后判断当前目标对象是否有实现对应需要拦截的接口,如果没有则返回目标对象本身,如果有则返回一个代理对象。而这个代理对象的InvocationHandler正是一个Plugin。所以当目标对象在执行接口方法时,如果是通过代理对象执行的,则会调用对应InvocationHandler的invoke方法,也就是Plugin的invoke方法。所以接着我们来看一下该invoke方法的内容。这里invoke方法的逻辑是:如果当前执行的方法是定义好的需要拦截的方法,则把目标对象、要执行的方法以及方法参数封装成一个Invocation对象,再把封装好的Invocation作为参数传递给当前拦截器的intercept方法。如果不需要拦截,则直接调用当前的方法。Invocation中定义了定义了一个proceed方法,其逻辑就是调用当前方法,所以如果在intercept中需要继续调用当前方法的话可以调用invocation的procced方法。

这就是Mybatis中实现Interceptor拦截的一个思想,如果用户觉得这个思想有问题或者不能完全满足你的要求的话可以通过实现自己的Plugin来决定什么时候需要代理什么时候需要拦截。以下讲解的内容都是基于Mybatis的默认实现即通过Plugin来管理Interceptor来讲解的。

对于实现自己的Interceptor而言有两个很重要的注解,一个是@Intercepts,其值是一个@Signature数组。@Intercepts用于表明当前的对象是一个Interceptor,而@Signature则表明要拦截的接口、方法以及对应的参数类型。来看一个自定义的简单Interceptor: Java代码


MyBatis拦截器及分页插件.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:俺来也案例分析 - 图文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: