springboot+pagehelper做分页处理

做分页处理,一直觉得pagehelper比较好用,以下是分享springboot+pagehelper做的分页处理。
1.导入pagehelper所需的jar

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

2.springboot中加入yml

pagehelper:
    auto-dialect: mysql
    reasonable: true
    close-conn: false

3.根据不同的前端框架返回要求格式进行返回数据,创建返回数据的model,我这里是采用的datatables做的数据表格,所以的他的返回数据格式如下:

package com.zkr.cxhjcxt.page;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * Created by youzongxu on 2018/8/29.
 */
@Data
public class PageResult implements Serializable{
    private static final long serialVersionUID = 7223334258815093683L;
    private Long recordsTotal;
    private Long recordsFiltered;
    private List<?> data;

    public PageResult (long recordsTotal, long recordsFiltered, List<?> data){
        super();
        this.recordsTotal = recordsTotal;
        this.recordsFiltered = recordsFiltered;
        this.data = data;
    }
}

4.controller分页写法

    /**
     * jira缺陷列表
     * @param jiraBugWhere
     * @return
     */
    @ApiOperation(value = "jira缺陷列表获取")
    @GetMapping
    public PageResult list(JiraBugWhere jiraBugWhere){
        //处理引入pagehelper,标明开始页以及每页所展示的相关条数
        PageHelper.startPage(jiraBugWhere.getStart(),jiraBugWhere.getLength());
        //根据条件所查询出的数据集合
        List<JiraBug> lists = jiraBugDao.list(jiraBugWhere);
        //pagehelper做分页处理
        PageInfo<JiraBug> p = new PageInfo<>(lists);
        //返回结果集
        return new PageResult(p.getTotal(),p.getTotal(),p.getList());
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用Spring Boot和Layui实现分页,可以按照以下步骤进行操作: 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> ``` 这个依赖是用来实现分页的,它是基于MyBatis的分页插件,可以帮助我们快速实现分页功能。 2. 编写Controller 在Controller中定义一个方法,用于查询数据并返回分页结果。可以使用PageHelper.startPage()方法来开启分页功能,并将查询结果包装成PageInfo对象返回。例如: ```java @GetMapping("/list") public PageInfo<User> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { PageHelper.startPage(pageNum, pageSize); List<User> userList = userService.list(); return new PageInfo<>(userList); } ``` 3. 编写前端页面 使用Layui的表格组件来展示分页数据。在html文件中引入Layui的相关文件,并创建一个表格: ```html <table class="layui-table" lay-data="{url: '/user/list', page:true, limit:10}"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr> <td>{{d.id}}</td> <td>{{d.name}}</td> <td>{{d.age}}</td> <td>{{d.gender}}</td> </tr> </tbody> </table> ``` 这里使用了Layui的数据表格组件,通过设置url属性来指定数据接口的地址,page属性来开启分页功能,limit属性来设置每页显示的数据量。在表格中使用{{d.xxx}}的形式来渲染数据。 4. 启动应用程序 现在可以启动Spring Boot应用程序并访问前端页面,就能看到分页数据了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值