MyBatis整体预览(二)
本文将介绍MyBatis的插件实现原理
一、MyBatis为开发者提供了非常丰富的接口,以满足开发者扩充自己的功能。将扩展的插件配置到configuration的plugins的标签中,那么mybatis自动将插件插入到你想执行的地方。在《MyBatis整体预览(一)》中,曾介绍MyBatis允许开发者在StatementHandler、ResultSetHandler、ParameterHandler以及Executor插入自己想执行的代码。下面将详细介绍从我们定制自己的插件到插件是如何被调用的来进行分析。
首先,要开发MyBatis的插件需要实现org.apache.ibatis.plugin.Interceptor接口,这个接口将会要求实现几个方法:intercept()、plugin()及setProperties(),intercept方法是开发人员所要执行的操作,plugin是将你插件放入到MyBatis的插件集合中去,而setProperties这是在你配置你插件的时候将plugins/plugin/properties的值设置到该插件中。这是实现自己插件的几个步骤,注意:一般在plugin方法中只写Plugin.wrap(target,this),target一般是你要拦截的对象,this这是当前的插件,在plugin方法参数中有个plugin(Object target),这个target类型就是StatementHandler、ResultSetHandler、ParameterHandler以及Executor中的一个。这里就插件的基本结构和方法进行了介绍。下面将对MyBatis如何获得开发人员开发的插件,以及具体执行的过程进行分析。
在《MyBatis整体预览(一)》中,对MyBatis的整个执行过程进行了一个介绍,主要是对Configuration对象的初始化过程进行了比较详细的介绍。当然,在Configuration初始化的过程中当然也包括对开发人员自己的插件进行初始化,并进行保存插件对象。
在XMLConfigBuilder的parsetConfiguration里面调用了pluginElement方法,这个方法将会解析开发人员配置在configuration中的plugin标签下面的元素。执行代码如下:
[java] 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();
interceptorInstance.setProperties(properties);
configuration.addInterceptor(interceptorInstance);
}
}
}
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();
interceptorInstance.setProperties(properties);
configuration.addInterceptor(interceptorInstance);
}
}
}
这个方法里面调用了configuration类中的addInterceptor方法,将插件实例添加到configuration对象中,那么让我们看看configuration里面对插件对象做了什么:
[java] public void addInterceptor(Interceptor interceptor) {
interceptorChain.addInterceptor(interceptor);
}
public void addInterceptor(Interceptor interceptor) {
interceptorChain.addInterceptor(interceptor);
}
这就是在configuration类中的这个addInterceptor方法,他将这个插件添加到一个链中,那么这个易做图链是怎样的呢?
[java] public class InterceptorChain {
private final List<Interceptor> interceptors = new ArrayList<Interceptor>();
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
public void addInterceptor(Interceptor interceptor) {
interceptors.add(interceptor);
}
}
public class InterceptorChain {
private final List<Interceptor> interceptors = new ArrayList<Interceptor>();
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
public void addInterceptor(Interceptor interceptor) {
interceptors.add(interceptor);
}
}
这个类很简单,直接将这个插件添加到了一个List对象集合中。你可能会发现上面还有一个pluginAll方法,并且在该方法里面调用了插件的plugin方法。大家是否明白了,这个plugin方法里面上面已经介绍,只是执行了Plugin.wrap(target,this)段代码。那么现在就有个几个问题:第一、这个pluginAll方法什么时候调用,还有就是Plugin.wrap(target,this),这段代码是干什么用的。理解清楚这两个问题,那么MyBatis的插件开发过程就完全理解了。
首先让我们开看看如何调用pluginAll方法的。在Configuration类中会发现一下几个方法:
[java] public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
}
public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,
ResultHandler resultHandler, BoundSql boundSql) {
ResultSetHandler resultSetHandler = mappedStatement.hasNestedResultMaps() ? new NestedResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql,
rowBounds) : new FastResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
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);
return statementHandler;
}
public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {
executorType = executorType == null ? defaultExecutorType : executorType;
&n
补充:软件开发 , Java ,