IT俱乐部 Java MyBatis拦截器如何自动设置创建时间和修改时间

MyBatis拦截器如何自动设置创建时间和修改时间

前言

在日常的插入和修改的时候要频繁的插入时间,浪费时间,可以通过实现mybatis的 Intercepts注解来实现,获取实体,并且在实体里面插入日期

一、实现Interceptor接口,并写相关逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.ruoyi.common.filter;
 
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
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.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.Properties;
 
 
/**
 *  Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)   拦截执行器的方法
    ParameterHandler (getParameterObject, setParameters)    拦截参数的处理
    ResultSetHandler (handleResultSets, handleOutputParameters) 拦截结果集的处理
    StatementHandler (prepare, parameterize, batch, update, query) 拦截Sql语法构建的处理
 * @author Administrator
 *
 */
@Intercepts({@Signature(type = Executor.class,method = "update",args = {MappedStatement.class,Object.class})})
@Component
public class HandleTimeInterceptor implements Interceptor {
 
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
         Object[] args = invocation.getArgs();
         MappedStatement mappedStatement = (MappedStatement) args[0];
         SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
         if(sqlCommandType== SqlCommandType.UPDATE) {
             if(args[1] instanceof BaseEntity) {
                BaseEntity baseEntity = (BaseEntity) args[1];
                baseEntity.setUpdateTime(new Date());
                String userId="";
                 try
                 {
                     userId= SecurityUtils.getUserId().toString();
                 }catch (Exception e){
                    // throw new BaseException("当前没有登录人");
 
                 }
                baseEntity.setUpdateBy(userId);
             }
         }else if(sqlCommandType==SqlCommandType.INSERT) {
             if(args[1] instanceof BaseEntity) {
                BaseEntity baseEntity = (BaseEntity) args[1];
                baseEntity.setCreateTime(new Date());
                String userId="";
                try
                {
                 userId= SecurityUtils.getUserId().toString();
                }catch (Exception e){
                    //throw new BaseException("当前没有登录人");
                }
                baseEntity.setCreateBy(userId);
            }
         }
 
        return invocation.proceed();
    }
 
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
 
    @Override
    public void setProperties(Properties properties) {
    }
}

二、将插件注册到mybatis 的配置文件 mybatis-config.xml

1
 

总结

然后就可以实现在实体里面自动设置创建时间和修改时间

以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/java/14773.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部