value
2024-05-09 f8a3a9826738a397bdf4aff4ca7a5116344b5a0a
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
package com.yuanchu.mom.mybatis_config;
 
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yuanchu.mom.utils.Jwt;
import com.yuanchu.mom.utils.ServletUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
 
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
 
    @Autowired
    Jwt jwt;
 
    @Override
    public void insertFill(MetaObject metaObject) {
        Integer userId = null;
        try {
            userId = Integer.parseInt(JSONUtil.parseObj(jwt.readJWT(ServletUtils.getRequest().getHeader("token")).get("data")).get("id") + "");
        }catch (Exception ignored){}
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        this.strictInsertFill(metaObject, "createUser", Integer.class, userId);
        this.strictInsertFill(metaObject, "updateUser", Integer.class, userId);
    }
 
    @Override
    public void updateFill(MetaObject metaObject) {
        Integer userId = null;
        try {
            userId = Integer.parseInt(JSONUtil.parseObj(jwt.readJWT(ServletUtils.getRequest().getHeader("token")).get("data")).get("id") + "");
        }catch (Exception ignored){}
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
        this.strictUpdateFill(metaObject, "updateUser", Integer.class, userId);
    }
}