• JPA


    前言

    这里介绍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 -
    梦想是咸鱼
    关注一下吧
    以上为本篇文章的主要内容,希望大家多提意见,如果喜欢记得点个推荐哦
    作者:Maggieq8324
    本文版权归作者和博客园共有,欢迎转载,转载时保留原作者和文章地址即可。
  • 相关阅读:
    Java基础系列——IO流
    如何为网站添加 CSS 暗模式(转载)
    详解 UWP (通用 Windows 平台) 中的两种 HttpClient API
    微软微服务架构 eShopOnContainers
    web开发中使用html/css全屏铺开显示
    web开发中的Cookie
    WPF依赖属性Binding实现
    SQL Server2014安装流程及注意事项
    .Net配置文件读取及修改方法封装(未加密)
    WCF开发优秀博客园推荐
  • 原文地址:https://www.cnblogs.com/maggieq8324/p/15145593.html
Copyright © 2020-2023  润新知