wordpress搭的,泉州做网站优化多少钱,网站正在备案,合肥专业的房产网站建设来源#xff1a;http://my.oschina.net/zudajun大多数框架#xff0c;都支持插件#xff0c;用户可通过编写插件来自行扩展功能#xff0c;Mybatis也不例外。我们从插件配置、插件编写、插件运行原理、插件注册与执行拦截的时机、初始化插件、分页插件的原理等六个方面展开… 来源http://my.oschina.net/zudajun大多数框架都支持插件用户可通过编写插件来自行扩展功能Mybatis也不例外。我们从插件配置、插件编写、插件运行原理、插件注册与执行拦截的时机、初始化插件、分页插件的原理等六个方面展开阐述。# 插件配置Mybatis的插件配置在configuration内部初始化时会读取这些插件保存于Configuration对象的InterceptorChain中。?xml version1.0 encodingUTF-8? public class Configuration { protected final InterceptorChain interceptorChain new InterceptorChain();}org.apache.ibatis.plugin.InterceptorChain.java源码。public class InterceptorChain { private final List interceptors new ArrayList(); public Object pluginAll(Object target) { for (Interceptor interceptor : interceptors) { target interceptor.plugin(target); } return target; } public void addInterceptor(Interceptor interceptor) { interceptors.add(interceptor); } public List getInterceptors() { return Collections.unmodifiableList(interceptors); }}上面的for循环代表了只要是插件都会以责任链的方式逐一执行(别指望它能跳过某个节点)所谓插件其实就类似于拦截器。# 如何编写一个插件插件必须实现org.apache.ibatis.plugin.Interceptor接口。public interface Interceptor { Object intercept(Invocation invocation) throws Throwable; Object plugin(Object target); void setProperties(Properties properties);}intercept()方法执行拦截内容的地方比如想收点保护费。由plugin()方法触发interceptor.plugin(target)足以证明。plugin()方法决定是否触发intercept()方法。setProperties()方法给自定义的拦截器传递xml配置的属性参数。下面自定义一个拦截器Intercepts({ Signature(type Executor.class, method query, args { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }), Signature(type Executor.class, method close, args { boolean.class }) })public class MyBatisInterceptor implements Interceptor { private Integer value; Override public Object intercept(Invocation invocation) throws Throwable { return invocation.proceed(); } Override public Object plugin(Object target) { System.out.println(value); // Plugin类是插件的核心类用于给target创建一个JDK的动态代理对象触发intercept()方法 return Plugin.wrap(target, this); } Override public void setProperties(Properties properties) { value Integer.valueOf((String) properties.get(value)); }}面对上面的代码我们需要解决两个疑问1. 为什么要写Annotation注解注解都是什么含义答Mybatis规定插件必须编写Annotation注解是必须而不是可选。Intercepts注解装载一个Signature列表一个Signature其实就是一个需要拦截的方法封装。那么一个拦截器要拦截多个方法自然就是一个Signature列表。type Executor.class, method query, args { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }解释要拦截Executor接口内的query()方法参数类型为args列表。2. Plugin.wrap(target, this)是干什么的答使用JDK的动态代理给target对象创建一个delegate代理对象以此来实现方法拦截和增强功能它会回调intercept()方法。org.apache.ibatis.plugin.Plugin.java源码public class Plugin implements InvocationHandler { private Object target; private Interceptor interceptor; private Map, Set signatureMap; private Plugin(Object target, Interceptor interceptor, Map, Set signatureMap) { this.target target; this.interceptor interceptor; this.signatureMap signatureMap; } public static Object wrap(Object target, Interceptor interceptor) { Map, Set signatureMap getSignatureMap(interceptor); Class type target.getClass(); Class[] interfaces getAllInterfaces(type, signatureMap); if (interfaces.length 0) { // 创建JDK动态代理对象 return Proxy.newProxyInstance( type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap)); } return target; } Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { Set methods signatureMap.get(method.getDeclaringClass()); // 判断是否是需要拦截的方法(很重要) if (methods ! null methods.contains(method)) { // 回调intercept()方法 return interceptor.intercept(new Invocation(target, method, args)); } return method.invoke(target, args); } catch (Exception e) { throw ExceptionUtil.unwrapThrowable(e); } }//...}Map, Set signatureMap缓存需拦截对象的反射结果避免多次反射即target的反射结果。所以我们不要动不动就说反射性能很差那是因为你没有像Mybatis一样去缓存一个对象的反射结果。判断是否是需要拦截的方法这句注释很重要一旦忽略了都不知道Mybatis是怎么判断是否执行拦截内容的要记住。# Mybatis可以拦截哪些接口对象public class Configuration {//...public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) { ParameterHandler parameterHandler mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql); parameterHandler (ParameterHandler) interceptorChain.pluginAll(parameterHandler); // 1 return parameterHandler; } public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSql boundSql) { ResultSetHandler resultSetHandler new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds); resultSetHandler (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler); // 2 return resultSetHandler; } public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { StatementHandler statementHandler new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql); statementHandler (StatementHandler) interceptorChain.pluginAll(statementHandler); // 3 return statementHandler; } public Executor newExecutor(Transaction transaction) { return newExecutor(transaction, defaultExecutorType); } public Executor newExecutor(Transaction transaction, ExecutorType executorType) { executorType executorType null ? defaultExecutorType : executorType; executorType executorType null ? ExecutorType.SIMPLE : executorType; Executor executor; if (ExecutorType.BATCH executorType) { executor new BatchExecutor(this, transaction); } else if (ExecutorType.REUSE executorType) { executor new ReuseExecutor(this, transaction); } else { executor new SimpleExecutor(this, transaction); } if (cacheEnabled) { executor new CachingExecutor(executor); } executor (Executor) interceptorChain.pluginAll(executor); // 4 return executor; }//...}Mybatis只能拦截ParameterHandler、ResultSetHandler、StatementHandler、Executor共4个接口对象内的方法。重新审视interceptorChain.pluginAll()方法该方法在创建上述4个接口对象时调用其含义为给这些接口对象注册拦截器功能注意是注册而不是执行拦截。拦截器执行时机plugin()方法注册拦截器后那么在执行上述4个接口对象内的具体方法时就会自动触发拦截器的执行也就是插件的执行。所以一定要分清何时注册何时执行。切不可认为pluginAll()或plugin()就是执行它只是注册。# Invocationpublic class Invocation { private Object target; private Method method; private Object[] args;}intercept(Invocation invocation)方法的参数Invocation 我相信你一定可以看得懂不解释。# 初始化插件源码解析org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XNode)方法部分源码。pluginElement(root.evalNode(plugins)); private void pluginElement(XNode parent) throws Exception { if (parent ! null) { for (XNode child : parent.getChildren()) { String interceptor child.getStringAttribute(interceptor); Properties properties child.getChildrenAsProperties(); Interceptor interceptorInstance (Interceptor) resolveClass(interceptor).newInstance(); // 这里展示了setProperties()方法的调用时机 interceptorInstance.setProperties(properties); configuration.addInterceptor(interceptorInstance); } } }对于Mybatis它并不区分是何种拦截器接口所有的插件都是InterceptorMybatis完全依靠Annotation去标识对谁进行拦截所以具备接口一致性。# 分页插件原理由于Mybatis采用的是逻辑分页而非物理分页那么市场上就出现了可以实现物理分页的Mybatis的分页插件。要实现物理分页就需要对String sql进行拦截并增强Mybatis通过BoundSql对象存储String sql而BoundSql则由StatementHandler对象获取。public interface StatementHandler { List query(Statement statement, ResultHandler resultHandler) throws SQLException; BoundSql getBoundSql();}public class BoundSql { public String getSql() { return sql; }}因此就需要编写一个针对StatementHandler的query方法拦截器然后获取到sql对sql进行重写增强。任它天高海阔任它变化无穷我们只要懂得原理再多插件我们都可以对其投送王之蔑视。热文推荐Java面试题汇总(04)12月DB-Engines数据库排名你猜谁会是第一老弟你连HTTPS 原理都不懂还给我讲“中间人攻击”逗我吗...觉得不错请给个「在看」分享给你的朋友- End -