Springboot 和 layui 整合 pageHelper分页插件

本文介绍如何在SpringBoot项目中整合PageHelper插件实现数据分页功能,包括必要的Maven依赖配置及Service层分页处理示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@2900498871@qq.com

这里对springboot+pagehelper 整合做一个记录
首先在pom.xml 导入依赖

// An highlighted block
<!--pagehelper 分页插件-->
      <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper</artifactId>
          <version>5.1.8</version>
      </dependency>

      <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
          <version>1.2.5</version>
      </dependency>

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

这里需要导入3个依赖否则分页可能出现问题,(会查询出所有的数据),
再service(最好在service 中) 中进行分页处理

/**
  * 加载用户数据,分页
  * @param user
  * @param pageSize 一页条数
  * @param pageNum 第几页
  * @return
  */
 public templetJson loadList(user user , Integer pageSize, Integer pageNum){
     templetJson tem=null;
     try{
     	//分页初始化
         Page<user> page = PageHelper.startPage(pageNum, pageSize);
         //加载用户列表
         List list= userMapper.loadUserList(user);
         //传入list就可以了
         PageInfo  pageInfo = new PageInfo(list);
         //处理分页数据(根据需要添加)
         tem=utils.loadJsonPage(pageInfo);
     }catch (Exception e){
         e.printStackTrace();
     }
    return tem;
 }

loadJsonPage() 方法处理layui 分页使用的方法

  /**
  * 分页方法使用
  * @param pageInfo
  * @return
  */
 public static templetJson loadJsonPage(PageInfo pageInfo){
     templetJson templetJson = new templetJson();
     if(pageInfo!=null){

     }
     try{
         List list= pageInfo.getList();
         int i=(int)pageInfo.getTotal();
         templetJson.setCount(i);
         templetJson.setCode(0);
         templetJson.setMsg("");
         templetJson.setData(list);
     }catch (Exception e){
         templetJson.setCode(1);
     }
     return templetJson;
 }

templetJson bean

package com.example.manage.bean;

/**
* layui 的模板数据bean
*/
public class templetJson {
 private Integer code;
 private String msg;
 private Integer count;
 private Object data;

 public Integer getCode() {
     return code;
 }

 public void setCode(Integer code) {
     this.code = code;
 }

 public String getMsg() {
     return msg;
 }

 public void setMsg(String msg) {
     this.msg = msg;
 }

 public Integer getCount() {
     return count;
 }

 public void setCount(Integer count) {
     this.count = count;
 }

 public Object getData() {
     return data;
 }

 public void setData(Object data) {
     this.data = data;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值