一篇聊聊Mybatis插件开发

开发 前端
允许在 plugin 元素中配置所需参数,方法在插件初始化的时候就被调用了一次,然后把插件对象存入到配置中,以便后面再取出。

Mybatis的插件,主要用于在执行sql前后,对sql进行封装加工,或者在sql执行后,对数据进行加工处理。常用于一些公共数据操作处理,例如:

  1. 分页插件,在执行sql查询前增加分页参数
  2. 多租户系统中,增加租户ID参数。
  3. 增加更新时间、创建时间、更新人、创建人的参数信息。
  4. 数据权限中,增加参数查询。

插件开发过程

确定需要拦截的签名

指定需要拦截的方法,通过方法签名来指定,方法签名即指定哪个类的哪个方法+方法参数。这里的类不能随便写,只能从以下几个类中选,也就是说,MyBatis 插件可以拦截四大对象中的任意一个。

  • Executor 是执行 SQL 的全过程,包括组装参数,组装结果集返回和执行 SQL 过程,都可以拦截。
  • StatementHandler 是执行 SQL 的过程,我们可以重写执行 SQL 的过程。
  • ParameterHandler 是拦截执行 SQL 的参数组装,我们可以重写组装参数规则。
  • ResultSetHandler 用于拦截执行结果的组装,我们可以重写组装结果的规则。

我们来看以下mybatisplus的插件配置的签名:

@Intercepts(
    {
        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}),
        @Signature(type = StatementHandler.class, method = "getBoundSql", args = {}),
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
        @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 MybatisPlusInterceptor implements Interceptor {
//...
}

type指定四大类型中的任意一个,method指定拦截类型中方法,args指定方法参数。例如:

@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})

指定了拦截StatementHandler的prepare方法,方法有两个参数,一个是Connection类型,另一个是Integer类型。

public interface StatementHandler {

  Statement prepare(Connection connection, Integer transactionTimeout)
      throws SQLException;
      
      //....
      }

插件接口定义

在 MyBatis 中开发插件,需要实现 Interceptor 接口。接口的定义如下:

public interface Interceptor {
 
  Object intercept(Invocation invocation) throws Throwable;
 
  Object plugin(Object target);
 
  void setProperties(Properties properties);
 
}
  • intercept 方法:它将直接覆盖你所拦截对象原有的方法,因此它是插件的核心方法。通过 invocation 参数可以反射调度原来对象的方法。
  • plugin 方法:target 是被拦截对象,它的作用是给被拦截对象生成一个代理对象,并返回它。为了方便 MyBatis 使用 org.apache.ibatis.plugin.Plugin 中的 wrap 静态方法提供生成代理对象。
  • setProperties 方法:允许在 plugin 元素中配置所需参数,方法在插件初始化的时候就被调用了一次,然后把插件对象存入到配置中,以便后面再取出。

实现插件

创建个类实现Interceptor接口,并且在实现类上指定方法签名即可。

最后需要在mybatis配置文件中配置插件

<plugins>
        <plugin interceptor="com.yjw.demo.mybatis.common.page.PageInterceptor">
        </plugin>
    </plugins>

最后建议看一下MybatisPlusInterceptor的实现,里面还使用到了责任链设计模式。

责任编辑:武晓燕 来源: 今日头条
相关推荐

2023-07-31 07:48:43

Java内存虚拟机

2023-08-03 07:34:34

格式化字符串参数

2022-06-02 07:11:13

JVMJava

2023-12-08 08:26:05

数据存储持久性

2022-10-08 15:07:06

ChatOps运维

2021-10-30 07:55:00

BLE 蓝牙开发

2021-11-15 07:47:40

字符串位置存储

2021-04-16 07:46:13

Serverless 云开发FaaS

2021-11-03 14:49:20

开发摸鱼侧边栏

2021-02-26 11:54:38

MyBatis 插件接口

2023-04-20 08:00:00

ES搜索引擎MySQL

2022-02-07 11:01:23

ZooKeeper

2021-05-20 06:57:16

RabbitMQ开源消息

2024-04-02 08:58:13

2024-03-05 18:27:43

2022-10-20 18:00:00

MyBatis缓存类型

2021-07-12 06:11:14

SkyWalking 仪表板UI篇

2020-11-20 10:15:05

TensorFlow

2022-03-18 07:48:58

GhostNode.js开源

2021-05-17 05:51:31

KubeBuilderOperator测试
点赞
收藏

51CTO技术栈公众号