添加自动填充

This commit is contained in:
2024-03-28 11:08:17 +08:00
parent 646a26b754
commit 4aef506c93
5 changed files with 235 additions and 0 deletions

View File

@@ -59,6 +59,11 @@
<artifactId>ruoyi-system</artifactId>
</dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>

View File

@@ -0,0 +1,46 @@
package com.ruoyi.framework.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
/**
* 自动填充处理类
*
* @author jishanfeng
* @date 2024-01-12
*/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
// 获取当前登录用户
String userName = SecurityUtils.getUsername();
fillValue("createBy", userName, metaObject);
fillValue("createTime", DateUtils.getNowDate(), metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
// 获取当前登录用户
String userName = SecurityUtils.getUsername();
fillValue("updateBy", userName, metaObject);
fillValue("updateTime", DateUtils.getNowDate(), metaObject);
}
private void fillValue(String fieldName, Object data, MetaObject metaObject) {
if (metaObject.hasSetter(fieldName)) {
// 值为空时设置默认值
Object sidObj = getFieldValByName(fieldName, metaObject);
if (sidObj == null || "updateBy".equals(fieldName) || "updateTime".equals(fieldName)) {
setFieldValByName(fieldName, data, metaObject);
}
}
}
}