如关于mybatis的Executor,有几种完成:BatchExecutor,ReuseExecutor、SimpleExecutor和CachingExecutor,当这几种Executor接口的query要领没法满足我们的请求的时刻,我们就能够竖立一个阻拦器来完成本身的query要领;阻拦器平常采纳aop动态完成。
阻拦器道理
关于mybatis,我们能够经由过程interceptor接口定义本身的阻拦器。interceptor接口定义:
package org.apache.ibatis.plugin; import java.util.Properties; public interface Interceptor { Object intercept(Invocation invocation) throws Throwable; Object plugin(Object target); void setProperties(Properties properties); }
plugin要领主假如用于封装目的对象,经由过程该要领我们能够决议是不是要举行阻拦进而决议返回什么样的目的对象。
intercept要领就是要举行阻拦的时刻实行的要领。setProperties主要用于在设置文件中指定属性,这个要领在Configuration初始化当前的Interceptor时就会实行.在mybatis中有一个plugin类,该类包含静态要领wrap,经由过程该要领能够决议须要返回的对象是目的对象照样代办。
package org.apache.ibatis.plugin; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.apache.ibatis.reflection.ExceptionUtil; public class Plugin implements InvocationHandler { private Object target; private Interceptor interceptor; private Map<Class<?>, Set<Method>> signatureMap; private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) { this.target = target; this.interceptor = interceptor; this.signatureMap = signatureMap; } public static Object wrap(Object target, Interceptor interceptor) { //剖析猎取须要阻拦的类以及要领{*} Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor); Class<?> type = target.getClass(); //剖析type是不是存在须要阻拦的接口{*} Class<?>[] interfaces = getAllInterfaces(type, signatureMap); //决议返回的对象是不是为代办{*} if (interfaces.length > 0) { return Proxy.newProxyInstance( type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap)); } //返回原目的对象 return target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { Set<Method> methods = signatureMap.get(method.getDeclaringClass()); //假如当前实行的要领是定义的须要阻拦的要领,则把目的对象,要阻拦的要领以及参数封装为一个Invocation对象传递给阻拦器要领intercept; //Invocation中定义了定义了一个proceed要领,其逻辑就是挪用当前要领,所以假如在intercept中须要继承挪用当前要领的话能够挪用invocation的procced要领; if (methods != null && methods.contains(method)) { return interceptor.intercept(new Invocation(target, method, args)); } return method.invoke(target, args); } catch (Exception e) { throw ExceptionUtil.unwrapThrowable(e); } } //依据注解剖析须要阻拦的要领 //两个主要的注解:@Intercepts以及其值其值@Signature(一个数组) //@Intercepts用于表明当前的对象是一个Interceptor //@Signature则表明要阻拦的接口、要领以及对应的参数范例。 private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) { Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class); if (interceptsAnnotation == null) { // issue #251 throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName()); } Signature[] sigs = interceptsAnnotation.value(); Map<Class<?>, Set<Method>> signatureMap = new HashMap<Class<?>, Set<Method>>(); for (Signature sig : sigs) { Set<Method> methods = signatureMap.get(sig.type()); if (methods == null) { methods = new HashSet<Method>(); signatureMap.put(sig.type(), methods); } try { Method method = sig.type().getMethod(sig.method(), sig.args()); methods.add(method); } catch (NoSuchMethodException e) { throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e); } } return signatureMap; } private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) { Set<Class<?>> interfaces = new HashSet<Class<?>>(); while (type != null) { for (Class<?> c : type.getInterfaces()) { if (signatureMap.containsKey(c)) { interfaces.add(c); } } type = type.getSuperclass(); } return interfaces.toArray(new Class<?>[interfaces.size()]); } }
阻拦器实例
package com.mybatis.interceptor; import java.sql.Connection; import java.util.Properties; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; @Intercepts( { @Signature(method = "query", type = Executor.class, args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class })}) public class TestInterceptor implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { Object result = invocation.proceed(); return result; } public Object plugin(Object target) { return Plugin.wrap(target, this); } public void setProperties(Properties properties) { String p = properties.getProperty("property"); } }
首先用@Intercepts标记了这是一个Interceptor,经由过程@Signatrue设想阻拦点:阻拦Executor接口中参数范例为MappedStatement、Object、RowBounds和ResultHandler的query要领;intercept要领挪用invocation的proceed要领,使当前要领一般挪用。
阻拦器的注册
注册阻拦器是经由过程在Mybatis设置文件中plugins元素下的plugin元夙来举行的,Mybatis在注册定义的阻拦器时会先把对应阻拦器下面的一切property经由过程Interceptor的setProperties要领注入。如:
<plugins> <plugin interceptor="com.mybatis.interceptor.TestInterceptor"> <property name="property" value="阻拦器设置"/> </plugin> </plugins>
以上就是mybatis阻拦器的细致内容,更多请关注ki4网别的相干文章!