5.13 mybatis之plugins使用

server/2024/10/18 16:46:33/

MyBatis插件的核心功能在于拦截和修改MyBatis框架在执行过程中的行为。具体来说,它可以拦截以下四大核心组件的方法调用:

  • Executor:执行器,负责SQL语句的执行和事务管理;
  • StatementHandler:语句处理器,处理具体的SQL语句,包括预编译和参数设置等;
  • ParameterHandler:参数处理器,负责将用户传递的参数转换成JDBC可识别的参数;
  • ResultSetHandler:结果集处理器,负责将JDBC返回的结果集转换成用户所需的对象或集合。

通过拦截这些方法调用,MyBatis插件可以实现诸如SQL重写、日志记录、性能监控、事务管理增强等多种功能。

mybatis的插件如何使用,我们通过插件实现统计SQL的耗时功能。

1. 首先实现Mybatis的Interceptor接口

package com.lzj.plugins;import org.apache.ibatis.executor.Executor;
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.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;import java.util.Properties;@Intercepts(value = {@Signature(type = Executor.class,	//type:标记需要拦截的类method = "query", 		//method: 标记是拦截类的那个方法 	args = { 				//args:标记拦截方法的入参MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class})}
)
public class LogPlugin implements Interceptor {/*计算统计耗时的核心逻辑*/@Overridepublic Object intercept(Invocation invocation) throws Throwable {long startTime = System.currentTimeMillis();Object result = invocation.proceed();System.out.println("耗时:" + (System.currentTimeMillis() - startTime));return result;}@Overridepublic Object plugin(Object target) {return Interceptor.super.plugin(target);}/*设置插件的属性*/@Overridepublic void setProperties(Properties properties) {Interceptor.super.setProperties(properties);}
}

2. 实现耗时拦截器后还需要在mybatis配置文件中配置拦截器

 <plugins><plugin interceptor="com.lzj.plugins.LogPlugin"></plugin>
</plugins>

只需执行完上述2步后,就可以正确打印出SQL的耗时了。
执行案例结果如下所示

Opening JDBC Connection
Created connection 868815265.
==>  Preparing: select * from car where name=? 
==> Parameters: xiaoli(String)
<==    Columns: name, brand
<==        Row: xiaoli, BYD
<==      Total: 1
耗时:154

另外Interceptor接口中还一个setProperties方法,那么该方法是起什么作用呢?其实该方法起到了向拦截器传递参数的作用。那么具体如何使用呢,还是以上面的例子,执行结果耗时打印出来是154,但是不知道是耗时154ms还是154s,比较模糊,假设这个单位我们支持通过参数传递。那么首先配置文件修改成如下所示,其中property指定了向拦截器传递一个name为MyPlugin,value为ms的参数

<plugins><plugin interceptor="com.lzj.plugins.LogPlugin" ><property name="MyPlugin" value="ms"/></plugin>
</plugins>

然后自定义的LogPlugin拦截器类修改如下所示

package com.lzj.plugins;import org.apache.ibatis.executor.Executor;
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.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.util.Properties;@Intercepts(value = {@Signature(type = Executor.class,method = "query",args = {MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class})}
)
public class LogPlugin implements Interceptor {private Properties properties;@Overridepublic Object intercept(Invocation invocation) throws Throwable {long startTime = System.currentTimeMillis();Object result = invocation.proceed();System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + properties.getProperty("MyPlugin"));return result;}@Overridepublic Object plugin(Object target) {return Interceptor.super.plugin(target);}@Overridepublic void setProperties(Properties properties) {this.properties = properties;}
}

再次执行结果打印的耗时就带单位了,如下所示

Opening JDBC Connection
Created connection 868815265.
==>  Preparing: select * from car where name=? 
==> Parameters: xiaoli(String)
<==    Columns: name, brand
<==        Row: xiaoli, BYD
<==      Total: 1
耗时:146ms

http://www.ppmy.cn/server/38501.html

相关文章

MySQL慢查询优化

当需要优化MySQL的慢查询时&#xff0c;通常需要结合多个方面进行分析和优化&#xff0c;包括索引优化、SQL语句重构、数据库结构调整等。下面&#xff0c;我将通过一个例子来说明如何优化MySQL的慢查询&#xff0c;包括多表关联和条件查询。 假设我们有一个简化的电子商务系统…

MATLAB基础应用精讲-【数模应用】信度分析(附MATLAB和R语言代码实现)

目录 前言 几个高频面试题目 信度和效度对比 一、信度 二、效度

深入探究MySQL常用的存储引擎

前言 MySQL是一个广泛使用的开源关系型数据库管理系统&#xff0c;它支持多种存储引擎。存储引擎决定了MySQL数据库如何存储、检索和管理数据。不同的存储引擎具有不同的特点、性能表现和适用场景。选择适合的存储引擎对于优化数据库性能、确保数据完整性和安全性至关重要。本…

uniapp 小程序低功耗蓝牙配网 ble配网 物联网

1.获取蓝牙列表 bleList.vue <template><view><button touchstart"startSearch">获取蓝牙列表</button><scroll-view :scroll-top"scrollTop" scroll-y class"content-pop"><viewclass"bluetoothItem&q…

【MySQL】事务及其隔离性/隔离级别

目录 一、事务的概念 1、事务的四种特性 2、事务的作用 3、存储引擎对事务的支持 4、事务的提交方式 二、事务的启动、回滚与提交 1、准备工作&#xff1a;调整MySQL的默认隔离级别为最低/创建测试表 2、事务的启动、回滚与提交 3、启动事务后未commit&#xff0c;但是…

ansible—playbook的template、tags、roles模块

目录 一、template 1、简介 2、template模块实例 1.先准备一个以.j2结尾的template模板文件&#xff0c;设置引用的变量&#xff0c;ansible上要先安装httpd 2、修改主机清单文件&#xff0c;使用主机变量定义一个变量名相同而值不同的变量 3、主机添加hosts 4、编写pla…

Flutter笔记:Widgets Easier组件库(11)- 使用提示吐丝(Tip Toasts)

Flutter笔记 Widgets Easier组件库&#xff08;11&#xff09;使用提示吐丝 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this …

ARP欺骗使局域网内设备断网

一、实验准备 kali系统&#xff1a;可使用虚拟机软件模拟 kali虚拟机镜像链接&#xff1a;https://www.kali.org/get-kali/#kali-virtual-machines 注意虚拟机网络适配器采用桥接模式 局域网内存在指定断网的设备 二、实验步骤 打开kali系统命令行&#xff1a;ctrlaltt可快…