IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> JPA - 使用Page进行分页 -> 正文阅读

[Java知识库]JPA - 使用Page进行分页

前言

这里介绍JPA中使用Page进行分页及分页的封装,前端传递的参数类型如下形式:

?start=10&count=5

结果展示如下:
在这里插入图片描述

环境

SpringBoot 2.5.3 + JPA 2.5.3

具体实现

  • Product 控制器
import com.coisini.springbootlearn.model.Product;
import com.coisini.springbootlearn.model.common.PageCounter;
import com.coisini.springbootlearn.model.common.Paging;
import com.coisini.springbootlearn.repository.ProductRepository;
import com.coisini.springbootlearn.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductRepository productRepository;

    /**
     * 获取商品分页数据
     * @return
     */
    @GetMapping("/getPageProduct")
    public Paging<Product> getPageSpu(@RequestParam(defaultValue = "0") Integer start,
                                         @RequestParam(defaultValue = "2") Integer count) {
        // 分页参数构造
        PageCounter pageCounter = CommonUtil.convertToPageParameter(start, count);

        // 构造分页查询信息
        Pageable page = PageRequest.of(pageCounter.getPage(), pageCounter.getCount(), Sort.by("id").descending());

        // 查询分页结果
        Page<Product> pageResult = productRepository.findAll(page);

        // 返回分页构造对象
        return new Paging<>(pageResult);
    }

}
  • Product 仓储
/**
 * @Description Product 仓储
 * @author coisini
 * @date Aug 14, 2021
 * @Version 1.0
 */
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

}
  • Product 模型
/**
 * @Description Product 模型
 * @author coisini
 * @date Aug 15, 2021
 * @Version 1.0
 */
@Entity
@Getter
@Setter
public class Product {

    @Id
    private int id;
    private String title;

}
  • 公用工具类
import com.coisini.springbootlearn.model.common.PageCounter;

/**
 * @Description 公用工具类
 * @author coisini
 * @Version 1.0
 */
public class CommonUtil {

    /**
     * page参数转换
     * @param start
     * @param count
     */
    public static PageCounter convertToPageParameter(Integer start, Integer count) {

        int pageNum = start / count;

        return PageCounter.builder()
                          .page(pageNum)
                          .count(count)
                          .build();
    }

}
  • 分页返回对象
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.domain.Page;
import java.util.List;

/**
 * @Description 分页返回对象
 * @author coisini
 * @date Aug 15, 2021
 * @Version 1.0
 */
@Getter
@Setter
@NoArgsConstructor
public class Paging<T> {

    /**
     * 总数量
     */
    private Long total;

    /**
     * 当前请求数量
     */
    private Integer count;

    /**
     * 当前页码
     */
    private Integer page;

    /**
     * 总页码
     */
    private Integer totalPage;

    /**
     * 数据
     */
    private List<T> data;

    /**
     * 构造函数
     */
    public Paging(Page<T> pageData) {
        this.initPageParameters(pageData);
    }

    /**
     * 构造分页参数
     * @param pageData
     */
    void initPageParameters(Page<T> pageData) {
        this.total = pageData.getTotalElements();
        this.count = pageData.getSize();
        this.page = pageData.getNumber();
        this.totalPage = pageData.getTotalPages();
        this.data = pageData.getContent();
    }

}
  • 接口访问
    在这里插入图片描述
    在这里插入图片描述

- End -
梦想是咸鱼
关注一下吧
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-08-17 01:24:15  更:2021-08-17 01:25:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 22:23:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码