您好,欢迎访问一九零五行业门户网

mybatis拦截器

拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,或者丢弃这些被拦截的方法而执行自己的逻辑。
如对于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拦截器的详细内容。
其它类似信息

推荐信息