public class MyPlugin implements Interceptor {
Transaction transaction;
//方法拦截
public Object intercept(Invocation invocation) throws Throwable {
//通过StatementHandler获取执行的sql
StatementHandler executor = (StatementHandler) ();
ParameterHandler parameterHandler = ();
();
BoundSql boundSql = ();
List<ParameterMapping> parameterMappings = ();
if (!(parameterMappings)) {
ParameterMapping parameterMapping = (0);
Class clazz = ();
Field field = ("configuration");
(true);
Configuration configuration = (Configuration) (parameterMapping);
Environment environment = ();
TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
transaction = ((), null, false);
Statement statement = prepareStatement(executor);
();
(());
}
Object proceed = ();
return proceed;
}
//获取到拦截的对象,底层也是通过代理实现的,实际上是拿到一个目标代理对象
public Object plugin(Object target) {
return (target, this);
}
//获取设置的阈值等参数
public void setProperties(Properties properties) {
}
private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
if (environment == null || () == null) {
return new ManagedTransactionFactory();
}
return ();
}
private Statement prepareStatement(StatementHandler handler) throws SQLException {
Statement stmt;
Connection connection = ();
stmt = (connection, ());
(stmt);
return stmt;
}
}