MyBatis拦截器用到了两个注解:@Intercepts和@Signature
@Intercepts({@Signature(type = Executor.class, method ="update", args = {MappedStatement.class, Object.class})})
@Slf4j
@Component
public class ExecutorInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
String sql = ExecutorPluginUtils.getSqlByInvocation(invocation);
//可以对sql重写
log.error("拦截器ExecutorInterceptor:"+sql);
//sql = "SELECT id from BUS_RECEIVER where id = ? ";
ExecutorPluginUtils.resetSql2Invocation( invocation, sql);
return invocation.proceed();
}
@Override
public Object plugin(Object o) {
return Plugin.wrap(o, this);
}
@Override
public void setProperties(Properties properties) {
}
}
type:拦截对象
method:要拦截的方法
args:要拦截的方法的参数
可以自定义拦截对象,默认允许拦截的几个对象
Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query)