【开端】Java 分页工具类运用

ops/2024/10/18 22:30:08/

一、绪论

Java系统中,分页查询的场景随处可见,本节介com.baomidou.mybatisplus.core.metadata.IPage;来分页的工具类

二、分页工具类

public class PageUtils implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 总记录数
     */
    private int totalCount;
    /**
     * 每页记录数
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 当前页数
     */
    private int currPage;
    /**
     * 列表数据
     */
    private List<?> list;
    
    /**
     * 分页
     * @param list        列表数据
     * @param totalCount  总记录数
     * @param pageSize    每页记录数
     * @param currPage    当前页数
     */
    public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
        this.list = list;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
    }

    /**
     * 分页
     */
    public PageUtils(IPage<?> page) {
        this.list = page.getRecords();
        this.totalCount = (int)page.getTotal();
        this.pageSize = (int)page.getSize();
        this.currPage = (int)page.getCurrent();
        this.totalPage = (int)page.getPages();
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrPage() {
        return currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    public List<?> getList() {
        return list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }
    
}
 

java">public class PageUtils implements Serializable {private static final long serialVersionUID = 1L;/*** 总记录数*/private int totalCount;/*** 每页记录数*/private int pageSize;/*** 总页数*/private int totalPage;/*** 当前页数*/private int currPage;/*** 列表数据*/private List<?> list;/*** 分页* @param list        列表数据* @param totalCount  总记录数* @param pageSize    每页记录数* @param currPage    当前页数*/public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {this.list = list;this.totalCount = totalCount;this.pageSize = pageSize;this.currPage = currPage;this.totalPage = (int)Math.ceil((double)totalCount/pageSize);}/*** 分页*/public PageUtils(IPage<?> page) {this.list = page.getRecords();this.totalCount = (int)page.getTotal();this.pageSize = (int)page.getSize();this.currPage = (int)page.getCurrent();this.totalPage = (int)page.getPages();}public int getTotalCount() {return totalCount;}public void setTotalCount(int totalCount) {this.totalCount = totalCount;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getTotalPage() {return totalPage;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public int getCurrPage() {return currPage;}public void setCurrPage(int currPage) {this.currPage = currPage;}public List<?> getList() {return list;}public void setList(List<?> list) {this.list = list;}}

Ipage接口

/*
 * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baomidou.mybatisplus.core.metadata;

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

import static java.util.stream.Collectors.toList;

/**
 * 分页 Page 对象接口
 *
 * @author hubin
 * @since 2018-06-09
 */
public interface IPage<T> extends Serializable {

    /**
     * 获取排序信息,排序的字段和正反序
     *
     * @return 排序信息
     */
    List<OrderItem> orders();

    /**
     * 自动优化 COUNT SQL【 默认:true 】
     *
     * @return true 是 / false 否
     */
    default boolean optimizeCountSql() {
        return true;
    }

    /**
     * {@link com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor#isOptimizeJoin()}
     * 两个参数都为 true 才会进行sql处理
     *
     * @return true 是 / false 否
     * @since 3.4.4 @2021-09-13
     */
    default boolean optimizeJoinOfCountSql() {
        return true;
    }

    /**
     * 进行 count 查询 【 默认: true 】
     *
     * @return true 是 / false 否
     */
    default boolean searchCount() {
        return true;
    }

    /**
     * 计算当前分页偏移量
     */
    default long offset() {
        long current = getCurrent();
        if (current <= 1L) {
            return 0L;
        }
        return Math.max((current - 1) * getSize(), 0L);
    }

    /**
     * 最大每页分页数限制,优先级高于分页插件内的 maxLimit
     *
     * @since 3.4.0 @2020-07-17
     */
    default Long maxLimit() {
        return null;
    }

    /**
     * 当前分页总页数
     */
    default long getPages() {
        if (getSize() == 0) {
            return 0L;
        }
        long pages = getTotal() / getSize();
        if (getTotal() % getSize() != 0) {
            pages++;
        }
        return pages;
    }

    /**
     * 内部什么也不干
     * <p>只是为了 json 反序列化时不报错</p>
     */
    default IPage<T> setPages(long pages) {
        // to do nothing
        return this;
    }

    /**
     * 分页记录列表
     *
     * @return 分页对象记录列表
     */
    List<T> getRecords();

    /**
     * 设置分页记录列表
     */
    IPage<T> setRecords(List<T> records);

    /**
     * 当前满足条件总行数
     *
     * @return 总条数
     */
    long getTotal();

    /**
     * 设置当前满足条件总行数
     */
    IPage<T> setTotal(long total);

    /**
     * 获取每页显示条数
     *
     * @return 每页显示条数
     */
    long getSize();

    /**
     * 设置每页显示条数
     */
    IPage<T> setSize(long size);

    /**
     * 当前页
     *
     * @return 当前页
     */
    long getCurrent();

    /**
     * 设置当前页
     */
    IPage<T> setCurrent(long current);

    /**
     * IPage 的泛型转换
     *
     * @param mapper 转换函数
     * @param <R>    转换后的泛型
     * @return 转换泛型后的 IPage
     */
    @SuppressWarnings("unchecked")
    default <R> IPage<R> convert(Function<? super T, ? extends R> mapper) {
        List<R> collect = this.getRecords().stream().map(mapper).collect(toList());
        return ((IPage<R>) this).setRecords(collect);
    }

    /**
     * 老分页插件不支持
     * <p>
     * MappedStatement 的 id
     *
     * @return id
     * @since 3.4.0 @2020-06-19
     */
    default String countId() {
        return null;
    }
}
 

java">/** Copyright (c) 2011-2021, baomidou (jobob@qq.com).** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package com.baomidou.mybatisplus.core.metadata;import java.io.Serializable;
import java.util.List;
import java.util.function.Function;import static java.util.stream.Collectors.toList;/*** 分页 Page 对象接口** @author hubin* @since 2018-06-09*/
public interface IPage<T> extends Serializable {/*** 获取排序信息,排序的字段和正反序** @return 排序信息*/List<OrderItem> orders();/*** 自动优化 COUNT SQL【 默认:true 】** @return true 是 / false 否*/default boolean optimizeCountSql() {return true;}/*** {@link com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor#isOptimizeJoin()}* 两个参数都为 true 才会进行sql处理** @return true 是 / false 否* @since 3.4.4 @2021-09-13*/default boolean optimizeJoinOfCountSql() {return true;}/*** 进行 count 查询 【 默认: true 】** @return true 是 / false 否*/default boolean searchCount() {return true;}/*** 计算当前分页偏移量*/default long offset() {long current = getCurrent();if (current <= 1L) {return 0L;}return Math.max((current - 1) * getSize(), 0L);}/*** 最大每页分页数限制,优先级高于分页插件内的 maxLimit** @since 3.4.0 @2020-07-17*/default Long maxLimit() {return null;}/*** 当前分页总页数*/default long getPages() {if (getSize() == 0) {return 0L;}long pages = getTotal() / getSize();if (getTotal() % getSize() != 0) {pages++;}return pages;}/*** 内部什么也不干* <p>只是为了 json 反序列化时不报错</p>*/default IPage<T> setPages(long pages) {// to do nothingreturn this;}/*** 分页记录列表** @return 分页对象记录列表*/List<T> getRecords();/*** 设置分页记录列表*/IPage<T> setRecords(List<T> records);/*** 当前满足条件总行数** @return 总条数*/long getTotal();/*** 设置当前满足条件总行数*/IPage<T> setTotal(long total);/*** 获取每页显示条数** @return 每页显示条数*/long getSize();/*** 设置每页显示条数*/IPage<T> setSize(long size);/*** 当前页** @return 当前页*/long getCurrent();/*** 设置当前页*/IPage<T> setCurrent(long current);/*** IPage 的泛型转换** @param mapper 转换函数* @param <R>    转换后的泛型* @return 转换泛型后的 IPage*/@SuppressWarnings("unchecked")default <R> IPage<R> convert(Function<? super T, ? extends R> mapper) {List<R> collect = this.getRecords().stream().map(mapper).collect(toList());return ((IPage<R>) this).setRecords(collect);}/*** 老分页插件不支持* <p>* MappedStatement 的 id** @return id* @since 3.4.0 @2020-06-19*/default String countId() {return null;}
}

三、运用

    /**
     * 分页查询权限信息
    * @param param
    * @return
    */
   public PageUtils queryPage(Map<String, Object> param) {

       IPage<TMenu> page = new Query<TMenu>().getPage(param);
       QueryWrapper<TMenu> wrapper = new QueryWrapper<>();
       if (param.containsKey("leftId")) {
           wrapper.eq("left_id", param.get("leftId").toString());
       }
       wrapper.eq("state", "0");
       wrapper.orderByAsc("sort");
       IPage<TMenu> tMenuIPage = tMenuMapper.selectPage(page, wrapper);
       return new PageUtils(tMenuIPage);

   }

java">    /*** 分页查询权限信息* @param param* @return*/public PageUtils queryPage(Map<String, Object> param) {IPage<TMenu> page = new Query<TMenu>().getPage(param);QueryWrapper<TMenu> wrapper = new QueryWrapper<>();if (param.containsKey("leftId")) {wrapper.eq("left_id", param.get("leftId").toString());}wrapper.eq("state", "0");wrapper.orderByAsc("sort");IPage<TMenu> tMenuIPage = tMenuMapper.selectPage(page, wrapper);return new PageUtils(tMenuIPage);}


http://www.ppmy.cn/ops/93677.html

相关文章

一篇文章教会你如何使用Haproxy,内含大量实战案例

1. Haproxy 介绍 HAProxy是法国开发者 威利塔罗&#xff08;Willy Tarreau&#xff09; 使用C语言编写的自由及开放源代码软件&#xff0c;是一款具备高并发&#xff08;万级以上&#xff09;、高性能的TCP和HTTP应用程序代理. HAProxy运行在当前的硬件上&#xff0c;可以支持…

论企业私域流量运营中的玩法创新与开源 AI 智能名片 O2O 商城小程序的应用

摘要&#xff1a;本文旨在探讨企业在构建私域流量池时的多种玩法策略&#xff0c;并着重分析如何针对不同类型客户制定个性化方案。同时&#xff0c;引入开源 AI 智能名片 O2O 商城小程序这一工具&#xff0c;阐述其在私域流量运营中的重要作用和价值&#xff0c;为企业提升运营…

【JAVA开发】JAVA开发手册

前言 《Java开发手册》是阿里巴巴集团技术团队的集体智慧结晶和经验总结&#xff0c;经历了多次大规模一线实战的检验及不断的完善&#xff0c;系统化地整理成册&#xff0c;反馈给广大开发者。现代软件行业的高速发展对开发者的综合素质要求越来越高&#xff0c;因为不仅是编…

allegor工具实现位号自动对齐

前言&#xff1a;用allegor绘制PCB时&#xff0c;需要调丝印&#xff0c;于是想到能用工具或者skill实现位号自动对齐。 主要用到两个功能&#xff1a;Lable tune&#xff0c;和Change Lable tune* 对齐之前是这样的 然后做如下设置 选择Allegor productivity Toolbox, 然…

探索list与iterator的区别及yield的用法

1 问题 探索list与iterator的区别探索yield的用法 2 方法 通过网上学习后了解到 List返回的类型是list&#xff0c;list只会查询一级缓存。list()中返回的List中每个对象都是原本的对象。查询的时候没遍历一个对象会产生一条sql&#xff1b;而iterator这个迭代器返回的类型是it…

Sketch软件挑战Adobe Illustrator:国产设计软件的竞争力分析

你是否知道如何选择合适的界面设计软件&#xff1f;你是否知道设计美观的用户界面软件有哪些吗&#xff1f;&#xff0c;以及你是否知道良好用户体验的应用程序&#xff0c;需要哪些界面设计软件&#xff1f;根据不同的产品界面功能&#xff0c;所选的界面设计软件也会有所不同…

jenkins 安装以及自动构建maven项目并且运行

在这里找到你对应jdk的版本的jenkins包 War Jenkins Packages 我这里用的使java8,所以下载 https://mirrors.jenkins.io/war-stable/2.60.1/jenkins.war 然后jenkins可以安装到centos系统 在本地windows系统运行命令行 scp C:\Users\98090\Downloads\jenkins.war root@192…

如何确保LLMs在IDE中的交互既高效又安全?

确保LLMs在IDE中的交互既高效又安全&#xff0c;需要考虑多个层面的策略。首先&#xff0c;从系统安全的角度来看&#xff0c;大模型安全综述中提到的风险分类体系和相应的防御策略是关键 。这包括输入模块的风险管理&#xff0c;比如通过设计防御性提示语和对抗性提示语的检测…