mybatis插件原理初始化过程
目录
插件的目的
让程序员能对SQL参数设置到SQL执行结果返回过程进行扩展, 比如分页插件就是通过对StatementHandler.prepare方法进行拦截,将分页信息拼接到SQL上
原理
利用动态代理对拦截器进行包装,从而实现对所需方法的拦截处理
mybatis插件针对哪几个接口
Executor
执行器,对SqlSession暴露增删改查接口
ParameterHandler
参数处理器,对预编译占位参数进行设值
StatementHandler
SQL执行器,获取statement发送sql执行
ResultHandler
对结果集进行处理
四个接口执行流程
插件如何加载
如何实现一个插件
用分页插件举个例子
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class PaginationInterceptor extends AbstractSqlParserHandler implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
......省略
StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
//内部根据不同的数据拼接指定的分页SQL
DialectModel model = DialectFactory.buildPaginationSql(page, buildSql, dbType, dialectClazz);
Configuration configuration = mappedStatement.getConfiguration();
List<ParameterMapping> mappings = new ArrayList<>(boundSql.getParameterMappings());
Map<String, Object> additionalParameters = (Map<String, Object>) metaObject.getValue("delegate.boundSql.additionalParameters");
model.consumers(mappings, configuration, additionalParameters);
metaObject.setValue("delegate.boundSql.sql", model.getDialectSql());
metaObject.setValue("delegate.boundSql.parameterMappings", mappings);
return invocation.proceed();
}
}
核心注解
@Intercepts
标注此类是一个拦截器
@Signature
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Signature {
/**
* 拦截的class类型
*
* @return the java type
*/
Class<?> type();
/**
* 拦截的class中的方法名
*
* @return the method name
*/
String method();
/**
* 方法所需的参数
* @return java types for method argument
*/
Class<?>[] args();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!