• Springboot 2.3.5使用 pagehelper 实现分页


    参考

    1. SpringBoot - 获取Get请求参数详解(附样例:非空、默认值、数组、对象)
    2. 如何使用 PageHelper 一篇博客就够了
    3. pageHelper详解
    4. Mybatis中的resultType和resultMap
    5. 如何使用分页插件
    6. springboot实现MyBatis分页
    7. Spring Boot:实现MyBatis分页
    8. mybatis 中文文档 配置

    正文

    1. pom.xml 节点 dependencies 下添加 pagehelper 依赖
    <!--        分页工具-->
            <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.3.1</version>
            </dependency>
    
    1. mapper.xml 添加 id 为 findByPaging 的查(直接查询所有即可,因为在 Service中的 PageHelper.startPage(pageNum, pageSize); 会对这个语句下面的第一个查询进行分页)
        <select id="findByPaging" resultType="com.xxx.blog.entity.ArticleEntity" parameterType="map">
            select
            *
            from `articles`
        </select>
    
    1. ArticleMapper.java
    @Mapper
    public interface ArticleMapper {
        public ArticleEntity findById(Integer id);
        public List<ArticleEntity> findByPaging(Map param);
    }
    
    1. ArticleService.java 新增 findByPaging 方法,将查询出的数据转换为 分页类型,对控制器隐藏处理过程
    @Service
    public class ArticleService {
        @Autowired
        ArticleMapper articleMapper;
        public ArticleEntity findById(Integer id){
            return articleMapper.findById(id);
        }
    
        /**
         * 分页查询
         * @param pageNum
         * @param pageSize
         * @param map
         * @return
         */
        public PageInfo findByPaging(Integer pageNum, Integer pageSize,Map map){
            PageHelper.startPage(pageNum, pageSize);
            List<ArticleEntity> articleEntityPage = articleMapper.findByPaging(map);
            PageInfo pageInfo = new PageInfo(articleEntityPage);
            return pageInfo;
        }
    }
    
    1. ArticleController.java 下新增 list 方法
    @RestController
    @RequestMapping("v1/article")
    public class ArticleController {
        @Autowired
        ArticleService articleService;
    
        @GetMapping(value = "", produces = "application/json;charset=UTF-8")
        public ResponseEntity<CustomResponseStructureDto<PageInfo>> list(@RequestParam(name = "page_num", defaultValue = "1") Integer pageNum, @RequestParam(name = "page_size",defaultValue = "10") Integer pageSize){
            Map<String, String> map = new HashMap<>();
            PageInfo pageInfo = articleService.findByPaging(pageNum, pageSize, map);
            return ResponseEntity.ok(new CustomResponseStructureDto(0, pageInfo, "成功"));
        }
    
        @GetMapping(value = "{id}", produces = "application/json;charset=UTF-8")
        public ResponseEntity<CustomResponseStructureDto<ArticleEntity>> list(@PathVariable("id") Integer id){
            ArticleEntity article = articleService.findById(id);
            return ResponseEntity.ok(new CustomResponseStructureDto<ArticleEntity>(0, article, "成功"));
        }
    }
    
    1. 测试 localhost:8080/v1/article?page_num=2&page_size=1
    {
        "code": 0,
        "msg": "成功",
        "data": {
            "total": 2,
            "list": [
                {
                    "id": 4,
                    "title": "测试文章002",
                    "introduction": "描述内容",
                    "created_at": null,
                    "updated_at": null,
                    "content": "内容12131内容12131内容12131内容12131内容12131内容12131",
                    "shows": 0,
                    "likes": 0
                }
            ],
            "pageNum": 2,
            "pageSize": 1,
            "size": 1,
            "startRow": 2,
            "endRow": 2,
            "pages": 2,
            "prePage": 1,
            "nextPage": 0,
            "isFirstPage": false,
            "isLastPage": true,
            "hasPreviousPage": true,
            "hasNextPage": false,
            "navigatePages": 8,
            "navigatepageNums": [
                1,
                2
            ],
            "navigateFirstPage": 1,
            "navigateLastPage": 2
        }
    }
    
    如果觉得文章对您有帮助,希望您能 关注+推荐 哦
  • 相关阅读:
    学习笔记-级数
    闭包
    superfetch服务到底该不该关闭
    用tor进入“深网”
    浏览器内核
    媒体查询实现响应式布局
    oracle用浏览器界面管理数据库
    angular登录状态检查
    从输入网址后。。。。。。
    原型
  • 原文地址:https://www.cnblogs.com/xiaqiuchu/p/15136679.html
Copyright © 2020-2023  润新知