MyBatis的拦截器可以用于在以下对象及方法中拦截修改:

  • Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
  • ParameterHandler (getParameterObject, setParameters)
  • ResultSetHandler (handleResultSets, handleOutputParameters)
  • StatementHandler (prepare, parameterize, batch, update, query)

每一个方法会对应不同的参数列表, 这些需要体现在Intercepts的Signature中

配置

除了实现Interceptor接口以外, 需要在项目中做以下配置

application.yml

增加对应的mybatis-config.xml, 因为现在还不支持在yml中配置plugin

mybatis:
type-aliases-package: com.compayn.proj.commons.api.dto
mapper-locations: classpath:mapper/*.xml
config-location: classpath:mybatis-config.xml

mybatis-config.xml

增加mybatis配置文件, 注意这里配置的拦截器, 其实际执行顺序是自下而上的

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<settings>
<setting name="..." value="true" />
</settings> <plugins>
<!-- The invocation order is bottom up -->
<plugin interceptor="com.company.proj.commons.impl.interceptor.BaseInterceptor" />
</plugins> </configuration>

  

Interceptor实现

一个简单的拦截器实现, 可以输出执行的sql, 以及执行时间.

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
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.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.sql.Statement;
import java.util.Properties; @Intercepts({
@Signature(type = StatementHandler.class, method = "batch", args = { Statement.class}),
@Signature(type = StatementHandler.class, method = "update", args = { Statement.class}),
@Signature(type = StatementHandler.class, method = "query", args = { Statement.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class,Object.class})})
public class BaseInterceptor implements Interceptor {
private static Logger logger = LoggerFactory.getLogger(BaseInterceptor.class); /**
* 用于封装目标对象, 可以返回目标对象本身也可以返回一个它的代理, 这将决定是否会进行拦截
*/
@Override
public Object plugin(Object target) {
if (target instanceof Executor) { // 仅当对象为Executor时, 才使用本插件
return Plugin.wrap(target, this);
} else {
return target;
}
} /**
* 用于配置本插件的相关属性
*/
@Override
public void setProperties(Properties properties) {
// Do nothing
} @Override
public Object intercept(Invocation invocation) throws Throwable {
long start = System.currentTimeMillis(); Object target = invocation.getTarget(); //被代理对象 if (target instanceof Executor) {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Object parameter = invocation.getArgs()[1];
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
String sql = boundSql.getSql();
logger.info("SQL: {}", sql);
} Object result = invocation.proceed();
long duration = System.currentTimeMillis() - start;
logger.info("Time elapsed: {}ms", duration);
return result;
}
}

代码说明

@Intercepts 注解: 这个地方配置需要拦截的对象方法, 每个方法对应一个Signature, 这个Signature由对象类型, 方法名和参数组成, 方法名和参数可以直接参考这个对象的接口.

Object plugin(Object target)接口: 用于封装目标对象, 可以返回目标对象本身也可以返回一个它的代理, 这将决定是否会进行拦截

void setProperties(Properties properties)接口: 用于配置本插件的相关属性, 值可以通过Mybatis配置文件传入

Object intercept(Invocation invocation) throws Throwable接口: 执行拦截的方法, 其中 invocation.getTarget() 可以看到实际被代理的对象, 根据对象类型不同, 可以读取这个对象方法的参数并进行需要的操作.

MyBatis Interceptor的更多相关文章

  1. Mybatis Interceptor 拦截器原理 源码分析

    Mybatis采用责任链模式,通过动态代理组织多个拦截器(插件),通过这些拦截器可以改变Mybatis的默认行为(诸如SQL重写之类的),由于插件会深入到Mybatis的核心,因此在编写自己的插件前最 ...

  2. mybatis Interceptor拦截器代码详解

    mybatis官方定义:MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  3. mybatis interceptor 处理查询参数及查询结果

    拦截器:拦截update,query方法,处理查询参数及返回结果. /** * Created by windwant on 2017/1/12. */ @Intercepts({ @Signatur ...

  4. MyBatis源码分析(2)—— Plugin原理

    @(MyBatis)[Plugin] MyBatis源码分析--Plugin原理 Plugin原理 Plugin的实现采用了Java的动态代理,应用了责任链设计模式 InterceptorChain ...

  5. mybatis 打印sql 语句

    拦截器 package com.cares.asis.mybatis.interceptor; import java.text.DateFormat; import java.util.Date; ...

  6. mybatis 控制台打印sql

    开发时调试使用 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBe ...

  7. 在mybatis执行SQL语句之前进行拦击处理

    转载自:http://blog.csdn.net/hfmbook/article/details/41985853 比较适用于在分页时候进行拦截.对分页的SQL语句通过封装处理,处理成不同的分页sql ...

  8. Mybatis拦截器介绍

    拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法.Mybatis拦截器设计的一个初 ...

  9. MyBatis查询两个字段,返回Map,一个字段作为key,一个字段作为value的实现

    1. 问题描述 在使用MyBatis,我们经常会遇到这种情况:SELECT两个字段,需要返回一个Map,其中第一个字段作为key,第二个字段作为value.MyBatis的MapKey虽然很实用,但并 ...

  10. MyBatis浅尝笔记

    MyBatis应属于一种轻量级的java持久层技术,它通过简单的SQL xml或注解,将数据库数据映射到接口与POJO.最近项目要用到mybatis,所以学习之后在这里做个总结,文中的示例以xml配置 ...

随机推荐

  1. SpringMVC08——拦截器——2021-05-11

    拦截器概述 SpringMVC的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并做相应的处理. 例如:进行权限验证.记录请求信息的日志.判断用户 ...

  2. [转帖]decimal and numeric (Transact-SQL)

    https://learn.microsoft.com/en-us/sql/t-sql/data-types/decimal-and-numeric-transact-sql?view=sql-ser ...

  3. [转帖]TiDB BR 备份至 MinIO S3 实战

    https://tidb.net/blog/3a31d41d#3.%E9%83%A8%E7%BD%B2%20MinIO%20S3%20%E5%8F%8A%E5%A4%87%E4%BB%BD%E6%81 ...

  4. [转帖]使用s3(minio)为kubernetes提供pv存储

    http://www.lishuai.fun/2021/12/31/k8s-pv-s3/#/%E8%A6%81%E6%B1%82 我们可以通过csi使用s3为kubernetes提供pv存储,当我们申 ...

  5. [转帖]oswatch--Linux

    https://www.fengnayun.com/news/content/46922.html 一 官方参考OSWatcher (Includes: [Video]) (文档 ID 301137. ...

  6. [转帖]nginx 反向代理中proxy_set_header的含义

    https://www.jianshu.com/p/cd813d68ed25 0.1212020.10.23 09:29:53字数 284阅读 9,939 1.proxy_set_header设置的请 ...

  7. Nginx 系列 | (转)Nginx 上传文件:client_max_body_size 、client_body_buffer_size

    原文:http://php-note.com/article/detail/488 client_max_body_size client_max_body_size 默认 1M,表示 客户端请求服务 ...

  8. DashBoard in k8s 简单使用

    DashBoard in k8s 简单使用 第一部分 拉取分发镜像 没办法的事情,公司网络实在是太垃圾了, dockerhub 又不让多次docker pull 找一台网络表好的机器 执行如下命令: ...

  9. 【VictoriaMetrics的vmbackupmanager】这个一年卖 2 万美元的功能,我做出来了

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 1.背景 在可观测领域的 metrics 解决方案中,Vi ...

  10. 【笔记】VictoriaMetrics中,对大量的pull模式的targets进行分片

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 配置的方法请看这里:https://docs.victor ...