SpringBoot后台系统--新手教程(四)pagehelper分页插件

分页查询,我们使用pagehelper分页插件,在SpringBoot项目中,我们要使用 pagehelper作为分页插件,我需要导入pagehelper插件的启动器依赖,直接导入pagehelper依赖会报错。

在这里插入图片描述

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>
  • 在我们导入好依赖后,在开始进行分页查询之前,我们需要一个PageResult的工具类

    import java.io.Serializable;
    import java.util.List;
    
    public class PageResult implements Serializable {
        private Long total;//总记录数
        private List rows;//当前页结果
        public PageResult(Long total, List rows) {
            super();
            this.total = total;
            this.rows = rows;
        }
        public Long getTotal() {
            return total;
        }
        public void setTotal(Long total) {
            this.total = total;
        }
        public List getRows() {
            return rows;
        }
        public void setRows(List rows) {
            this.rows = rows;
        }
    }
    

    【把我们的工具类放到我们的entity包下】

  • 这时我们还需要一个QueryPageBean的工具类

    package com.sanchi.springboot_mybatis.pojo;
    
    import java.io.Serializable;
    
    public class QueryPageBean implements Serializable {
        private Integer currentPage;//页码
        private Integer pageSize;//每页记录数
        private String queryString;//查询条件
    
        public Integer getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
    
        public Integer getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public String getQueryString() {
            return queryString;
        }
    
        public void setQueryString(String queryString) {
            this.queryString = queryString;
        }
    }
    
  • 导入好我们的工具类后,我们开始进行分页查询,我们的分页查询,需要前端给我们传递俩个参数,分别是:当前页、每页数据量。这时候我们开始写接口。

    public Result findAll(Integer currentPage, Integer pageSize) {
        return null;
    }
    
  • 简单的接口写好后,我们开始使用pagehelper进行分页查询。

    public Result findAll(Integer currentPage, Integer pageSize) {
        //首先创建QueryPageBean
    	QueryPageBean queryPageBean = new QueryPageBean();
        //创建好Bean之后,向我们的PageBean里放置前端向我们传递的参数,当前页和每页的数据量
    	queryPageBean.setCurrentPage(currentPage);
    	queryPageBean.setPageSize(pageSize);
        //将QueryPageBean传入业务层的方法进行查询,使用我们的工具类接收数据
    	PageResult pageResult = service.findAll(queryPageBean);
    	return null;
    }
    

    【我们的controller写好后,开始编写业务层的代码】

  • 在我们的service层开始写方法

    public PageResult findAll(QueryPageBean queryPageBean) {
        //启动PageHelper,放入PageHelper需要的参数,使用Page接收数据
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //调用mapper的查询方法,使用List<Student>进行数据接收
        List<Student> students = mapper.findAll();
        //我们方法的返回值是PageResult,所以我们使用PageResult进行返回,返回到这个方法的调用者
        return new PageResult(page.getTotal(), students);
    }
    

    【这时,我们service的代码就写好了】

  • 在我们的dao层开始写我们方法
    在这里插入图片描述

    <select id="findAll" resultType="com.sanchi.springboot_mybatis.pojo.Student">
        select * from student
    </select>
    

    【我们使用的是xml的方式进行sql查询的,所以在dao层写好接口后,在我们xml文件中写我们的sql查询】

    我们已经使用了pagehelper插件作为我们的分页查询插件了,所以在sql语句这里,我们不需要做多余的操作,直接把我们需要的数据全部查询出来就ok了,pagehelper会帮我们对我们查询出来的数据进行分页操作的

  • dao层和service层我们都写好了,但是在controller层我们返回了null,接下来我们继续完成我们controller

    @RequestMapping("/findAll")
    public Result findAll(Integer currentPage, Integer pageSize) {
        QueryPageBean queryPageBean = new QueryPageBean();
        queryPageBean.setCurrentPage(currentPage);
        queryPageBean.setPageSize(pageSize);
        PageResult pageResult = service.findAll(queryPageBean);
        //我们controller返回是使用我们的Result工具类进行返回的,所以把我们使用pagehelper查询到的数据放进去,进行返回就ok了
        return new Result(true, MessageConstant.QUERY_STUDENT_SUCCESS, pageResult);
    }
    

    使用工具类是为了方便我们对代码进行管理,防止代码冗余

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值