fix:修复使用Gson序列化时,MyBatis自动填充createTime和updateTime不一致的问题

pull/246/head
wangxu 2025-01-01 01:11:02 +08:00
parent d2c9912724
commit ac85622227
1 changed files with 41 additions and 20 deletions

View File

@ -21,7 +21,6 @@ import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.*; import cn.hutool.core.util.*;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
@ -81,6 +80,8 @@ import java.lang.reflect.Array;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -268,7 +269,8 @@ public class GlobalConfigure implements WebMvcConfigurer {
// 如果是预检请求,则立即返回到前端 // 如果是预检请求,则立即返回到前端
SaRouter.match(SaHttpMethod.OPTIONS) SaRouter.match(SaHttpMethod.OPTIONS)
// OPTIONS预检请求不做处理 // OPTIONS预检请求不做处理
.free(r -> {}) .free(r -> {
})
.back(); .back();
}) })
@ -357,7 +359,7 @@ public class GlobalConfigure implements WebMvcConfigurer {
// 获取该接口缓存的限流数据跟当前ip以及登录用户有关 // 获取该接口缓存的限流数据跟当前ip以及登录用户有关
String cacheKey = COMMON_REPEAT_SUBMIT_CACHE_KEY + CommonIpAddressUtil.getIp(request) + StrUtil.COLON; String cacheKey = COMMON_REPEAT_SUBMIT_CACHE_KEY + CommonIpAddressUtil.getIp(request) + StrUtil.COLON;
Object loginId = StpUtil.getLoginIdDefaultNull(); Object loginId = StpUtil.getLoginIdDefaultNull();
if(ObjectUtil.isNotEmpty(loginId)) { if (ObjectUtil.isNotEmpty(loginId)) {
cacheKey = cacheKey + Convert.toStr(loginId) + StrUtil.COLON + url; cacheKey = cacheKey + Convert.toStr(loginId) + StrUtil.COLON + url;
} else { } else {
cacheKey = cacheKey + url; cacheKey = cacheKey + url;
@ -365,14 +367,14 @@ public class GlobalConfigure implements WebMvcConfigurer {
Object cacheObj = commonCacheOperator.get(cacheKey); Object cacheObj = commonCacheOperator.get(cacheKey);
if (ObjectUtil.isNotEmpty(cacheObj)) { if (ObjectUtil.isNotEmpty(cacheObj)) {
JSONObject cacheJsonObject = JSONUtil.parseObj(cacheObj); JSONObject cacheJsonObject = JSONUtil.parseObj(cacheObj);
if(cacheJsonObject.containsKey(url)) { if (cacheJsonObject.containsKey(url)) {
JSONObject existRepeatJsonObject = cacheJsonObject.getJSONObject(url); JSONObject existRepeatJsonObject = cacheJsonObject.getJSONObject(url);
// 如果与上次参数一致,且时间间隔小于要求的限流时长,则判定为重复提交 // 如果与上次参数一致,且时间间隔小于要求的限流时长,则判定为重复提交
if (jsonObject.getStr("repeatParam").equals(existRepeatJsonObject.getStr("repeatParam"))) { if (jsonObject.getStr("repeatParam").equals(existRepeatJsonObject.getStr("repeatParam"))) {
long interval = jsonObject.getLong("repeatTime") - existRepeatJsonObject.getLong("repeatTime"); long interval = jsonObject.getLong("repeatTime") - existRepeatJsonObject.getLong("repeatTime");
if(interval < commonNoRepeat.interval()) { if (interval < commonNoRepeat.interval()) {
long secondsParam = (commonNoRepeat.interval() - interval) / 1000; long secondsParam = (commonNoRepeat.interval() - interval) / 1000;
if(secondsParam > 0) { if (secondsParam > 0) {
throw new CommonException("请求过于频繁,请" + CommonTimeFormatUtil.formatSeconds(secondsParam) + "后再试"); throw new CommonException("请求过于频繁,请" + CommonTimeFormatUtil.formatSeconds(secondsParam) + "后再试");
} }
} }
@ -563,19 +565,29 @@ public class GlobalConfigure implements WebMvcConfigurer {
@Component @Component
public static class CustomMetaObjectHandler implements MetaObjectHandler { public static class CustomMetaObjectHandler implements MetaObjectHandler {
/** 删除标志 */ /**
*
*/
private static final String DELETE_FLAG = "deleteFlag"; private static final String DELETE_FLAG = "deleteFlag";
/** 创建人 */ /**
*
*/
private static final String CREATE_USER = "createUser"; private static final String CREATE_USER = "createUser";
/** 创建时间 */ /**
*
*/
private static final String CREATE_TIME = "createTime"; private static final String CREATE_TIME = "createTime";
/** 更新人 */ /**
*
*/
private static final String UPDATE_USER = "updateUser"; private static final String UPDATE_USER = "updateUser";
/** 更新时间 */ /**
*
*/
private static final String UPDATE_TIME = "updateTime"; private static final String UPDATE_TIME = "updateTime";
@Override @Override
@ -586,21 +598,26 @@ public class GlobalConfigure implements WebMvcConfigurer {
if (ObjectUtil.isNull(deleteFlag)) { if (ObjectUtil.isNull(deleteFlag)) {
setFieldValByName(DELETE_FLAG, EnumUtil.toString(CommonDeleteFlagEnum.NOT_DELETE), metaObject); setFieldValByName(DELETE_FLAG, EnumUtil.toString(CommonDeleteFlagEnum.NOT_DELETE), metaObject);
} }
} catch (ReflectionException ignored) { } } catch (ReflectionException ignored) {
}
try { try {
//为空则设置createUser //为空则设置createUser
Object createUser = metaObject.getValue(CREATE_USER); Object createUser = metaObject.getValue(CREATE_USER);
if (ObjectUtil.isNull(createUser)) { if (ObjectUtil.isNull(createUser)) {
setFieldValByName(CREATE_USER, this.getUserId(), metaObject); setFieldValByName(CREATE_USER, this.getUserId(), metaObject);
} }
} catch (ReflectionException ignored) { } } catch (ReflectionException ignored) {
}
try { try {
//为空则设置createTime //为空则设置createTime
Object createTime = metaObject.getValue(CREATE_TIME); Object createTime = metaObject.getValue(CREATE_TIME);
if (ObjectUtil.isNull(createTime)) { if (ObjectUtil.isNull(createTime)) {
setFieldValByName(CREATE_TIME, DateTime.now(), metaObject); LocalDateTime dt = LocalDateTime.now();
String now = dt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
setFieldValByName(CREATE_TIME, now, metaObject);
}
} catch (ReflectionException ignored) {
} }
} catch (ReflectionException ignored) { }
} }
@Override @Override
@ -608,11 +625,15 @@ public class GlobalConfigure implements WebMvcConfigurer {
try { try {
//设置updateUser //设置updateUser
setFieldValByName(UPDATE_USER, this.getUserId(), metaObject); setFieldValByName(UPDATE_USER, this.getUserId(), metaObject);
} catch (ReflectionException ignored) { } } catch (ReflectionException ignored) {
}
try { try {
//设置updateTime //设置updateTime
setFieldValByName(UPDATE_TIME, DateTime.now(), metaObject); LocalDateTime dt = LocalDateTime.now();
} catch (ReflectionException ignored) { } String now = dt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
setFieldValByName(UPDATE_TIME, now, metaObject);
} catch (ReflectionException ignored) {
}
} }
/** /**