自定义注解CreatedTimeTag和UpdateTimeTag处理2个时间字段

@Retention(RetentionPolicy.RUNTIME)
@Target( {ElementType.FIELD})
public @interface CreatedTimeTag {String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target( {ElementType.FIELD})
public @interface UpdateTimeTag {String value() default "";
}

插件拦截:

@Intercepts({@Signature(type = org.apache.ibatis.executor.Executor.class,method = "update",args = {MappedStatement.class, Object.class})})
public class CreateUpdateTimeInterceptor implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];// 获取 SQL 命令SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();// 获取参数Object parameter = invocation.getArgs()[1];// 获取私有成员变量Field[] declaredFields = parameter.getClass().getDeclaredFields();if (parameter.getClass().getSuperclass() != null) {Field[] superField = parameter.getClass().getSuperclass().getDeclaredFields();declaredFields = ArrayUtils.addAll(declaredFields, superField);}for (Field field : declaredFields) {// insertif (field.getAnnotation(CreatedTimeTag.class) != null) {if (SqlCommandType.INSERT.equals(sqlCommandType)) {field.setAccessible(true);field.set(parameter, new Timestamp(System.currentTimeMillis()));}}// updateif (field.getAnnotation(UpdateTimeTag.class) != null) {if (SqlCommandType.INSERT.equals(sqlCommandType)|| SqlCommandType.UPDATE.equals(sqlCommandType)) {field.setAccessible(true);field.set(parameter, new Timestamp(System.currentTimeMillis()));}}}return invocation.proceed();}@Overridepublic Object plugin(Object target) {if (target instanceof org.apache.ibatis.executor.Executor) {return Plugin.wrap(target, this);}return target;}@Overridepublic void setProperties(Properties properties) {}}

注册插件:

  @Beanpublic SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {SqlSessionFactoryBean factory = new SqlSessionFactoryBean();factory.setDataSource(dataSource);factory.setTypeAliasesPackage(ProjectConstant.MODEL_PACKAGE);factory.setPlugins(new Interceptor[]{new CreateUpdateTimeInterceptor()});// 添加XML目录ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));return factory.getObject();}

Mybatis插件处理createTime和updateTime相关推荐

  1. idea mybatis plugin插件,免费mybatis插件

    idea的mybatis插件.一直想下一个,在大批量修改一些问题时候 mapper和.xml文件查看会方便许多. 直接在idea的插件market里看经常会卡住,直接去网站看. 于是去官网查查看,网站 ...

  2. c++map的使用_mybatis源码 | mybatis插件及动态代理的使用

    学习背景 最近公司在做一些数据库安全方面的事情,如数据库中不能存手机号明文,不能存身份证号明文, 但是项目已经进行了好几个月了, 这时候在应用层面去改显然不太现实, 所以就有了Mybatis的自定义插 ...

  3. Intelij IDEA 2016.3安装mybatis插件并激活教程

    转载自:http://blog.csdn.net/solo_talk/article/details/53540449 现在Mybatis框架越来越受欢迎,Intelij IDEA这个编辑器逐渐成为很 ...

  4. mybatis 插件原理

    [传送门]:mybatis 插件原理 转载于:https://www.cnblogs.com/virgosnail/p/10079838.html

  5. MyBatis 插件原理与自定义插件-猜想

    MyBatis 的插件可以在不修改原来的代码的情况下,通过拦截的方式,改变四大核心对象的行为,比如处理参数,处理SQL,处理结果. 第一个问题: 不修改对象的代码,怎么对对象的行为进行修改,比如说在原 ...

  6. MyBatis(四)MyBatis插件原理

    MyBatis插件原理 MyBatis对开发者非常友好,它通过提供插件机制,让我们可以根据自己的需要去增强MyBatis的功能.其底层是使用了代理模式+责任链模式 MyBatis官方https://m ...

  7. idea mybatis插件_IntelliJ IDEA插件推荐(二)

    上次发过IntelliJ IDEA的插件推荐,详见:IntelliJ IDEA插件推荐,但是由于篇幅有限加上一些网友的推荐和自己这段时间的接触到一些新的非常实用的插件,所以又写一篇插件推荐.废话不多说 ...

  8. 后端技术:mybatis插件原理详解

    关注"Java后端技术全栈" 回复"面试"获取全套面试资料 上次发文说到了如何集成分页插件MyBatis插件原理分析,看完感觉自己better了,今天我们接着来 ...

  9. SpringCloud或SpringBoot+Mybatis-Plus利用mybatis插件实现数据操作记录及更新对比

    引文 本文主要介绍如何使用mybatis插件实现拦截数据库操作并根据不同需求进行数据对比分析,主要适用于系统中需要对数据操作进行记录.在更新数据时准确记录更新字段 核心:mybatis插件(拦截器). ...

最新文章

  1. 国际顶刊《PNAS》:爱发朋友圈的人,更容易长寿
  2. Yii 框架调试之CWebLogRoute
  3. crt python_SecureCRT Python
  4. php 单用户登录,Linux 系统的单用户模式、修复模式、跨控制台登录在系统修复中的运用...
  5. CSS的Grid网格布局
  6. linux定时备份mysql数据库文件脚本
  7. dz自动开起html,discuz论坛开启markdown 允许html代码嵌入js
  8. [css] 使用rem的优缺点是什么?和使用百分比有什么区别?
  9. Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!
  10. linux 桥,Linux网桥
  11. Enterprise Architect(简称EA)使用技巧
  12. template < class T> ,map和vector用法——恶补c++
  13. Linux网卡bond
  14. 用来做重试的库Tenacity
  15. 《linux内核分析》第二次课 实验作业
  16. NVIDIA Canvas 初体验~
  17. 上一页 1 2 3 ... 10 下一页 固定分页
  18. android录音声波动画,Android开发:仿微信 录音声波
  19. docker 创建容器时指定容器ip
  20. 实验室NEWIFI-D1路由小云系统简易配置参考

热门文章

  1. 一文读懂:GPU加速是什么?
  2. html错位排列,解决“错位排列”问题的一般方法
  3. Activiti6---通用拒绝
  4. 天创速盈拼多多引流玩法,快速引爆首页
  5. linux ctags目录在哪里,Linux(centos)下vim、ctags的配置及基本用法详解
  6. 为什么动态链接.dll和.lib都需要(详解静、动态链接库)
  7. express中res.sendFile、res.render、express.static()三者的使用
  8. SharePoint 2013 安装图解
  9. 个人题库--不怕万人阻挡,就怕自己投降
  10. Markdown快速入门教程