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.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.*;
import cn.hutool.extra.spring.SpringUtil;
@ -81,6 +80,8 @@ import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -268,7 +269,8 @@ public class GlobalConfigure implements WebMvcConfigurer {
// 如果是预检请求,则立即返回到前端
SaRouter.match(SaHttpMethod.OPTIONS)
// OPTIONS预检请求不做处理
.free(r -> {})
.free(r -> {
})
.back();
})
@ -277,7 +279,7 @@ public class GlobalConfigure implements WebMvcConfigurer {
// 由于过滤器中抛出的异常不进入全局异常处理,所以必须提供[异常处理函数]来处理[认证函数]里抛出的异常
// 在[异常处理函数]里的返回值将作为字符串输出到前端此处统一转为JSON输出前端
SaResponse saResponse = SaHolder.getResponse();
saResponse.setHeader(Header.CONTENT_TYPE.getValue(), ContentType.JSON + ";charset=" + CharsetUtil.UTF_8);
saResponse.setHeader(Header.CONTENT_TYPE.getValue(), ContentType.JSON + ";charset=" + CharsetUtil.UTF_8);
return GlobalExceptionUtil.getCommonResult((Exception) e);
});
}
@ -357,7 +359,7 @@ public class GlobalConfigure implements WebMvcConfigurer {
// 获取该接口缓存的限流数据跟当前ip以及登录用户有关
String cacheKey = COMMON_REPEAT_SUBMIT_CACHE_KEY + CommonIpAddressUtil.getIp(request) + StrUtil.COLON;
Object loginId = StpUtil.getLoginIdDefaultNull();
if(ObjectUtil.isNotEmpty(loginId)) {
if (ObjectUtil.isNotEmpty(loginId)) {
cacheKey = cacheKey + Convert.toStr(loginId) + StrUtil.COLON + url;
} else {
cacheKey = cacheKey + url;
@ -365,14 +367,14 @@ public class GlobalConfigure implements WebMvcConfigurer {
Object cacheObj = commonCacheOperator.get(cacheKey);
if (ObjectUtil.isNotEmpty(cacheObj)) {
JSONObject cacheJsonObject = JSONUtil.parseObj(cacheObj);
if(cacheJsonObject.containsKey(url)) {
if (cacheJsonObject.containsKey(url)) {
JSONObject existRepeatJsonObject = cacheJsonObject.getJSONObject(url);
// 如果与上次参数一致,且时间间隔小于要求的限流时长,则判定为重复提交
if (jsonObject.getStr("repeatParam").equals(existRepeatJsonObject.getStr("repeatParam"))) {
long interval = jsonObject.getLong("repeatTime") - existRepeatJsonObject.getLong("repeatTime");
if(interval < commonNoRepeat.interval()) {
if (interval < commonNoRepeat.interval()) {
long secondsParam = (commonNoRepeat.interval() - interval) / 1000;
if(secondsParam > 0) {
if (secondsParam > 0) {
throw new CommonException("请求过于频繁,请" + CommonTimeFormatUtil.formatSeconds(secondsParam) + "后再试");
}
}
@ -545,7 +547,7 @@ public class GlobalConfigure implements WebMvcConfigurer {
return "dm";
} else if (url.contains("jdbc:kingbase")) {
return "kingbase";
} else {
} else {
return "mysql";
}
} finally {
@ -563,19 +565,29 @@ public class GlobalConfigure implements WebMvcConfigurer {
@Component
public static class CustomMetaObjectHandler implements MetaObjectHandler {
/** 删除标志 */
/**
*
*/
private static final String DELETE_FLAG = "deleteFlag";
/** 创建人 */
/**
*
*/
private static final String CREATE_USER = "createUser";
/** 创建时间 */
/**
*
*/
private static final String CREATE_TIME = "createTime";
/** 更新人 */
/**
*
*/
private static final String UPDATE_USER = "updateUser";
/** 更新时间 */
/**
*
*/
private static final String UPDATE_TIME = "updateTime";
@Override
@ -586,21 +598,26 @@ public class GlobalConfigure implements WebMvcConfigurer {
if (ObjectUtil.isNull(deleteFlag)) {
setFieldValByName(DELETE_FLAG, EnumUtil.toString(CommonDeleteFlagEnum.NOT_DELETE), metaObject);
}
} catch (ReflectionException ignored) { }
} catch (ReflectionException ignored) {
}
try {
//为空则设置createUser
Object createUser = metaObject.getValue(CREATE_USER);
if (ObjectUtil.isNull(createUser)) {
setFieldValByName(CREATE_USER, this.getUserId(), metaObject);
}
} catch (ReflectionException ignored) { }
} catch (ReflectionException ignored) {
}
try {
//为空则设置createTime
Object createTime = metaObject.getValue(CREATE_TIME);
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
@ -608,11 +625,15 @@ public class GlobalConfigure implements WebMvcConfigurer {
try {
//设置updateUser
setFieldValByName(UPDATE_USER, this.getUserId(), metaObject);
} catch (ReflectionException ignored) { }
} catch (ReflectionException ignored) {
}
try {
//设置updateTime
setFieldValByName(UPDATE_TIME, DateTime.now(), metaObject);
} catch (ReflectionException ignored) { }
LocalDateTime dt = LocalDateTime.now();
String now = dt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
setFieldValByName(UPDATE_TIME, now, metaObject);
} catch (ReflectionException ignored) {
}
}
/**