判断一条数据是否是新纪录,以及保存之前执行的方法

在basecontroller实体类中声明一个final isnewrecord=false

对外公开一个方法:getisnewRecord(),默认是返回false,如果对象的id不为空就返回true

public boolean getIsNewRecord() {

        return isNewRecord || StringUtils.isBlank(getId());

    }

/**
     * 插入之前执行方法,需要手动调用
     */
    @Override
    public void preInsert(){
        // 不限制ID为UUID,调用setIsNewRecord()使用自定义ID
        if (!this.isNewRecord){
            setId(IdGen.uuid());
        }
        User user = UserUtils.getUser();
        if (StringUtils.isNotBlank(user.getId())){
            this.updateBy = user;
            this.createBy = user;
        }
        this.updateDate = new Date();
        this.createDate = this.updateDate;
    }

猜你喜欢

转载自blog.csdn.net/weixin_41148727/article/details/84109935