JAVA实现“分页功能”

本文介绍了两种在Java中实现分页功能的方法。第一种方法涉及pom.xml文件的依赖配置,第二种方法则通过使用工具类来实现。详细步骤包括相关库的引入和具体代码实现。
摘要由CSDN通过智能技术生成

方法一:

1.pom.xml依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.11</version>
</dependency>
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
    <exclusions>
        <exclusion>
            <artifactId>mybatis-spring</artifactId>
            <groupId>org.mybatis</groupId>
        </exclusion>
        <exclusion>
            <artifactId>mybatis</artifactId>
            <groupId>org.mybatis</groupId>
        </exclusion>
    </exclusions>
</dependency>
PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
List<DataElement> dataElements = baseMapper.elementList(dto);
PageInfo<DataElement> dataElementPageInfo = new PageInfo<>(dataElements);

方法二: 

1.工具类

package com.messageadapter.util;

import com.messageadapter.common.api.ResultPageObject;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class PageUtil<E> {

    /**
     * 分页
     *
     * @param list     数据
     * @param pageNum  页数
     * @param pageSize 条数
     * @return
     */
    public ResultPageObject listPagination(List<E> list, Integer pageNum, Integer pageSize) {
     ResultPageObject resultPageObject = new ResultPageObject();
     if (CollectionUtils.isEmpty(list)) {
      resultPageObject.setCode(200);
      resultPageObject.setErrMsg("无数据");
      return resultPageObject;
     }
     final int total = list.size();
     int totalSize = pageNum * pageSize;
     if (totalSize > total) {
      totalSize = total;
     }
     int start = (pageNum - 1) * pageSize;
     List<E> newArray = new ArrayList<>(pageSize);
     for (int i = start; i < totalSize; i++) {
      newArray.add(list.get(i));
     }

     resultPageObject.setCode(200);
     resultPageObject.setPageCurrent(pageNum);
     resultPageObject.setPageSize(pageSize);
     resultPageObject.setTotal(Long.parseLong(String.valueOf(total)));
     int totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
     resultPageObject.setTotalPages(Long.parseLong(String.valueOf(totalPage)));
     resultPageObject.setData(newArray);
     return resultPageObject;
    }
}
import io.swagger.annotations.*;
import lombok.*;

/**
 * @description:页面对象
 * @time: 2019/11/6 14:13
 */
@Data
@ApiModel
public class ResultPageObject {

    @ApiModelProperty(value = "状态码")
    private Integer code;
    @ApiModelProperty(value = "每页条数")
    private Integer pageSize;
    @ApiModelProperty(value = "总条数")
    private Long total;
    @ApiModelProperty(value = "数据")
    private Object data;
    @ApiModelProperty(value = "当前页")
    private Integer pageCurrent;
    @ApiModelProperty(value = "总页数")
    private Long totalPages;

    private String errMsg;

    public ResultPageObject() {
    }

    public ResultPageObject(Integer code, Integer pageSize, Long total, Object data,
                Integer pageCurrent, Long totalPages) {
     this.code = code;
     this.pageSize = pageSize;
     this.total = total;
     this.data = data;
     this.pageCurrent = pageCurrent;
     this.totalPages = totalPages;

    }

    public ResultPageObject(Integer code, String errMsg) {
     this.code = code;
     this.errMsg = errMsg;
    }

    public static ResultPageObject successReturn(Integer pageSize, Long total, Object data,
                         Integer pageCurrent, Long totalPages) {
     return new ResultPageObject(200, pageSize, total, data, pageCurrent, totalPages);
    }

    /**
     * @param message 错误信息
     * @Description://
     * @Param:
     * @Return:
     * @Author: wzw
     * @Date: 2019/11/25 18:04
     */
    public static ResultPageObject errorPageObject(String message) {
     return new ResultPageObject(500, message);
    }
}
PageUtil pageUtil = new PageUtil();
ResultPageObject resultPageObject = pageUtil.listPagination(dataElements, dto.getPageNum(), dto.getPageSize());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_57322959

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值