Browse Source

公共字段自动填充

Aron 6 years ago
parent
commit
343b3384e0

+ 32 - 12
src/main/java/com/ifast/common/base/BaseDO.java

@@ -1,25 +1,45 @@
 package com.ifast.common.base;
 
+import com.baomidou.mybatisplus.annotations.TableField;
 import com.baomidou.mybatisplus.annotations.TableLogic;
 import com.baomidou.mybatisplus.annotations.Version;
+import com.baomidou.mybatisplus.enums.FieldFill;
 import lombok.Data;
 
 import java.io.Serializable;
 import java.util.Date;
 
+/**
+ * <pre>
+ * 业务表基础公共字段
+ *
+ * deleted 由 mybatis-plus.global-config.sql-injector:com.baomidou.mybatisplus.mapper.LogicSqlInjector 自动维护
+ * version 由 MyBatisConfig.optimisticLockerInterceptor 自动维护
+ *
+ * 以下字段由 meta-object-handler: com.ifast.common.mp.MpMetaObjectHandler 自动维护
+ * createAt
+ * createBy
+ * updateAt
+ * updateBy
+ * </pre>
+ * @author Aron
+ */
 @Data
 @SuppressWarnings("serial")
 public class BaseDO implements Serializable {
-	/** 由mybatis-plus.global-config.sql-injector:com.baomidou.mybatisplus.mapper.LogicSqlInjector自动维护 */
-	@TableLogic
-	private Boolean deleted;
-	/** 由MyBatisConfig.optimisticLockerInterceptor自动维护 */
-	@Version
-	private Integer version;
-	/** 由MySQL自动维护 */
-	private Date createAt;
-	private Date updateAt;
-	/** 由LogAspect.logMapper自动维护 */
-	private Long createBy;
-	private Long updateBy;
+
+    @TableLogic
+    private Boolean deleted;
+
+    @Version
+    private Integer version;
+
+    @TableField(fill = FieldFill.INSERT)
+    private Date createAt;
+    @TableField(fill = FieldFill.INSERT)
+    private Long createBy;
+    @TableField(fill = FieldFill.UPDATE)
+    private Date updateAt;
+    @TableField(fill = FieldFill.UPDATE)
+    private Long updateBy;
 }

+ 74 - 0
src/main/java/com/ifast/common/mp/MpMetaObjectHandler.java

@@ -0,0 +1,74 @@
+package com.ifast.common.mp;
+
+import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
+import com.ifast.common.utils.ShiroUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.reflection.MetaObject;
+
+import java.util.Date;
+
+/**
+ * <pre>
+ *
+ * </pre>
+ * <small> 2019-06-06 16:26 | Aron</small>
+ */
+@Slf4j
+public class MpMetaObjectHandler extends MetaObjectHandler {
+
+    private final static String COLUMN_CREATE_AT = "createAt";
+    private final static String COLUMN_CREATE_BY = "createBy";
+    private final static String COLUMN_UPDATE_AT = "updateAt";
+    private final static String COLUMN_UPDATE_BY = "updateBy";
+
+
+    @Override
+    public void insertFill(MetaObject metaObject) {
+
+        Object createTime = getFieldValByName(COLUMN_CREATE_AT, metaObject);
+        Date now = new Date();
+        if (createTime == null) {
+            if(log.isDebugEnabled()){
+                log.debug("set COLUMN_CREATE_AT = " + now);
+            }
+            setFieldValByName(COLUMN_CREATE_AT, now, metaObject);
+        }
+
+        Object createBy = getFieldValByName(COLUMN_CREATE_BY, metaObject);
+        if (createBy == null) {
+            Long userId = getUserId();
+            if(log.isDebugEnabled()){
+                log.debug("set COLUMN_CREATE_BY = " + userId);
+            }
+            setFieldValByName(COLUMN_CREATE_BY, userId, metaObject);
+        }
+
+    }
+
+    @Override
+    public void updateFill(MetaObject metaObject) {
+
+        Object updateTime = getFieldValByName(COLUMN_UPDATE_AT, metaObject);
+        Date now = new Date();
+        if (updateTime == null) {
+            if(log.isDebugEnabled()){
+                log.debug("set COLUMN_UPDATE_AT = " + now);
+            }
+            setFieldValByName(COLUMN_UPDATE_AT, now, metaObject);
+        }
+
+        Object updateBy = getFieldValByName(COLUMN_UPDATE_BY, metaObject);
+        if (updateBy == null) {
+            Long userId = getUserId();
+            if(log.isDebugEnabled()){
+                log.debug("set COLUMN_UPDATE_BY = " + userId);
+            }
+            setFieldValByName(COLUMN_UPDATE_BY, userId, metaObject);
+        }
+
+    }
+
+    private Long getUserId() {
+        return ShiroUtils.getUserId();
+    }
+}