欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

分页查询(PageHelper插件)

程序员文章站 2022-06-18 22:38:38
...

spring boot+mybatis+jquery + thmeleaf +PageHelper做分页查询

分页插件配置时,依赖包

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

Dao层

@Select("select * from student")
     List<Student> listu();

Service 层

@Override
    public List<Student> listu() {
        return studentDao.listu();
    }

@Override
    public List<Student> queryUserList(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return  studentDao.listu();                            //查询必须跟在startPage之后,中间不能放任何语句
}

Controller层

 @RequestMapping("/queryAll")
    public String list(Map<String,Object> map) {
        List<Student> list = iStudentService.listu();
        //list.stream().forEach(x->System.out.println(x));
        map.put("list", list);
        return "show";
    }
@RequestMapping("/findCommodityAction_1")
public String findCommodityAction_1 (Model model , Integer page , Integer limit){
    List<Student> students=iStudentService.queryUserList (page , limit);
    //然后把查询出来的数据放到插件自带的PageInfo对象里面;

    PageInfo<Student> pageInfo = new PageInfo<Student>(students);
    System.out.println(pageInfo);

    model.addAttribute ("pageInfo" , pageInfo);
    return "show";
}

前端jquery 页面

<div id="ye">
        <p>当前第  <td th:text="${pageInfo.pageNum}"></td> 页,总 <td th:text="${pageInfo.pages}"></td> 页,共<td th:text="${pageInfo.total}"></td> 条记录</p>
        <p>
            <a th:href="@{/findCommodityAction_1?page=1&limit=6}">首页</a>
            <a th:if="${pageInfo.hasPreviousPage}" th:href="@{'/findCommodityAction_1?page=' + ${pageInfo.pageNum - 1} + '&limit=6'}">上一页</a>
            <a th:if="${pageInfo.hasNextPage}" th:href="@{'/findCommodityAction_1?page=' + ${pageInfo.pageNum + 1} + '&limit=6'}">下一页</a>
            <a th:href="@{'/findCommodityAction_1?page=' + ${pageInfo.pages} + '&limit=6'}">末页</a>
        </p>
    </div>

(在后端进入页面时必须输入?page=1&limit=6 才能找到页面)