【修复】兼容LocalDateTime以及date
This commit is contained in:
@@ -94,6 +94,6 @@ public class ParamsCenter implements Serializable {
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
@TableField(fill = FieldFill.UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import java.util.Date;
|
||||
|
||||
/**
|
||||
* MyBatis-Plus 自动填充处理器
|
||||
* 用于自动填充创建时间和更新时间
|
||||
* 兼容 Date 和 LocalDateTime 两种时间类型的自动填充
|
||||
*/
|
||||
@Component
|
||||
public class CustomMetaObjectHandler implements MetaObjectHandler {
|
||||
@@ -44,6 +44,7 @@ public class CustomMetaObjectHandler implements MetaObjectHandler {
|
||||
setFieldValByName(DELETE_FLAG, EnumUtil.toString(CommonDeleteFlagEnum.NOT_DELETE), metaObject);
|
||||
}
|
||||
} catch (ReflectionException ignored) { }
|
||||
|
||||
try {
|
||||
//为空则设置createUser
|
||||
Object createUser = metaObject.getValue(CREATE_USER);
|
||||
@@ -51,12 +52,10 @@ public class CustomMetaObjectHandler implements MetaObjectHandler {
|
||||
setFieldValByName(CREATE_USER, this.getUserId(), metaObject);
|
||||
}
|
||||
} catch (ReflectionException ignored) { }
|
||||
|
||||
try {
|
||||
//为空则设置createTime
|
||||
Object createTime = metaObject.getValue(CREATE_TIME);
|
||||
if (ObjectUtil.isNull(createTime)) {
|
||||
setFieldValByName(CREATE_TIME, DateTime.now(), metaObject);
|
||||
}
|
||||
// 兼容 Date 和 LocalDateTime 类型的 createTime 填充
|
||||
fillTimeField(metaObject, CREATE_TIME);
|
||||
} catch (ReflectionException ignored) { }
|
||||
}
|
||||
|
||||
@@ -67,13 +66,38 @@ public class CustomMetaObjectHandler implements MetaObjectHandler {
|
||||
setFieldValByName(UPDATE_USER, this.getUserId(), metaObject);
|
||||
} catch (ReflectionException ignored) {
|
||||
}
|
||||
|
||||
try {
|
||||
//设置updateTime
|
||||
setFieldValByName(UPDATE_TIME, DateTime.now(), metaObject);
|
||||
// 兼容 Date 和 LocalDateTime 类型的 updateTime 填充
|
||||
fillTimeField(metaObject, UPDATE_TIME);
|
||||
} catch (ReflectionException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一填充时间字段(自动识别 Date/LocalDateTime 类型)
|
||||
* @param metaObject 元对象
|
||||
* @param fieldName 时间字段名(createTime/updateTime)
|
||||
*/
|
||||
private void fillTimeField(MetaObject metaObject, String fieldName) {
|
||||
// 先判断字段是否为空
|
||||
Object fieldValue = metaObject.getValue(fieldName);
|
||||
if (ObjectUtil.isNotNull(fieldValue)) {
|
||||
return; // 字段已有值,不填充
|
||||
}
|
||||
|
||||
// 获取字段的类型,自动匹配填充值类型
|
||||
Class<?> fieldType = metaObject.getGetterType(fieldName);
|
||||
if (fieldType == LocalDateTime.class) {
|
||||
// 字段是 LocalDateTime 类型
|
||||
setFieldValByName(fieldName, LocalDateTime.now(), metaObject);
|
||||
} else if (fieldType == Date.class || fieldType == DateTime.class) {
|
||||
// 字段是 Date/hutool DateTime 类型
|
||||
setFieldValByName(fieldName, new Date(), metaObject);
|
||||
}
|
||||
// 其他类型不处理(避免填充错误)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户id
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user