当前使用版本(必填,否则不予处理)
3.4.3.1
该问题是如何引起的?(确定最新版也有问题再提!!!)
原有代码写法:
api包定义的接口:
public interface ApplicationService {
IPage<ApplicationDto> page(Integer status, Long developerId, Integer offset, Integer count);
}
这里使用了IPage作为返回类型。
实现方法:
@Override
public IPage<ApplicationDto> page(Integer status, Long developerId, Integer offset, Integer count) {
Page<Application> page = Page.of(offset, count);
List<Long> appList = new LambdaQueryChainWrapper<>(appDeveloperRelationRepository)
.eq(Objects.nonNull(developerId), AppDeveloperRelation::getDeveloperId, developerId)
.list()
.stream()
.map(AppDeveloperRelation::getAppId)
.collect(Collectors.toList());
if (appList.isEmpty()) {
return new PageDTO<>(Long.MIN_VALUE, Long.MAX_VALUE);
}
return new LambdaQueryChainWrapper<>(applicationRepository)
.in(Application::getId, appList)
.eq(Application::getStatus, status)
.orderByDesc(Application::getUpdateTime)
.page(page)
.convert(entity -> applicationObjectMapper.poToDto(entity));
}
由于使用convert方法将po转dto,这里只能返回IPage。
最近引入的PageDto没有提供IPage转为PageDto的快速方法(当然也不应该有父类转子类的方法)。同时提供的静态构造方法也不能将一个IPage的查询结果转为PageDto。只能通过new一个PageDto手动set各个属性。
这样看来使用PageDto并不方便。
还是说的我理解错了意思,传输分页结果的最佳实践应该是什么样的?
Comment From: qmdx
个人建议在 service 内部使用默认 page 就可以了, controller 层可以定义个 Result 之类的对象,返回 page 信息及 code 类型的,例如:
public class R<T> implements Serializable {
/**
* 业务错误码
*/
private long code;
/**
* 分页总记录数
*/
private Long count;
/**
* 结果集
*/
private T data;
/**
* 描述
*/
private String message;
}