• java
  • go
  • 数据库
  • linux
  • 中间件
  • 书
  • 源码
  • 夕拾

  • java
  • go
  • 数据库
  • linux
  • 中间件
  • 书
  • 源码
  • 夕拾

mybatis些拦截器

目录

  • 目录
  • 简单示例
  • 全部可被拦截的类
    • executor
    • parameterHandler
    • statementHandler

简单示例

拦截Executor得query方法,打印执行sql时长

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Intercepts({
// signature:
// 1. type 类
// 2. method 方法
// 3. 参数
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})
})
public class TestDbInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
String sqlId = mappedStatement.getId();
Object returnValue;

long start = System.currentTimeMillis();
returnValue = invocation.proceed();
long end = System.currentTimeMillis();
log.info("{} -- {} ", sqlId, end - start);
return returnValue;
}
}

全部可被拦截的类

executor

主从库查询的拦截器进行主从切换,aop也可以,但是得拦截方法不如此方法更友好

parameterHandler

statementHandler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

// 分页插件拦截此方法
Statement prepare(Connection connection, Integer transactionTimeout)
throws SQLException;

void parameterize(Statement statement)
throws SQLException;

void batch(Statement statement)
throws SQLException;

int update(Statement statement)
throws SQLException;

<E> List<E> query(Statement statement, ResultHandler resultHandler)
throws SQLException;

<E> Cursor<E> queryCursor(Statement statement)
throws SQLException;

BoundSql getBoundSql();

ParameterHandler getParameterHandler();
mybatis-configuration
mybatis-目录
  1. 1. 目录
  2. 2. 简单示例
  3. 3. 全部可被拦截的类
    1. 3.1. executor
    2. 3.2. parameterHandler
    3. 3.3. statementHandler
© 2023 haoxp
Hexo theme