| | |
| | | import org.apache.ibatis.reflection.MetaObject; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.time.OffsetDateTime; |
| | | import java.time.LocalDateTime; |
| | | |
| | | @Component |
| | | public class MyMetaObjectHandler implements MetaObjectHandler { |
| | | |
| | | @Override |
| | | public void insertFill(MetaObject metaObject) { |
| | | // 判断字段是否存在,存在才填充 |
| | | if (metaObject.hasSetter("createBy")) { |
| | | Long userId; |
| | | try { |
| | | if (SecurityUtils.getLoginUser() != null) { |
| | | userId = SecurityUtils.getLoginUser().getUserId(); |
| | | this.strictInsertFill(metaObject, "createBy", String.class, userId.toString()); |
| | | public void insertFill(MetaObject meta) { |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | Long uid = SecurityUtils.getLoginUser().getUserId(); |
| | | if (uid != null) { |
| | | strictInsertFill(meta, "createBy", String.class, uid.toString()); |
| | | strictInsertFill(meta, "updateBy", String.class, uid.toString()); |
| | | } |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | |
| | | if (metaObject.hasSetter("createTime")) { |
| | | this.strictInsertFill(metaObject, "createTime", OffsetDateTime.class, OffsetDateTime.now()); |
| | | } |
| | | |
| | | if (metaObject.hasSetter("updateTime")) { |
| | | this.strictUpdateFill(metaObject, "updateTime", OffsetDateTime.class, OffsetDateTime.now()); |
| | | } |
| | | strictInsertFill(meta, "createTime", LocalDateTime.class, now); |
| | | strictInsertFill(meta, "updateTime", LocalDateTime.class, now); |
| | | } |
| | | |
| | | @Override |
| | | public void updateFill(MetaObject metaObject) { |
| | | // 判断字段是否存在,存在才填充 |
| | | if (metaObject.hasSetter("updateBy")) { |
| | | Long userId = SecurityUtils.getLoginUser().getUserId(); |
| | | this.strictUpdateFill(metaObject, "updateBy", String.class, userId.toString()); |
| | | public void updateFill(MetaObject meta) { |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | Long uid = SecurityUtils.getLoginUser().getUserId(); |
| | | if (uid != null) { |
| | | strictUpdateFill(meta, "updateBy", String.class, uid.toString()); |
| | | } |
| | | // 强制覆盖 updateTime |
| | | setFieldValByName("updateTime", now, meta); |
| | | } |
| | | |
| | | if (metaObject.hasSetter("updateTime")) { |
| | | this.strictUpdateFill(metaObject, "updateTime", OffsetDateTime.class, OffsetDateTime.now()); |
| | | } |
| | | } |
| | | } |