chenrui
10 天以前 beeba1259f983a5f92e6b8137d2fb05389f184b4
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
package com.ruoyi.common.config;
 
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
 
@Component
public class MybatisHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Integer userId = null;
        Integer tenantId = null;
        try {
            userId = SecurityUtils.getUserId().intValue();
            tenantId = SecurityUtils.getLoginUser().getTenantId();
        } catch (Exception ignored) {
        }
        System.out.println("执行插入填充...");
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime",  LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createUser", Integer.class, userId);
        this.strictInsertFill(metaObject, "updateUser", Integer.class, userId);
        this.strictInsertFill(metaObject, "tenantId", Integer.class, tenantId);
    }
 
    @Override
    public void updateFill(MetaObject metaObject) {
        Integer userId = null;
        try {
            userId = SecurityUtils.getUserId().intValue();
        } catch (Exception ignored) {
        }
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
        this.strictUpdateFill(metaObject, "updateUser", Integer.class, userId);
    }
}