当前位置: 首页 > news >正文

MyBatis原理-拦截器

一、MyBatis拦截器介绍

MyBatis提供了一种插件(plugin)的功能,虽然叫做插件,但其实这是拦截器功能。

MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:

  1. Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
  2. ParameterHandler (getParameterObject, setParameters)
  3. ResultSetHandler (handleResultSets, handleOutputParameters)
  4. StatementHandler (prepare, parameterize, batch, update, query)

我们看到了可以拦截Executor接口的部分方法,比如update,query,commit,rollback等方法,还有其他接口的一些方法等。

总体概括为:

  1. 拦截执行器的方法
  2. 拦截参数的处理
  3. 拦截结果集的处理
  4. 拦截Sql语法构建的处理

 

二、拦截器的使用

MyBatis拦截器的接口定义:

package org.apache.ibatis.plugin;

import java.util.Properties;

/**
 * @author Clinton Begin
 */
public interface Interceptor {

  Object intercept(Invocation invocation) throws Throwable;

  Object plugin(Object target);

  void setProperties(Properties properties);

}

  

下面的MyBatis官网的一个拦截器实例:

@Intercepts({@Signature(
  type= Executor.class,
  method = "update", // 拦截update方法
  args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
  public Object intercept(Invocation invocation) throws Throwable {
    return invocation.proceed();
  }
  public Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }
  public void setProperties(Properties properties) {
  }
}

  

全局xml配置:

<plugins>
    <plugin interceptor="org.format.mybatis.cache.interceptor.ExamplePlugin"></plugin>
</plugins>

分页插件PageHelper配置

 <!-- 配置mybatis的分页插件PageHelper -->
 <plugins>
     <!-- com.github.pagehelper为PageHelper类所在包名 -->
     <plugin interceptor="com.github.pagehelper.PageHelper">
         <!-- 设置数据库类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
         <property name="dialect" value="mysql"/>
     </plugin>
 </plugins>

springboot的PageHelper配置

//配置mybatis的分页插件pageHelper
 2     @Bean
 3     public PageHelper pageHelper(){
 4         PageHelper pageHelper = new PageHelper();
 5         Properties properties = new Properties();
 6         properties.setProperty("offsetAsPageNum","true");
 7         properties.setProperty("rowBoundsWithCount","true");
 8         properties.setProperty("reasonable","true");
 9         properties.setProperty("dialect","mysql");    //配置mysql数据库的方言
10         pageHelper.setProperties(properties);
11         return pageHelper;
12     }

  

这个拦截器拦截Executor接口的update方法(其实也就是SqlSession的新增,删除,修改操作),所有执行executor的update方法都会被该拦截器拦截到。

 

转载于:https://www.cnblogs.com/harvey2017/p/9578137.html

相关文章:

  • Django项目 第一课 【nvm、node、npm安装及使用】
  • 牛客网暑期ACM多校训练营(第三场) H Diff-prime Pairs(欧拉筛法)
  • CF 1036 B Diagonal Walking v.2 —— 思路
  • 系统完整性检查工具--Tripwire和AIDE
  • tp5 路由定义
  • 随机图片
  • Vue框架的两种使用方式
  • WPF的x:名称空间
  • 15 个 Android 通用流行框架大全
  • BZOJ1926: [Sdoi2010]粟粟的书架
  • php 进行跨域操作
  • 定义和实现相同的顺序
  • 编程语言分类
  • 关于win10下JDK环境变量的配置以及关于JDK的一些说明
  • python包的安装
  • GraphQL学习过程应该是这样的
  • HTTP中GET与POST的区别 99%的错误认识
  • MYSQL 的 IF 函数
  • Node项目之评分系统(二)- 数据库设计
  • python大佬养成计划----difflib模块
  • React+TypeScript入门
  • Web Storage相关
  • 反思总结然后整装待发
  • 计算机常识 - 收藏集 - 掘金
  • 看域名解析域名安全对SEO的影响
  • 前端自动化解决方案
  • 使用SAX解析XML
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (02)Hive SQL编译成MapReduce任务的过程
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (pojstep1.3.1)1017(构造法模拟)
  • (二)c52学习之旅-简单了解单片机
  • (十一)图像的罗伯特梯度锐化
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • .net core 连接数据库,通过数据库生成Modell
  • .net 受管制代码
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • @Autowired注解的实现原理
  • [2021ICPC济南 L] Strange Series (Bell 数 多项式exp)
  • [Android Pro] listView和GridView的item设置的高度和宽度不起作用
  • [AutoSar]BSW_OS 01 priority ceiling protocol(PCP)
  • [C# WPF] 如何给控件添加边框(Border)?
  • [C#]C# OpenVINO部署yolov8图像分类模型
  • [C#]DataTable常用操作总结【转】
  • [codevs] 1029 遍历问题
  • [IE编程] WebBrowser控件的多页面浏览(Tabbed Browsing)开发接口
  • [ISITDTU 2019]EasyPHP
  • [Machine Learning][Part 8]神经网络的学习训练过程
  • [MICROSAR Adaptive] --- Hello Adaptive World
  • [MZ test.16]P1 评测
  • [NEWS] J2SE5.0来了
  • [Phoenix] 七、如何使用自增ID
  • [python 刷题] 2866 Beautiful Towers II