需求背景:多个表的数据查询结果集list,最后需要合并给前台页面进行分页展示
技术栈:google的guava的partition方法
pom依赖:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version.guava>21.0</version.guava> </dependency>
封装业务方法:
public static List<List> partitionList(List list,Integer pageSize){ List<List> pageList = Lists.partition(list, pageSize); return pageList; }
业务调用(伪代码):
private List<List> getPageList(requestDto){
//list1
List<ItemInfoDto> distinctSyncPaisItems = allSyncItems.stream().distinct().collect(Collectors.toList());
//list2
List<ItemInfoDto> distinctLimitResult = allLimitItems.stream().distinct().collect(Collectors.toList());
List<ItemInfoDto> allItemInfoList=new ArrayList<>();
//收集所有list然后调用上面封装好的分页方法
allItemInfoList.addAll(distinctSyncPaisItems);
allItemInfoList.addAll(distinctLimitResult);
List<List> lists = PageUtil.partitionList(allItemInfoList,requestDto.getPageSize());
return lists;
}
前端展示(伪代码):
private responseDto getItemListModuleInfo(){
List<List> list=getPageList(requestDto);
responseDto.setPages(list.size());
responseDto.setItems(list.get(requestDto.getPageNum()-1));
}