Pageable+Page实现分页无需配置,也不需要加入jar包(maven依赖)
1 package com.gxuwz.late.controller; 2 3 import com.gxuwz.late.bean.Record; 4 import com.gxuwz.late.repository.RecordRepository; 5 import org.slf4j.Logger; 6 import org.slf4j.LoggerFactory; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.data.domain.Page; 9 import org.springframework.data.domain.PageRequest; 10 import org.springframework.data.domain.Pageable; 11 import org.springframework.data.domain.Sort; 12 import org.springframework.stereotype.Controller; 13 import org.springframework.ui.Model; 14 import org.springframework.web.bind.annotation.RequestMapping; 15 16 import javax.servlet.http.HttpServletResponse; 17 18 @Controller 19 @RequestMapping("/manager") 20 public class ManagerController { 21 static Logger logger = LoggerFactory.getLogger(ManagerController.class); 22 23 @Autowired 24 RecordRepository recordRepository; 25 26 @RequestMapping("/list") 27 public String list(HttpServletResponse response, Model model, Integer pageNum){ 28 29 if (pageNum == null){ 30 pageNum = 1; 31 } // 排序方式,这里是以“recordNo”为标准进行降序 32 Sort sort = new Sort(Sort.Direction.DESC, "recordNo"); // 这里的"recordNo"是实体类的主键,记住一定要是实体类的属性,而不能是数据库的字段 33 Pageable pageable = new PageRequest(pageNum - 1, 6, sort); // (当前页, 每页记录数, 排序方式) 34 Page<Record> list = recordRepository.findAll(pageable); 35 36 logger.info("pageNum==" + pageNum); 37 38 model.addAttribute("pageInfo", list); 39 40 response.addHeader("x-frame-options","SAMEORIGIN"); // 允许iframe 41 return "record_list"; 42 } 43 }
转自:
https://www.cnblogs.com/chuangqi/p/11261482.html