Mybatis插件
创始人
2024-05-30 15:58:09
0

插件使用

动手实现plugin

首先我们需要实现一下这个Interceptor,其中plugin和setProperties方法可以不实现,plugin是因为已经有了完善的逻辑,而setProperties,如果不需要在intercept()中使用属性,也可以不设置。然后在实现类中完成注解配置

public interface Interceptor {Object intercept(Invocation invocation) throws Throwable;default Object plugin(Object target) {return Plugin.wrap(target, this);}default void setProperties(Properties properties) {// NOP}}

以下是一个实现demo

@Intercepts({@Signature(type = StatementHandler.class,method = "prepare",args = {Connection.class, Integer.class}
), @Signature(type = Executor.class,method = "update",args = {MappedStatement.class, Object.class}
), @Signature(type = Executor.class,method = "query",args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(type = Executor.class,method = "query",args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class testMybatisInterceptor implements Interceptor {private Properties properties;@Overridepublic Object intercept(Invocation invocation) throws Throwable {return invocation.proceed();}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {this.properties = properties;}public Properties getProperties() {return properties;}}

向容器注入该interceptor

可以通过两种方式
一种是@Bean注入

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {return new testMybatisInterceptor();
}

另一种是xml注入,目前比较少用了



拦截器原理

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);}}

拦截器的添加

初始化的添加

Configuration是mybatis的核心初始化数据承装容器,在一开始它就会创建一个InterceptorChain,后期所有添加和过滤都是在这个InterceptorChain上操作的。

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);}}

@Bean

@Bean只是本人的猜测,mybatis应该会添加一个beanPostProcessor,对于当前bean是否拥有@interceptors注解进行扫描,如果有,就会把这个bean放入Configuration

XML

在解析XML的时候,就会把拦截器添加进configuration

  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).getDeclaredConstructor().newInstance();interceptorInstance.setProperties(properties);configuration.addInterceptor(interceptorInstance);}}}

初始化Handler时的设置

在完成newParameter的时候,就会对Handler进行拦截器封装。封装的过程就是使用代理模式,invokeHandler就是

  public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject,BoundSql boundSql) {ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement,parameterObject, boundSql);return (ParameterHandler) interceptorChain.pluginAll(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);return (ResultSetHandler) interceptorChain.pluginAll(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);return (StatementHandler) interceptorChain.pluginAll(statementHandler);}public Executor newExecutor(Transaction transaction, ExecutorType executorType) {executorType = executorType == null ? defaultExecutorType : 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);}return (Executor) interceptorChain.pluginAll(executor);}

在pluginAll中,会对每一个Interceptor进行接口拦截

  public Object pluginAll(Object target) {for (Interceptor interceptor : interceptors) {target = interceptor.plugin(target);}return target;}

target就是以上某种Handler或者Executor,interceptor就是当前遍历的执行器

  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) {return Proxy.newProxyInstance(type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap));}return target;}

其中getSignatureMap,就是把注解转化为map,供以后调用使用,里面的key就是type,而value就是prepare(Connection,Integer)这个方法。

@Intercepts({@Signature(type = StatementHandler.class,method = "prepare",args = {Connection.class, Integer.class}
)})

在invoke方法中,会先检查这个方法是否通过注解注册过,如果是注册的方法,就会调用intercept方法,并且把当前handler,当前方法,和方法参数返回给interceptor完成后续业务操作。

  @Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {try {Set methods = signatureMap.get(method.getDeclaringClass());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);}}

处理拦截器

对应Handler在调用方法时,会调用intercept(),如果通过,会调用method.invoke()进行真正的代码执行,如果在拦截器模式下,由于是代理模式套代理模式层层循环,所以实际上是调用了第二个代理器的intercept(),直到完全调用完,才会进入最内层的被代理对象的源方法。

@Override
public Object intercept(Invocation invocation) throws Throwable {return invocation.proceed();
}
public Object proceed() throws InvocationTargetException, IllegalAccessException {return method.invoke(target, args);
}

