提交 | 用户 | 时间
|
9f7aa7
|
1 |
package com.billion.framework.config; |
吴 |
2 |
|
|
3 |
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; |
|
4 |
import com.billion.common.core.domain.model.LoginUser; |
|
5 |
import com.billion.common.utils.SecurityUtils; |
|
6 |
import com.billion.common.utils.StringUtils; |
|
7 |
import org.apache.commons.lang3.ObjectUtils; |
|
8 |
import org.apache.ibatis.reflection.MetaObject; |
|
9 |
import org.springframework.stereotype.Component; |
|
10 |
|
|
11 |
import java.time.LocalDateTime; |
|
12 |
import java.util.Date; |
|
13 |
|
|
14 |
/** |
|
15 |
* 简介说明:Mybatis-Plus配置 |
|
16 |
* |
|
17 |
* @author: Eric |
|
18 |
* @date: 2024-11-20 15:46:49 |
|
19 |
* @version: 1.0 |
|
20 |
*/ |
|
21 |
@Component |
|
22 |
public class MyMetaObjectHandler implements MetaObjectHandler{ |
|
23 |
|
|
24 |
@Override |
|
25 |
public void insertFill(MetaObject metaObject) { |
|
26 |
// 检查是否有名为createTime的字段,如果有则设置当前时间 |
|
27 |
boolean hasCreateTime = metaObject.hasSetter("createTime"); |
|
28 |
if (hasCreateTime) { |
|
29 |
metaObject.setValue("createTime", new Date()); |
|
30 |
} |
|
31 |
|
|
32 |
// 检查是否有名为updateTime的字段,如果有则设置当前时间 |
|
33 |
boolean hasUpdateTime = metaObject.hasSetter("updateTime"); |
|
34 |
if (hasUpdateTime) { |
|
35 |
metaObject.setValue("updateTime", new Date()); |
|
36 |
} |
|
37 |
|
|
38 |
LoginUser securityUser = SecurityUtils.getLoginUser(); |
|
39 |
if (ObjectUtils.isNotEmpty(securityUser)){ |
|
40 |
String username = securityUser.getUsername(); |
|
41 |
if (StringUtils.isNoneBlank(username)){ |
|
42 |
boolean hasCreateBy = metaObject.hasSetter("createBy"); |
|
43 |
if (hasCreateBy){ |
|
44 |
metaObject.setValue("createBy", username); |
|
45 |
} |
|
46 |
boolean hasUpdateBy = metaObject.hasSetter("updateBy"); |
|
47 |
if (hasUpdateBy){ |
|
48 |
metaObject.setValue("updateBy", username); |
|
49 |
} |
|
50 |
} |
|
51 |
} |
|
52 |
|
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public void updateFill(MetaObject metaObject) { |
|
57 |
// 检查是否有名为updateTime的字段,如果有则设置当前时间 |
|
58 |
boolean hasUpdateTime = metaObject.hasSetter("updateTime"); |
|
59 |
if (hasUpdateTime) { |
|
60 |
metaObject.setValue("updateTime", new Date()); |
|
61 |
} |
|
62 |
|
|
63 |
LoginUser securityUser = SecurityUtils.getLoginUser(); |
|
64 |
if (ObjectUtils.isNotEmpty(securityUser)){ |
|
65 |
String username = securityUser.getUsername(); |
|
66 |
if (StringUtils.isNoneBlank(username)){ |
|
67 |
boolean hasUpdateBy = metaObject.hasSetter("updateBy"); |
|
68 |
if (hasUpdateBy){ |
|
69 |
metaObject.setValue("updateBy", username); |
|
70 |
} |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
} |