当前位置: 首页 > news >正文

开发区网站建设方案什么设计网站好

开发区网站建设方案,什么设计网站好,郑州金水区,怎么在百度上推广自己MyBatis插件机制是该框架提供的一种灵活扩展方式,允许开发者在不修改框架源代码的情况下对MyBatis的功能进行定制和增强。这种机制主要通过拦截器(Interceptor)实现,使得开发者可以拦截和修改MyBatis在执行SQL语句过程中的行为。 …

MyBatis插件机制是该框架提供的一种灵活扩展方式,允许开发者在不修改框架源代码的情况下对MyBatis的功能进行定制和增强。这种机制主要通过拦截器(Interceptor)实现,使得开发者可以拦截和修改MyBatis在执行SQL语句过程中的行为。
在这里插入图片描述

MyBatis允许使用插件来拦截的方法调用包括:

  • Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed) 拦截执行器的方法
  • ParameterHandler (getParameterObject, setParameters) 拦截参数的处理
  • ResultSetHandler (handleResultSets, handleOutputParameters) 拦截结果集的处理
  • StatementHandler (prepare, parameterize, batch, update, query) 拦截Sql语法构建的处理

拦截器介绍及配置

MyBatis拦截器的接口定义,plugin方法用于某些处理器(Handler)的构建过程。interceptor方法用于处理代理类的执行。setProperties方法用于拦截器属性的设置:

public interface Interceptor {Object intercept(Invocation invocation) throws Throwable;Object plugin(Object target);void setProperties(Properties properties);}

MyBatis默认没有一个拦截器接口的实现类。下面的MyBatis官网的一个拦截器实例,这个拦截器拦截Executor接口的update方法,所有执行executor的update方法都会被该拦截器拦截到:

@Intercepts({@Signature(type= Executor.class, method = "update", args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {public Object intercept(Invocation invocation) throws Throwable {return invocation.proceed();}public Object plugin(Object target) {return Plugin.wrap(target, this);}public void setProperties(Properties properties) {}
}
<plugins><plugin interceptor="org.format.mybatis.cache.interceptor.ExamplePlugin"></plugin>
</plugins>

源码分析

先从源头->配置文件开始分析:
XMLConfigBuilder解析MyBatis全局配置文件的pluginElement私有方法:

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);}}
}
public void addInterceptor(Interceptor interceptor) {interceptorChain.addInterceptor(interceptor);
}

InterceptorChain类包含一个Interceptor类型的列表,用于存储拦截器。类中有三个方法:pluginAll()用于将所有拦截器应用于目标对象:

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 List<Interceptor> getInterceptors() {return Collections.unmodifiableList(interceptors);}}

一开始说的几种方法都会调用pluginAll:

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

在这里插入图片描述
由于可以拦截StatementHandler,这个接口主要处理sql语法的构建,因此比如分页的功能,可以用拦截器实现,只需要在拦截器的plugin方法中处理StatementHandler接口实现类中的sql即可,可使用反射实现。

Plugin.wrap方法

Mybatis 中的 Plugin.wrap方法用于生成代理对象,实现对目标对象的增强。在 Mybatis 中,插件是通过动态代理实现的,而 Plugin.wrap方法就是用于创建代理对象的核心方法。
具体来说,Plugin.wrap方法接收两个参数:一个是目标对象(target),另一个是插件实例(plugin)。方法的主要作用是将插件实例和目标对象进行绑定,然后通过动态代理技术生成一个新的代理对象,该代理对象在调用目标对象的方法时,会先执行插件实例中对应的拦截方法(interceptor),从而实现对目标对象的功能增强。

public class Plugin implements InvocationHandler {
private Object target;// 被代理的目标类private Interceptor interceptor;// 对应的拦截器private Map<Class<?>, Set<Method>> signatureMap;// 拦截器拦截的方法缓存public static Object wrap(Object target, Interceptor interceptor) {Map<Class<?>, Set<Method>> 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;}}

总结

如果开发者对MyBatis的内部工作原理理解不深,使用拦截器可能会意外改变底层行为,导致难以预料的问题。当使用多个拦截器时,需要仔细管理它们的执行顺序,以确保它们按预期顺序应用,这增加了管理复杂性。同时在编写插件时需注意以下几个原则:

  • 不编写不必要的插件
  • 实现plugin方法时判断一下目标类型,是本插件要拦截的对象才执行Plugin.wrap方法,否则直接返回,这样可以减少目标被代理的次数。
http://www.yayakq.cn/news/339383/

相关文章:

  • 建设个直播网站要多少钱找产品代理加盟
  • 濂溪区建设局网站西安网站建设成功建设
  • 个人摄影网站模版哪个网站网站空间最好
  • 国外的优秀网站做网站语言
  • 淄博网站app县工商局 网站建设
  • 建设网站收费个人flash网站源码
  • 怎样建设淘宝客导购网站建设摩托车官网旗舰店
  • 永州市城乡建设中等职业技术学校网站免费外链代发平台
  • 做游戏的软件app济南网络优化推广公司哪家好
  • 无锡网站 制作江阴网站设计
  • 毕设做网站可以用模板吗网站开发后台编辑系统
  • 东营网站建设方案高端定制网站建设
  • 在线音乐网站开发php无法创建wordpress
  • 最新一键自助建站程序源码珠海模板开发建站
  • 做网站创业故事做美食网站的项目背景
  • 宝盒官方网站附近人才招聘市场
  • 比较大的软件下载网站外贸国际网站推广
  • 网站基本特点微信网页版登录手机版下载
  • 国外空间做网站怎么样济南电视台鲁中频道莱芜新闻
  • 网站建设需要什么内容做网站建设最好学什么
  • 网站合作客户邯郸微信小程序制作公司
  • 重庆营销型网站设计如何修改网站图片
  • 新加坡的网站域名alexa排名是什么意思
  • 火狐浏览器网站开发人员温州华侨职业中等专业学校
  • 企业网站后台内容如何修改网页设计与网站开发第三版课后答案
  • 技术先进的网站建设公司东莞关键词优化排名
  • 大楼物流公司网站源码北京app开发公司有哪些
  • 做网站被捉了解深圳最好的网站
  • wordpress常用插件汇总seo营销策略
  • 推广员网站网站建设需要交文化建设税吗