MyBatis 插件之拦截器(Interceptor)
参考 https://blog.csdn.net/weixin_39494923/article/details/91534658
//项目实际使用 就是在你进行数据库操作时,进行数据的第二次封装
package com.yueworldframework.core.mybatis; import com.yueworldframework.core.support.SessionHelper;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils; import java.lang.reflect.Method;
import java.util.Date;
import java.util.Properties; /**
* Created by wangbs
*/
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class AuditingInterceptor implements Interceptor {
private static final Logger logger = LoggerFactory.getLogger(AuditingInterceptor.class); private Properties props = null; @Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
if (invocation.getArgs()[1] instanceof Pojo) {
Pojo parameter = (Pojo) invocation.getArgs()[1];
if (SqlCommandType.INSERT == sqlCommandType) {
// 初始化主键ID
Method initializeUUID = BeanUtils.findDeclaredMethod(parameter.getClass(), "initializeUUID");
if (null != initializeUUID) {
initializeUUID.invoke(parameter);
}
if(null==parameter.getCreator()){
parameter.setCreator(SessionHelper.getId());
}
parameter.setUpdater(SessionHelper.getId());
parameter.setCreatedDate(new Date());
parameter.setUpdatedDate(parameter.getCreatedDate());
parameter.setVersion(1);
} else if (SqlCommandType.UPDATE == sqlCommandType) {
parameter.setUpdater(SessionHelper.getId());
parameter.setUpdatedDate(new Date());
parameter.setVersion(parameter.getVersion() + 1);
}
}
return invocation.proceed();
} @Override
public Object plugin(Object target) {
if (target instanceof Executor) {
return Plugin.wrap(target, this);
} else {
return target;
}
} public void setProperties(Properties properties) {
if (null != properties && !properties.isEmpty()) props = properties;
}
}
MyBatis 插件之拦截器(Interceptor)的更多相关文章
- MyBatis功能点二:MyBatis提供的拦截器平台
前面关于MyBatis功能点二plugin已经介绍了一些应用及其实现的底层代码,本文总结MyBatis提供的拦截器平台框架体系. 通过MyBatis功能点二:从责任链设计模式的角度理解插件实现技术 - ...
- Flume 拦截器(interceptor)详解
flume 拦截器(interceptor)1.flume拦截器介绍拦截器是简单的插件式组件,设置在source和channel之间.source接收到的事件event,在写入channel之前,拦截 ...
- 5-21 拦截器 Interceptor
Spring MVC拦截器 什么是拦截器 拦截器是SpringMvc框架提供的功能 它可以在控制器方法运行之前或运行之后(还有其它特殊时机)对请求进行处理或加工的特定接口 常见面试题:过滤器和拦截器的 ...
- struts2学习笔记--拦截器(Interceptor)和登录权限验证Demo
理解 Interceptor拦截器类似于我们学过的过滤器,是可以在action执行前后执行的代码.是我们做web开发是经常使用的技术,比如权限控制,日志.我们也可以把多个interceptor连在一起 ...
- struts2拦截器interceptor的三种配置方法
1.struts2拦截器interceptor的三种配置方法 方法1. 普通配置法 <struts> <package name="struts2" extend ...
- MVC框架的插件与拦截器基础
自制MVC框架的插件与拦截器基础 上篇谈到我自己写的MVC框架,接下来讲讲插件及拦截器! 在处理一些通用的逻辑最好把它封装一个插件或者拦截器,以便日后可以直接拿过来直接使用.在我的框架中可以通过继承以 ...
- SSM-SpringMVC-33:SpringMVC中拦截器Interceptor讲解
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 拦截器Interceptor: 对处理方法进行双向的拦截,可以对其做日志记录等 我选择的是实现Handler ...
- 过滤器(Filter)和拦截器(Interceptor)
过滤器(Filter) Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序.它依赖于servlet容器,在实现上,基于函数回调,它可以对几乎所有请求 ...
- 二十五、过滤器Filter,监听器Listener,拦截器Interceptor的区别
1.Servlet:运行在服务器上可以动态生成web页面.servlet的声明周期从被装入到web服务器内存,到服务器关闭结束.一般启动web服务器时会加载servelt的实例进行装入,然后初始化工作 ...
随机推荐
- <转> Android LayoutInflater详解
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- 【DELL存储】EMC会议 超融合+存储
场景:盐城工厂 IT人数 4个人 机房200台 主要以虚拟化为主 实体机 PG ORACAL MYSQL dell产品线 提供整体方案 架构 针对整车厂 :传统+超融合 1. 介绍产品 1.1 超融 ...
- WebClient 与HttpClient 的区别
需要搜索下资料. -------------------------------------------------- 微软文档介绍,新的开发中推荐使用:HttpClient WebClient 文档 ...
- 某网站的videojs的配置及操作
某网站的videojs的配置及操作 一.总结 一句话总结: 多参照参照别人的例子就好,省事 1.videojs如何获取用户当前视频的位置? this.currentTime() 2.回到视频开始处? ...
- Using Microsoft Visual C++ DLLs with C++Builder
Using Microsoft Visual C++ DLLs with C++Builder As powerful as C++Builder is, the majority of DLLs d ...
- MySQL进阶16 - 视图的创建/修改/删除/更新--可更新性的不适用条件
#进阶16 : 视图 /* 含义: 虚拟表,和普通表一样使用;(从5.1开始使用的:)是通过表动态生成的数据 创建语法: create view 视图名 as 查询语句; ---------- 作用: ...
- P2P system: Chord
DHT= Distributed Hash Table store the objects(files) at nodes (hosts, machines) in a cluster. The cl ...
- 聊聊Hash索引
hash index是基于哈希表实现的,只有精确匹配索引所有列的查询才会生效.对于每一行数据,存储引擎都会对所有的索引列计算一个hash code,并将的有的hash code存储在索引中,同时在哈希 ...
- 关于前端 jQuery 面试的知识点
参考一个博主整理的一些前端 jQuery 的一些面试题 参考博客:https://www.cnblogs.com/dashucoding/p/11140325.html 参考博客:https://ww ...
- 008——MATLAB-xlswrite的使用方法
(一)参考文献:https://blog.csdn.net/liangjiubujiu/article/details/80455753 以矩阵A=[1 2 3 4;5 6 7 8]为例进行介绍 例1 ...