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