From 0e5802adb8b30d57b65d6799eb8eb93984e539b7 Mon Sep 17 00:00:00 2001 From: hezhouqun Date: Mon, 8 Mar 2021 11:16:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=A1=AB=E5=85=85=EF=BC=8C=E5=A6=82=E6=9E=9C?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E6=98=BE=E5=BC=8F=E7=9A=84=E5=AF=B9=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E5=AD=97=E6=AE=B5=E8=BF=9B=E8=A1=8C=E8=B5=8B=E5=80=BC?= =?UTF-8?q?=EF=BC=8C=E5=88=99=E4=BC=9A=E8=87=AA=E5=8A=A8=E5=A1=AB=E5=85=85?= =?UTF-8?q?=E5=8F=82=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; /** * 是否删除 */ From ce7947b162680def57a51a5f24becd48b4cd02b0 Mon Sep 17 00:00:00 2001 From: hezhouqun Date: Mon, 8 Mar 2021 19:11:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B0=86=20DefaultParamHandler=20=E4=BB=8E?= =?UTF-8?q?=20config=20=E5=8C=85=E7=A7=BB=E5=8A=A8=E5=88=B0=20core/handle?= =?UTF-8?q?=20=E5=8C=85=E4=B8=8B=20=E5=B0=86=20DefaultParamHandler=20?= =?UTF-8?q?=E6=94=B9=E5=90=8D=E4=B8=BA=20DefaultDBFieldHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handle/DefaultDBFieldHandler.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/main/java/cn/iocoder/dashboard/framework/mybatis/{config/DefaultParamHandler.java => core/handle/DefaultDBFieldHandler.java} (95%) diff --git a/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/DefaultParamHandler.java b/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/handle/DefaultDBFieldHandler.java similarity index 95% rename from src/main/java/cn/iocoder/dashboard/framework/mybatis/config/DefaultParamHandler.java rename to src/main/java/cn/iocoder/dashboard/framework/mybatis/core/handle/DefaultDBFieldHandler.java index f94100fbc..1651ecc47 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/DefaultParamHandler.java +++ b/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/handle/DefaultDBFieldHandler.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.framework.mybatis.config; +package cn.iocoder.dashboard.framework.mybatis.core.handle; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.framework.security.core.LoginUser; @@ -18,7 +18,7 @@ import java.util.Objects; * @author hexiaowu */ @Component -public class DefaultParamHandler implements MetaObjectHandler { +public class DefaultDBFieldHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) {