1,添加自定义序号,翻页累加

  1. int pageNum = 1;
  2. int pageSize = 10;
  3. Page<User> page = new Page<>(pageNum,pageSize);
  4. Page<User> userPage = userService.page(page);
  5. List<User> records = userPage.getRecords();
  6. // 序号,翻页会累加
  7. int i = 1;
  8. if (pageNum > 1) {
  9. i = (pageNum - 1) * pageSize + 1;
  10. }
  11. for (User user : records) {
  12. user.setIdx(i++);
  13. }
  14. return userPage;

2,根据总条数和每页条数计算总页数

  1. int pageSize = 20; //每页记录数量
  2. int totalCount = 100005; //总记录数
  3. int pageCount; //总页数
  4. //方法一:
  5. int pageCount = (totalCount + pageSize - 1) / pageSize; //推荐写法
  6. //方法二:
  7. int pageCount = (totalCount - 1) / pageSize + 1;
  8. //方法三:
  9. int pageCount = (int)Math.Ceiling((double)totalCount / pageSize);
  10. //方法四:
  11. int pageCount = (int)Math.Ceiling((double)totalCount / pageSize);
  12. //方法五:
  13. int pageCount = totalCount%pageSize == 0 ? totalCount/pageSize : totalCount/pageSize + 1;