zoukankan      html  css  js  c++  java
  • springboot 表格创建人,创建时间,修改人,修改时间等字段自动填充实现代码

    代码一

    import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
    import org.apache.ibatis.reflection.MetaObject;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    @Component
    public class MetaHandler implements MetaObjectHandler {
    
        private static final String CREATE_TIME = "createTime";
        private static final String CREATOR_ID = "creatorId";
        private static final String UPDATE_TIME = "updateTime";
        private static final String UPDATE_ID = "updateId";
    
        /**
         * 新增数据执行
         * @param metaObject
         */
        @Override
        public void insertFill(MetaObject metaObject) {
            Long userId = 1L;// SecurityUtils.getUserId();
    
    //        this.setFieldValByName("createTime", new Date(), metaObject);
    //        this.setFieldValByName("creatorId", userId, metaObject);
    //       this.setFieldValByName("updateTime", new Date(), metaObject);
    //       this.setFieldValByName("updateId", userId, metaObject);
    
            if (metaObject.hasGetter(CREATOR_ID) && metaObject.getValue(CREATOR_ID) == null) {
                this.setFieldValByName(CREATOR_ID, userId, metaObject);
            }
            if (metaObject.hasGetter(CREATE_TIME) && metaObject.getValue(CREATE_TIME) == null) {
                this.setFieldValByName(CREATE_TIME, new Date(), metaObject);
            }
    
        }
    
        /**
         * 更新数据执行
         * @param metaObject
         */
        @Override
        public void updateFill(MetaObject metaObject) {
            Long userId = 10L;// SecurityUtils.getUserId();
    
    
            if (metaObject.hasGetter(UPDATE_TIME) && metaObject.getValue(UPDATE_TIME) == null) {
                this.setFieldValByName(UPDATE_TIME, new Date(), metaObject);
            }
            if (metaObject.hasGetter(UPDATE_ID) && metaObject.getValue(UPDATE_ID) == null) {
                this.setFieldValByName(UPDATE_ID, userId, metaObject);
            }
        }
    }

    代码二

    import com.baomidou.mybatisplus.core.config.GlobalConfig;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MyBatisPlusConfig {
        /**
         * 自动填充功能
         * @return
         */
        @Bean
        public GlobalConfig globalConfig() {
            GlobalConfig globalConfig = new GlobalConfig();
            globalConfig.setMetaObjectHandler(new MetaHandler());
            return globalConfig;
        }
    }

    代码三

    import com.baomidou.mybatisplus.annotation.FieldFill;
    import com.baomidou.mybatisplus.annotation.TableField;
    import com.fasterxml.jackson.annotation.JsonFormat;
    import lombok.Data;
    import org.apache.commons.lang3.builder.ToStringBuilder;
    import org.apache.commons.lang3.builder.ToStringStyle;
    
    import java.io.Serializable;
    import java.util.Date;
    
    
    @Data
    public class BaseEntity  implements Serializable, Cloneable {
    
        private static final long serialVersionUID = 1L;
    
    
        @TableField(value = "creator_id", fill = FieldFill.INSERT) // 新增执行
        private Long creatorId;
    
        @TableField(value = "create_time", fill = FieldFill.INSERT)
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date createTime;
    
        @TableField(value = "update_id", fill = FieldFill.UPDATE) // 新增和更新执行
        private Long updateId;
    
        @TableField(value = "update_time", fill = FieldFill.UPDATE)
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date updateTime;
    
        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
        }
    }

    代码四 表中没有这些字段,过滤填充

    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    import com.xxxxx.BaseEntity;
    import lombok.Data;
    
    import java.util.Date;
    
    
    
    @Data
    @TableName("xxxdemo")
    public class xxxxdemo  extends BaseEntity {
        private static final long serialVersionUID = 1L;
    
        /** $column.columnComment */
        @TableId(type = IdType.ASSIGN_ID)
        private Long Id;
    
        /** $column.columnComment */
        private Long xxxdeabc;
    
        /***/
        private Integer abctestType;
    
        /** 内容(json) */
        private String content;
    
        /** 描述 */
        private String description;
    
    
        /** 以下字段,如果表中没有就添加,有则忽略**/
        /***
         *表中不存在的字段 修改时间
         */
        private transient  Date updateTime;
        /***
         *表中不存在的字段 修改人
         */
        private transient   Long updateId;
    
        /***
         *表中不存在的字段  创建时间
         */
        private transient  Date createTime;
        /***
         *表中不存在的字段 创建人
         */
        private transient   Long creatorId;
    }

    https://blog.csdn.net/weixin_34072159/article/details/92037114

    https://gitee.com/baomidou/mybatisplus-spring-boot/issues/IKFLR

  • 相关阅读:
    UltraEdit语法高亮解决办法
    tcpdump命令格式及使用
    VS tricks
    git ready
    [FirefoxPlugin]Print pages to Pdf
    Searching and Navigating Code in VS 2010 (VS 2010 and .NET 4.0 Series)
    How to avoid StepInto unnecessary code area?
    Search and Navigation Tips/Tricks with Visual Studio
    squashing commits with rebase
    【原创】钻石继承与虚继承
  • 原文地址:https://www.cnblogs.com/xiaohuasan/p/14894828.html
Copyright © 2011-2022 走看看