• springboot+jpa分页(Pageable+Page)




    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

  • 相关阅读:
    Live Writer配置
    protobufnet 学习手记
    好的Sql语句也能提高效率(二)
    关于CodeSmith的输出问题
    [Scrum]12.29
    [scrum] 1.4
    分享 关于c#注释的规范
    [Scrum] 1.3
    分享:将XML(VS提取注释时生成)转换为Chm的一个方法
    【Scrum】2010.12.27
  • 原文地址:https://www.cnblogs.com/zt007/p/13944085.html
Copyright © 2020-2023  润新知