赞
踩
@Slf4j public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { try { if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) { BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject(); Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime()) ? baseEntity.getCreateTime() : new Date(); baseEntity.setCreateTime(current); baseEntity.setUpdateTime(current); Long creatorId = ObjectUtil.isNotNull(baseEntity.getCreatorId()) ? baseEntity.getCreatorId() : StpUtil.getLoginIdAsLong(); // 当前已登录 且 创建人为空 则填充 baseEntity.setCreatorId(creatorId); // 当前已登录 且 更新人为空 则填充 baseEntity.setUpdaterId(creatorId); } } catch (Exception e) { throw new RuntimeException("自动注入异常 => " + e.getMessage()); } } @Override public void updateFill(MetaObject metaObject) { try { if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) { BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject(); Date current = new Date(); // 更新时间填充(不管为不为空) baseEntity.setUpdateTime(current); Long creatorId = StpUtil.getLoginIdAsLong(); // 当前已登录 更新人填充(不管为不为空) if (ObjectUtil.isNotNull(creatorId)) { baseEntity.setUpdaterId(creatorId); } } } catch (Exception e) { throw new RuntimeException("自动注入异常 => " + e.getMessage()); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。