1,添加自定义序号,翻页累加
int pageNum = 1;
int pageSize = 10;
Page<User> page = new Page<>(pageNum,pageSize);
Page<User> userPage = userService.page(page);
List<User> records = userPage.getRecords();
// 序号,翻页会累加
int i = 1;
if (pageNum > 1) {
i = (pageNum - 1) * pageSize + 1;
}
for (User user : records) {
user.setIdx(i++);
}
return userPage;
2,根据总条数和每页条数计算总页数
int pageSize = 20; //每页记录数量
int totalCount = 100005; //总记录数
int pageCount; //总页数
//方法一:
int pageCount = (totalCount + pageSize - 1) / pageSize; //推荐写法
//方法二:
int pageCount = (totalCount - 1) / pageSize + 1;
//方法三:
int pageCount = (int)Math.Ceiling((double)totalCount / pageSize);
//方法四:
int pageCount = (int)Math.Ceiling((double)totalCount / pageSize);
//方法五:
int pageCount = totalCount%pageSize == 0 ? totalCount/pageSize : totalCount/pageSize + 1;