From 7d26c1d00a2a041008b21dea1b3a140af170ebd7 Mon Sep 17 00:00:00 2001 From: hexiaowu Date: Mon, 8 Mar 2021 11:16:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=A1=AB=E5=85=85=EF=BC=8C=E5=A6=82=E6=9E=9C=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E6=98=BE=E5=BC=8F=E7=9A=84=E5=AF=B9=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E8=BF=9B=E8=A1=8C=E8=B5=8B=E5=80=BC=EF=BC=8C?= =?UTF-8?q?=E5=88=99=E4=BC=9A=E8=87=AA=E5=8A=A8=E5=A1=AB=E5=85=85=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/config/DefaultParamHandler.java | 65 +++++++++++++++++++ .../mybatis/core/dataobject/BaseDO.java | 8 +-- 2 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/mybatis/config/DefaultParamHandler.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/DefaultParamHandler.java b/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/DefaultParamHandler.java new file mode 100644 index 000000000..f94100fbc --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/DefaultParamHandler.java @@ -0,0 +1,65 @@ +package cn.iocoder.dashboard.framework.mybatis.config; + +import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; +import cn.iocoder.dashboard.framework.security.core.LoginUser; +import cn.iocoder.dashboard.framework.security.core.util.SecurityFrameworkUtils; +import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; +import org.apache.ibatis.reflection.MetaObject; +import org.springframework.stereotype.Component; + +import java.util.Date; +import java.util.Objects; + +/** + * 通用参数填充实现类 + * + * 如果没有显式的对通用参数进行赋值,这里会对通用参数进行填充、赋值 + * + * @author hexiaowu + */ +@Component +public class DefaultParamHandler implements MetaObjectHandler { + + @Override + public void insertFill(MetaObject metaObject) { + if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseDO) { + LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); + BaseDO baseDO = (BaseDO) metaObject.getOriginalObject(); + Date current = new Date(); + + // 创建时间为空,则以当前时间为插入时间 + if (Objects.isNull(baseDO.getCreateTime())) { + baseDO.setCreateTime(current); + } + // 更新时间为空,则以当前时间为更新时间 + if (Objects.isNull(baseDO.getUpdateTime())) { + baseDO.setUpdateTime(current); + } + // 当前登录用户不为空,创建人为空,则当前登录用户为创建人 + if (Objects.nonNull(loginUser) && Objects.isNull(baseDO.getCreator())) { + baseDO.setCreator(loginUser.getId().toString()); + } + // 当前登录用户不为空,更新人为空,则当前登录用户为更新人 + if (Objects.nonNull(loginUser) && Objects.isNull(baseDO.getUpdater())) { + baseDO.setUpdater(loginUser.getId().toString()); + } + } + } + + @Override + public void updateFill(MetaObject metaObject) { + Object modifyTime = getFieldValByName("updateTime", metaObject); + Object modifier = getFieldValByName("updater", metaObject); + // 获取登录用户信息 + LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); + + // 更新时间为空,则以当前时间为更新时间 + if (Objects.isNull(modifyTime)) { + setFieldValByName("updateTime", new Date(), metaObject); + } + // 当前登录用户不为空,更新人为空,则当前登录用户为更新人 + if (Objects.nonNull(loginUser) && Objects.isNull(modifier)) { + setFieldValByName("updater", loginUser.getId(), metaObject); + } + } +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/dataobject/BaseDO.java b/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/dataobject/BaseDO.java index 7f6c76150..df9ddb22e 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/dataobject/BaseDO.java +++ b/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/dataobject/BaseDO.java @@ -21,13 +21,13 @@ public class BaseDO implements Serializable { */ private Date updateTime; /** - * 创建者 TODO 芋艿:迁移成编号 + * 创建者 */ - private String createBy; + private String creator; /** - * 更新者 TODO 芋艿:迁移成编号 + * 更新者 */ - private String updateBy; + private String updater; /** * 是否删除 */