优化一下代码 public IPage<FavoriteResp> favoritePage(IPage<FavoriteResp> page, String name, BladeUser bladeUser) { List<FavoriteResp> favoriteRespList; if (!AuthUtil.isAdministrator()) { favoriteRespList = baseMapper.favoritePage(page, name, bladeUser.getUserId()); } else { favoriteRespList = baseMapper.favoritePage(page, name, null); } List<SharedInfo> sharedInfoList = getSharedInfoList(); for (FavoriteResp favoriteResp : favoriteRespList) { List<SharedInfo> sharedInfos = sharedInfoList.stream().filter(sharedInfo -> sharedInfo.getResourcesId().equals(favoriteResp.getResourcesId())).collect(Collectors.toList()); favoriteResp.setResourcesTypeCn(DictCache.getValue(DictEnum.RESOURCES_TYPE, favoriteResp.getResourcesType())); List<String> memberImages = sharedInfos.stream().map(sharedInfo -> UserCache.getUser(sharedInfo.getSharedTo()).getAvatar()) .distinct().collect(Collectors.toList()); favoriteResp.setMemebersImage(memberImages); } return page.setRecords(favoriteRespList); }
时间: 2024-03-16 17:42:22 浏览: 59
mybatisplus分页查询(附代码)
可以使用左连接的方式一次性查询出所有需要的数据,避免多次查询数据库,从而提高查询效率。以下是优化后的代码:
```java
public IPage<FavoriteResp> favoritePage(IPage<FavoriteResp> page, String name, BladeUser bladeUser) {
QueryWrapper<FavoriteResp> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("f.name", name)
.eq(bladeUser != null, "f.user_id", bladeUser.getUserId())
.orderByDesc("f.create_time");
IPage<FavoriteResp> favoritePage = baseMapper.selectFavoritePage(page, queryWrapper);
List<FavoriteResp> favoriteList = favoritePage.getRecords();
if (CollUtil.isNotEmpty(favoriteList)) {
List<Long> resourcesIds = favoriteList.stream().map(FavoriteResp::getResourcesId).distinct().collect(Collectors.toList());
List<SharedInfo> sharedInfoList = getSharedInfoList(resourcesIds);
Map<Long, List<SharedInfo>> sharedInfoMap = sharedInfoList.stream().collect(Collectors.groupingBy(SharedInfo::getResourcesId));
for (FavoriteResp favoriteResp : favoriteList) {
favoriteResp.setResourcesTypeCn(DictCache.getValue(DictEnum.RESOURCES_TYPE, favoriteResp.getResourcesType()));
List<SharedInfo> sharedInfoSubList = sharedInfoMap.get(favoriteResp.getResourcesId());
if (CollUtil.isNotEmpty(sharedInfoSubList)) {
List<String> memberImages = sharedInfoSubList.stream()
.map(sharedInfo -> UserCache.getUser(sharedInfo.getSharedTo()).getAvatar())
.distinct().collect(Collectors.toList());
favoriteResp.setMemebersImage(memberImages);
}
}
}
return favoritePage;
}
private List<SharedInfo> getSharedInfoList(List<Long> resourcesIds) {
QueryWrapper<SharedInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.in("resources_id", resourcesIds);
return sharedInfoService.list(queryWrapper);
}
```
上述代码中,首先使用QueryWrapper构建查询条件,并调用baseMapper.selectFavoritePage查询出需要的FavoriteResp数据。然后,使用stream的方式将FavoriteResp集合中所有的resourcesId提取出来,去重后构建一个resourcesIds列表。接着,调用getSharedInfoList方法一次性查询出resourcesIds列表中所有的SharedInfo集合。使用stream的方式将SharedInfo集合按resourcesId分组,构建一个sharedInfoMap映射表。最后,遍历FavoriteResp集合,从sharedInfoMap中取出对应的SharedInfo集合,并使用stream的方式将数据处理后放回FavoriteResp中。最后返回IPage<FavoriteResp>对象。
阅读全文