How does springboot+mybatis integrate the pagehelper plugin

Before learning, make sure you are familiar with the use of springboot and the use of mybatis: do
n’t talk too much, first rely on:

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.3</version>
        </dependency>

The version suggested here is the same as mine, otherwise the result set list may not be loaded.
The premise of use is that the database and mybatis have been configured.
First, query all methods in the dao layer interface configuration:
dao layer interface:

    //存储Blog
    int saveBlog(Blog blog);

Then configure the mapper:
mapper:

    <!--查找全部博客-->
    <select id="queryBlog" resultType="com.hncu.cocoblog.entities.Blog">
        select * from t_blog
    </select>

For your own projects or get started, write a method to query all records on the dao layer and configure it in the mapper.
Next is how to use:
Service layer:

    @Override
    public PageInfo findPage(int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Blog> list=blogDao.queryBlog(); //这里的是我的查询所有的方法,根据情况,改成自己的
        PageInfo<Blog> pageInfo=new PageInfo<Blog>(list); //将list(全部结果传进去) 泛型根据情况而定
        return pageInfo;  //返回pageInfo对象给controller
    }

The Service layer is finished, and the Controller is next:

    // 分页查询博客
    @GetMapping("/getBlogByPage/{page}")
    public String getBlogByPage(@PathVariable("page") Integer page, Model model) {
        PageInfo<Blog> pageInfo = blogService.findPage(page, 8); //获取指定页的8条信息
        //这些信息都需要传递到前端做分页
        model.addAttribute("pageNum", pageInfo.getPageNum());//得到当前页
        model.addAttribute("pages", pageInfo.getPages()); //得到总页数
        model.addAttribute("list", pageInfo.getList()); //将分页得到的list集合返回到前端
        return "manageBlog";
    }

The required information is obtained through the pageInfo object passed from the Service layer, and we only need to pass two parameters to the Service PageHelper.startPage(page,pageSize);, one is the page number, and the other is the number of records per page.

The above is the use of pagehelper, next look at the parameters:

public class PageInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//由于startRow 和endRow 不常用,这里说个具体的用法
//可以在页面中"显示startRow 到endRow 共size 条数据"
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List<T> list;
//前一页
private int prePage;
//下一页
private int nextPage;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
//导航条上的第一页
private int navigateFirstPage;
//导航条上的最后一页
private int navigateLastPage;
}

We can all get this information through the set/get method of pageInfo!

Guess you like

Origin blog.csdn.net/Pzzzz_wwy/article/details/106207534