当前位置: 首页 > news >正文

在Java中如何使用List集合实现分页,以及模糊查询后分页

物理分页工具类

package com.yutu.garden.utils;import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;/*** @ClassName: PageUtils* @Description: 物理分页* @Author* @Date 2022/12/28* @Version 1.0*/
public class PageUtils {/*** 物理分页* @param list 数据* @param current 当前页* @param size 页码长度* @param <T>* @return*/public static <T> List<T> startPage(List<T> list, Integer current, Integer size) {if (CollectionUtils.isEmpty(list)) {return list;}return list.stream().skip((current - 1) * size).limit(size).collect(Collectors.toList());}
}

接收参数VO类

package com.yutu.garden.vo;import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;/*** @ClassName: SanitationJobStatisticsStaffParamVo* @Description: 人员列表请求参数* @Author* @Date 2023/8/8* @Version 1.0*/
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class SanitationJobStatisticsStaffParamVo {@ApiModelProperty("姓名")private String name;@ApiModelProperty("当前页")private Long current;@ApiModelProperty("每页的数量")private Long size;
}

核心代码

  • sanitationJobStatisticsStaffVoArrayList:是数据库中查出来的数据
  • sanitationJobStatisticsStaffParamVo:是接收参数VO类
//物理分页
List<SanitationJobStatisticsStaffVo> sanitationJobStatisticsStaffVosPage = PageUtils.startPage(sanitationJobStatisticsStaffVoArrayList, Integer.parseInt(sanitationJobStatisticsStaffParamVo.getCurrent().toString()), Integer.parseInt(sanitationJobStatisticsStaffParamVo.getSize().toString()));
map.put("tableData",sanitationJobStatisticsStaffVosPage);
//如果有模糊查询 则按模糊查询去分页
if (StringUtils.isNotEmpty(sanitationJobStatisticsStaffParamVo.getName())){//过滤 模糊查询List<SanitationJobStatisticsStaffVo> collectLikeSelect = sanitationJobStatisticsStaffVoArrayList.stream().filter(sanitationJobStatisticsStaffVo -> sanitationJobStatisticsStaffVo.getName().toLowerCase().contains(sanitationJobStatisticsStaffParamVo.getName().toLowerCase())).collect(Collectors.toList());//模糊查询后得到一个List集合,再将这个集合进行分页List<SanitationJobStatisticsStaffVo> sanitationJobStatisticsStaffVosLikePage = PageUtils.startPage(collectLikeSelect, Integer.parseInt(sanitationJobStatisticsStaffParamVo.getCurrent().toString()), Integer.parseInt(sanitationJobStatisticsStaffParamVo.getSize().toString()));map.put("tableData",sanitationJobStatisticsStaffVosLikePage);
}
http://www.lryc.cn/news/123190.html

相关文章:

  • 【JAVA】包装类、正则表达式、Arrays类、Lambda表达式
  • Java中的Maven Assembly插件是什么?
  • SpringBoot禁用Swagger3
  • 小红书Java后端2023-8-6笔试
  • metaRTC7 demo mac/ios编译指南
  • systemd-journal 占用内存的问题
  • Java # Spring(2)
  • 2021年03月 C/C++(二级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • 应用程序运行报错:First section must be [net] or [network]:No such file or directory
  • 【ECMAScript】ES6-ES11学习笔记
  • K8S MetalLB LoadBalancer
  • kubernetes二进制部署2之 CNI 网络组件部署
  • docker通用镜像方法,程序更新时不用重新构建镜像
  • Spring Cloud构建微服务断路器介绍
  • [国产MCU]-BL602开发实例-OLED-SSD1306驱动与U8g2移植
  • AWS asg(Auto Scaling Group)部署时报错Error: Termination Reason: Client.InternalError
  • Redis—过期删除策略和内存淘汰策略
  • 连续两年增收不增利,比亚迪电子靠新能源汽车业务再次起飞?
  • echarts3d柱状图
  • 使用webpack插件webpack-dev-server 出现Cannot GET/的解决办法
  • 老网工必备好物,分享15个网络监控神器
  • 拒绝摆烂!C语言练习打卡第一天
  • Spring 使用注解开发、代理模式、AOP
  • 考公-判断推理-逻辑判断-翻译推理
  • 关于MPU6050的VLOGIC引脚作用
  • 对约瑟夫问题的进一步思考
  • 程序员如何优雅的提升软件开发效率?
  • 宽屏企业网站介绍
  • OPENCV C++(八)HOG的实现
  • 干货分享:制作婚礼请柬的技巧,从零基础起步