MyBatis插件的核心功能在于拦截和修改MyBatis框架在执行过程中的行为。具体来说,它可以拦截以下四大核心组件的方法调用:
- Executor:执行器,负责SQL语句的执行和事务管理;
- StatementHandler:语句处理器,处理具体的SQL语句,包括预编译和参数设置等;
- ParameterHandler:参数处理器,负责将用户传递的参数转换成JDBC可识别的参数;
- ResultSetHandler:结果集处理器,负责将JDBC返回的结果集转换成用户所需的对象或集合。
通过拦截这些方法调用,MyBatis插件可以实现诸如SQL重写、日志记录、性能监控、事务管理增强等多种功能。
mybatis的插件如何使用,我们通过插件实现统计SQL的耗时功能。
1. 首先实现Mybatis的Interceptor接口
package com.lzj.plugins;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.util.Properties;
@Intercepts(value = {
@Signature(
type = Executor.class, //type:标记需要拦截的类
method = "query", //method: 标记是拦截类的那个方法
args = {
//args:标记拦截方法的入参
MappedStatement


6306

被折叠的 条评论
为什么被折叠?



