learning_record_doc/java/踩坑记录/MybatisPlus.md
2022-10-21 17:02:22 +08:00

3.8 KiB
Raw Blame History

Mybatis-Plus

LambdaUpdateWapper 配合泛型使用报错com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not find lambda cache for this property [xxx] of entity [com.xxx.si.fss.ssf.ass.entity.xxx]

public interface CloseV1Service<T extends CloseStatefulEntity> {
    
    BaseMapper<T> getBaseMapper();

    default void action(CloseOperateVO operateVO){
        if (NullUtils.isEmpty(operateVO.getId())){
            throw new MyException(RestResponse.MSG.PARAM_VALUE_ERROR).setMsg("未发现指定操作对象id");
        }
        T closeEntity = getBaseMapper().selectById(operateVO.getId());
        if (closeEntity==null){
            throw new MyException(RestResponse.MSG.NO_DATA_EXISTS);
        }

        closeEntity.closeable(operateVO.getCloseAction());

        LambdaUpdateWrapper<T> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper
            .eq(T::getId,operateVO.getId()) //此处抛出异常
            .set(T::getCloseStatus, operateVO.getCloseAction().getStepState());

        getBaseMapper().update(null,lambdaUpdateWrapper);

    }
}

原因LambdaUpdateWrapper没有指定实体类类型

解决方法为LambdaUpdateWrapper对象指定实体类类型

@Validated
public interface CloseV1Service<T extends CloseStatefulEntity> {
    Class<T> getTClass();

    BaseMapper<T> getBaseMapper();

    default void action(@NotNull(message = "操作VO对象不能为空") @Valid CloseOperateVO operateVO){
        if (NullUtils.isEmpty(operateVO.getId())){
            throw new MyException(RestResponse.MSG.PARAM_VALUE_ERROR).setMsg("未发现指定操作对象id");
        }
        T closeEntity = getBaseMapper().selectById(operateVO.getId());
        if (closeEntity==null){
            throw new MyException(RestResponse.MSG.NO_DATA_EXISTS);
        }

        closeEntity.closeable(operateVO.getCloseAction());
		//Wrappers.lambdaUpdate(getTClass()); 创建LambdaUpdate时指定实体类
        LambdaUpdateWrapper<T> lambdaUpdateWrapper = Wrappers.lambdaUpdate(getTClass());
        //或者lambdaUpdateWrapper.setEntityClass(getTClass());
        lambdaUpdateWrapper.eq(T::getId,operateVO.getId()).set(T::getCloseStatus, operateVO.getCloseAction().getStepState());

        getBaseMapper().update(null,lambdaUpdateWrapper);

    }
}

LambdaUpdateWapper 配合泛型使用出现错误java.lang.BootstrapMethodError: call site initialization exception

public interface CloseV1Service<T extends CloseStatefulEntity & CloseV1able> {
    Class<T> getTClass();

    BaseMapper<T> getBaseMapper();

    default void action(@NotNull(message = "操作VO对象不能为空") @Valid CloseOperateVO operateVO){
        if (NullUtils.isEmpty(operateVO.getId())){
            throw new MyException(RestResponse.MSG.PARAM_VALUE_ERROR).setMsg("未发现指定操作对象id");
        }
        T closeEntity = getBaseMapper().selectById(operateVO.getId());
        if (closeEntity==null){
            throw new MyException(RestResponse.MSG.NO_DATA_EXISTS);
        }

        closeEntity.closeable(operateVO.getCloseAction());

        LambdaUpdateWrapper<T> lambdaUpdateWrapper = Wrappers.lambdaUpdate(getTClass());

        lambdaUpdateWrapper
            .eq(T::getId,operateVO.getId()) //此处抛出异常 因为CloseStatefulEntity存在getId()方法 而CloseV1able不存在
            .set(T::getCloseStatus, operateVO.getCloseAction().getStepState());

        getBaseMapper().update(null,lambdaUpdateWrapper);

    }
}

原因LambdaUpdateWrapper设置的泛型是因为有两个上界LambdaUpdateWrapper使用方法引用时泛型的方法这个方法只在泛型一个上界类中存在则抛出java.lang.BootstrapMethodError: call site initialization exception异常

解决办法:在两个上界中都添加用到的方法或者只设置一个上界