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
