3.8 KiB
3.8 KiB
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);
}
}