栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis自定义拦截器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis自定义拦截器

1. 拦截器注解

1. mybatis自定义拦截器实现步骤:

  1. 实现org.apache.ibatis.plugin.Interceptor接口。
  2. 添加拦截器注解org.apache.ibatis.plugin.Intercepts。
  3. 配置文件中添加拦截器。

2. 在mybatis中可被拦截的类型有四种(按照拦截顺序):

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

具体规则如下:

@Intercepts({
    @Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
    @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
    @Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})
})
  1. @Intercepts:标识该类是一个拦截器;
  2. @Signature:指明自定义拦截器需要拦截哪一个类型,哪一个方法;
    2.1 type:对应四种类型中的一种;
    2.2 method:对应接口中的哪类方法(因为可能存在重载方法);
    2.3 args:对应哪一个方法;

2. 拦截器可拦截的方法:

拦截的类拦截的方法
Executorupdate, query, flushStatements, commit, rollback,getTransaction, close, isClosed
ParameterHandlergetParameterObject, setParameters
StatementHandlerprepare, parameterize, batch, update, query
ResultSetHandlerhandleResultSets, handleOutputParameters

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/314616.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号