相关内容

热门资讯

我的心爱之物四年级作文【优秀... 我的心爱之物四年级作文 篇一我最心爱的物品是一本名为《小王子》的图书。这本书是我四年级时的生日礼物,...
跳绳比赛四年级优秀作文(经典... 跳绳比赛四年级优秀作文 篇一:我的跳绳比赛经历上周,我们学校举行了一场跳绳比赛,我参加了四年级的比赛...
四年级我画画作文【精彩6篇】 四年级我画画作文 篇一我是一个四年级的小学生,非常喜欢画画。自从我学会拿起画笔,画画已经成为了我生活...
关于蜜蜂的作文四年级【优选6... 关于蜜蜂的作文四年级 篇一蜜蜂是一种非常有趣的昆虫,它们生活在蜂巢中,有着重要的作用。今天,我就来给...
关于危急时刻的四年级作文50... 关于危急时刻的四年级作文500字 篇一危急时刻在我们的生活中,我们常常会遇到一些危急时刻,这些时刻需...
我最敬佩的一位名人四年级作文... 我最敬佩的一位名人四年级作文 篇一我最敬佩的一位名人是李彦宏。李彦宏是中国著名的企业家,同时也是百度...
感恩老师四年级作文600字【... 篇一:感恩老师的温暖我是一名四年级的学生,今天我要写一篇关于感恩老师的作文。老师是我们成长道路上最重...
做家务的作文400字四年级作... 做家务的作文400字四年级作文20篇篇一:我喜欢做家务我是一个四年级的小学生,我喜欢做家务。每天放学...
四年级写作文买菜400字(经... 四年级写作文买菜400字 篇一:去菜市场买菜四年级的我,有一次和妈妈一起去菜市场买菜,这是我第一次亲...
四年级下册数学暑假新时空答案... 四年级下册数学暑假新时空答案 篇一在四年级下册的数学学习中,我们将继续探索数学的奥秘和应用。而暑假正...
地雷花四年级作文【最新3篇】 地雷花四年级作文 篇一地雷花地雷花是一种奇特的植物,它的花朵看起来像一颗小小的地雷。我在学校的花坛里...
我和孙悟空的一天四年级上册作... 我和孙悟空的一天四年级上册作文 篇一今天是一个特殊的日子,我竟然和孙悟空一起度过了一天!早上,我醒来...
推荐一个好地方作文四年级45... 推荐一个好地方作文四年级450字 篇一我的家乡是一个非常美丽的地方,我推荐大家来参观一下。我家乡的风...
小学的小学四年级作文500字... 小学的小学四年级作文500字 篇一我喜欢的运动我是一个活泼好动的小女孩,喜欢参加各种运动。今天我就来...
今天我当家四年级作文(推荐6... 今天我当家四年级作文 篇一我当家的一天今天,我得到了一个特殊的任务,那就是要当家一天。这是我第一次担...
跳绳比赛四年级作文(通用6篇... 跳绳比赛四年级作文 篇一跳绳比赛的经历我是一名四年级的学生,最近我们学校举办了一场跳绳比赛。这是我第...
家乡变化作文400字四年级(... 家乡变化作文400字四年级 篇一家乡变化我爱我的家乡,那是一个美丽而又宁静的地方。然而,自从几年前,...
一件高兴的事250作文四年级... 篇一:一件高兴的事今天,我要给大家讲一个让我非常高兴的事情。那天,我和妈妈一起去超市买东西。在超市里...
四年级写大自然的景色作文30... 四年级写大自然的景色作文300字 篇一大自然的美景大自然是我们共同的母亲,给予我们无尽的美景和宝贵的...
小小动物园四年级作文【精简6... 小小动物园四年级作文 篇一我参观了一个小小的动物园,里面有许多有趣的动物。首先,我看到了一只可爱的小...