BUG:Mybatisplus实现MetaObjectHandler接口自动更新或者添加时,insertFill()和 updateFill()失效的问题

BUG:Mybatisplus实现MetaObjectHandler接口自动更新或者添加时,insertFill()和 updateFill()失效的问题!!

​ 概述:Mybatisplus中MetaObjectHandler中有是监控添加或者修改的方法,可以在系统中根据修改,或者添加时做业务操作,比如要在更新时清除特定的缓存,如果在更新的方法中使用条件构造器就会造成自动更新的方法失效,从而不会进入这个方法;解决:不用条件构造器,使用实体类的方式去更新

注意:Mybatisplus会忽略空值的判断,如果要存入空值的话,可以在DB的实体类上打上注解**@TableField(value = “xxxx”,updateStrategy = FieldStrategy.IGNORED)**

@Component
@Slf4j
public class DBMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {


        this.strictInsertFill(metaObject, "createDate", () -> new Date(System.currentTimeMillis()), Date.class);
        this.strictInsertFill(metaObject, "updateDate", () -> new Date(System.currentTimeMillis()), Date.class);
        this.strictInsertFill(metaObject, "updateBy", () -> UserUtils.getUser().getId(), String.class);
        this.strictInsertFill(metaObject, "createBy", () -> UserUtils.getUser().getId(), String.class);
        this.strictInsertFill(metaObject, "delFlag", () -> "0", String.class);

    }

    @Override
    public void updateFill(MetaObject metaObject) {

        log.debug("start update fill...");
        metaObject.setValue("updateDate", null);
        this.fillStrategy(metaObject, "updateDate", new Date());
        this.strictUpdateFill(metaObject, "updateBy", String.class, UserUtils.getUser().getId());
        //清除配置缓存
        if (metaObject.getOriginalObject() instanceof CompanyEntity || metaObject.getOriginalObject() instanceof LoadPointEntity) {
            log.info("====>清除配置缓存<====");
            if (metaObject.getOriginalObject() instanceof ShhyCompanyEntity){
                CompanyEntity companyEntity =(CompanyEntity) metaObject.getOriginalObject();
                ValveUtils.cleanValveConfig(CompanyEntity.getId(), "");
            }
            if (metaObject.getOriginalObject() instanceof ShhyLoadPointEntity){
                LoadPointEntity loadPointEntity =(LoadPointEntity) metaObject.getOriginalObject();
                ValveUtils.cleanValveConfig(loadPointEntity.getOwnerCompany(), loadPointEntity.getId());
            }
            ValveUtils.cleanAll();
        }
    }
}

做更新操作时,没有执行updateFill(),不用条件构造器,就可以了;

//  	 updateWrapper.set(LoadPointEntity::getShowBook, configurationParam.getBookingInfo());
//        updateWrapper.eq(LoadPointEntity::getId, loadPointEntity.getId());
//        loadPointEntityService.update(updateWrapper);

 loadPointEntity.setShowBook(configurationParam.getBookingInfo());
        shhyLoadPointEntityService.updateById(loadPointEntity);

你可能感兴趣的:(Erro,Log,java-ee,intellij-idea,后端,struts,java)