Helius
2020-05-14 5a57e1438d4682d041933d88fbfa37cbc2e77919
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.xcong.excoin.configurations.interceptor;
 
import com.xcong.excoin.common.LoginUserUtils;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import lombok.extern.slf4j.Slf4j;
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.lang.reflect.Field;
import java.util.*;
 
/**
 * mybatis拦截器,自动注入创建人、创建时间、修改人、修改时间
 *
 * @author wzy
 * @date 2020-05-13
 **/
@Slf4j
//@Component
@Intercepts({ @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class MybatisInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        String sqlId = mappedStatement.getId();
        log.info("----sqlId----" + sqlId);
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        log.info("-------------->{}", sqlCommandType);
 
        Object parameter = invocation.getArgs()[1];
        if (parameter == null) {
            return invocation.proceed();
        }
 
        MemberEntity memberEntity = LoginUserUtils.getAppLoginUser();
        if (SqlCommandType.INSERT == sqlCommandType) {
            Field[] fields = this.getAllFields(parameter);
 
            for(Field field : fields) {
                log.info("----->{}", field.getName());
                if ("createBy".equals(field.getName()) || "updateBy".equals(field.getName())) {
                    byField(field, parameter, memberEntity);
                }
 
                if ("createTime".equals(field.getName()) || "updateTime".equals(field.getName())) {
                    timeField(field, parameter, memberEntity);
                }
            }
        }
 
        if (SqlCommandType.UPDATE == sqlCommandType) {
            Field[] fields = this.getAllFields(parameter);
 
            for (Field field : fields) {
                if ("updateBy".equals(field.getName())) {
                    byField(field, parameter, memberEntity);
                }
 
                if ("updateTime".equals(field.getName())) {
                    timeField(field, parameter, memberEntity);
                }
            }
        }
 
        return invocation.proceed();
    }
 
    @Override
    public Object plugin(Object o) {
        return Plugin.wrap(o, this);
    }
 
    @Override
    public void setProperties(Properties properties) {
 
    }
 
    private Field[] getAllFields(Object object) {
        Class<?> clazz = object.getClass();
        List<Field> fieldList = new ArrayList<>();
        while (clazz != null) {
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        return fields;
    }
 
    private void byField(Field field, Object parameter, MemberEntity memberEntity) throws IllegalAccessException {
        field.setAccessible(true);
        Object local = field.get(parameter);
        field.setAccessible(false);
        if (local != null) {
            field.setAccessible(true);
            field.set(parameter, memberEntity.getUsername());
            field.setAccessible(false);
        }
    }
 
    private void timeField(Field field, Object parameter, MemberEntity memberEntity) throws IllegalAccessException {
        field.setAccessible(true);
        Object local_createBy = field.get(parameter);
        field.setAccessible(false);
        if (local_createBy != null) {
            field.setAccessible(true);
            field.set(parameter, new Date());
            field.setAccessible(false);
        }
    }
}