PageHelper结合Lambda表达式实现简洁的分页封装

  Java   13分钟   301浏览   0评论

😊 @ 作者: 召田最帅boy
💖 @ 主页: https://www.hqxiaozou.top
🎉 @ 主题: PageHelper结合Lambda表达式实现简洁的分页封装
⏱️ @ 创作时间: 2023年02月20日

引入POM

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.4.2</version>
</dependency>

封装PageBean

创建查询分页类BaseQueryParam,其需要传递的查询参数类,可以直接进行继承,代码如下:

@Data
public class BaseQueryParam {
    /**
     * 当前页码
     */
    private Integer pageNum;

    /**
     * 每页数量
     */
    private Integer pageSize;
}

封装新的PageBean,利用泛型封装统一的请求方法与响应分页信息参数,代码如下:

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;

public class NewPageBean<T> {
    private Long total;
    private List<T> list;
    private MyPage pageInfo;

    /**
     * @param select   调用Mapper接口的结果
     * @param param  请求参数
     * @param tClass   响应的Class对象
     * @param <T>
     * @return
     */
    public static <T> NewPageBean<T> generatePage(ISelect select, BaseQueryParam param, Class<T> tClass) {
        Integer current = param.getPageNum();
        Integer pageSize = param.getPageSize();
        List<T> list = PageHelper
                .startPage(current == null ? 1 : current, pageSize == null ? 10 : pageSize)
                .doSelectPage(select);
        return new NewPageBean<>(list);
    }

    public NewPageBean(List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        this.total = pageInfo.getTotal();
        this.list = list;
        MyPage page = new MyPage();
        page.setTotalPage(pageInfo.getPages());
        page.setPageNum(pageInfo.getPageNum());
        page.setPageSize(pageInfo.getPageSize());
        page.setPreviousPage(pageInfo.getPrePage());
        page.setNextPage(pageInfo.getNextPage());
        this.pageInfo = page;
    }

    public long getNum() {
        return this.total;
    }

    public void setNum(Long total) {
        this.total = total;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public MyPage getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(MyPage pageInfo) {
        this.pageInfo = pageInfo;
    }

    static class MyPage {
        private int pageNum;
        private int pageSize;
        private int totalPage;
        private int nextPage;
        private int previousPage;

        public int getPageNum() {
            return pageNum;
        }

        public void setPageNum(int pageNum) {
            this.pageNum = pageNum;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getTotalPage() {
            return totalPage;
        }

        public void setTotalPage(int totalPage) {
            this.totalPage = totalPage;
        }

        public int getNextPage() {
            return nextPage;
        }

        public void setNextPage(int nextPage) {
            this.nextPage = nextPage;
        }

        public int getPreviousPage() {
            return previousPage;
        }

        public void setPreviousPage(int previousPage) {
            this.previousPage = previousPage;
        }
    }
}

定义Mapper

@Mapper
public interface TaskMapper {
    List<Task> list(BaseQueryParam param);
}

定义Mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.TaskMapper">

    <select id="list" parameterType="top.hqxiaozou.controller.page.BaseQueryParam" resultMap="BaseResultMap">
        select
            id,name,create_by,create_time,update_by,update_time,remark
        from sys_task
    </select>

</mapper>

具体实现

@RestController    
public class TaskController {

    @Resource
    private TaskMapper taskMapper;

    @GetMapping("test")
    public Object test(BaseQueryParam param) {
        /**
         * BaseQueryParam 就是查询的参数
         * Task 为响应的实体
         */
        return NewPageBean.generatePage(() -> taskMapper.list(param), param, Task.class);
    }
}

测试

测试url:http://localhost:9090/demo/test?pageNum=2&pageSize=2

SQL预览:

返回结果:

{
    "total":3,
    "list":[
        {
            "id":"1",
            "name":"定时任务一",
            "createBy":"1",
            "createTime":1676886586000,
            "updateBy":"1",
            "updateTime":1676886586000,
            "remark":""
        },
        {
            "id":"2",
            "name":"定时任务二",
            "createBy":"1",
            "createTime":1676886588000,
            "updateBy":"1",
            "updateTime":1676886588000,
            "remark":""
        }
    ],
    "pageInfo":{
        "pageNum":1,
        "pageSize":2,
        "totalPage":2,
        "nextPage":2,
        "previousPage":0
    },
    "num":3
}
如果你觉得文章对你有帮助,那就请作者喝杯咖啡吧☕
微信
支付宝
  0 条评论