黑马点评商铺分页查询异常排查与修复
在开发过程中遇到了一个棘手的前端交互问题。页面中选择美食类别后往下翻,无法自动滚动分页查询所有的美食店铺数据,前端滚动没反应。

还有个比较诡异的点:前端点击'距离'排序后,滚动查询第一页时数据被查询了两次,后续翻页则正常。如下图所示。
排查许久后,定位到根源在于后端分页配置的大小设定。我的后端代码如下:
Controller 层
/**
* 根据商铺类型分页查询商铺信息
* @param typeId 商铺类型
* @param current 页码
* @param x 经度
* @param y 纬度
* @return 商铺列表
*/
@GetMapping("/of/type")
public Result queryShopByType(
@RequestParam("typeId") Integer typeId,
@RequestParam(value = "current", defaultValue = "1") Integer current,
@RequestParam("x") Double x,
@RequestParam("y") Double y
) {
// 调用 Service 层方法(修正方法名驼峰规范)
return shopService.queryShopByType(typeId, current, x, y);
}
ServiceImpl 层
@Override
public Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {
if (x == null || y == null) {
// 直接返回对应类型的店铺就行了
Page<Shop> page = query()
.eq("type_id", typeId)
.page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE));
return Result.ok(page.getRecords());
}
int from (current - ) * SystemConstants.DEFAULT_PAGE_SIZE;
current * SystemConstants.DEFAULT_PAGE_SIZE;
+ typeId;
(, Metrics.METERS);
GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = stringRedisTemplate.opsForGeo()
.search(key,
GeoReference.fromCoordinate( (x, y)),
distance,
RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
.sortAscending()
.limit(end)
.includeDistance()
.includeCoordinates()
);
(geoResults == || geoResults.getContent().isEmpty()) {
Result.ok(Collections.emptyList());
}
List<GeoResult<RedisGeoCommands.GeoLocation<String>>> pageGeoList = geoResults.getContent().stream()
.skip(from)
.limit(SystemConstants.DEFAULT_PAGE_SIZE)
.collect(Collectors.toList());
List<Long> shopIds = pageGeoList.stream()
.map(geoResult -> Long.valueOf(geoResult.getContent().getName()))
.collect(Collectors.toList());
(shopIds.isEmpty()) {
Result.ok(Collections.emptyList());
}
StringUtil.join(shopIds, );
List<Shop> shopList = query().in(, shopIds).last(( + shopIdstr +


