From 9f5a03edcdae580a447f6788ed313bfb9f79ec5b Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期四, 13 五月 2021 21:22:51 +0800 Subject: [PATCH] add 增加分页工具 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/PageUtils.java | 101 +++++++++++++++++++++++++ ruoyi-common/src/main/java/com/ruoyi/common/core/page/PagePlus.java | 128 ++++++++++++++++++++++++++++++++ ruoyi-common/src/main/java/com/ruoyi/common/core/page/TableDataInfo.java | 5 3 files changed, 232 insertions(+), 2 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/page/PagePlus.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/page/PagePlus.java new file mode 100644 index 0000000..7200fc5 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/page/PagePlus.java @@ -0,0 +1,128 @@ +package com.ruoyi.common.core.page; + +import cn.hutool.core.bean.BeanUtil; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.metadata.OrderItem; +import lombok.Data; +import lombok.experimental.Accessors; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +@Data +@Accessors(chain = true) +public class PagePlus<T,K> implements IPage<T> { + + protected List<T> records; + protected List<K> recordsVo; + protected long total; + protected long size; + protected long current; + protected List<OrderItem> orders; + protected boolean optimizeCountSql; + protected boolean isSearchCount; + protected boolean hitCount; + protected String countId; + protected Long maxLimit; + + public PagePlus() { + this.records = Collections.emptyList(); + this.recordsVo = Collections.emptyList(); + this.total = 0L; + this.size = 10L; + this.current = 1L; + this.orders = new ArrayList(); + this.optimizeCountSql = true; + this.isSearchCount = true; + this.hitCount = false; + } + + public PagePlus(long current, long size) { + this(current, size, 0L); + } + + public PagePlus(long current, long size, long total) { + this(current, size, total, true); + } + + public PagePlus(long current, long size, boolean isSearchCount) { + this(current, size, 0L, isSearchCount); + } + + public PagePlus(long current, long size, long total, boolean isSearchCount) { + this.records = Collections.emptyList(); + this.total = 0L; + this.size = 10L; + this.current = 1L; + this.orders = new ArrayList(); + this.optimizeCountSql = true; + this.isSearchCount = true; + this.hitCount = false; + if (current > 1L) { + this.current = current; + } + + this.size = size; + this.total = total; + this.isSearchCount = isSearchCount; + } + + public boolean hasPrevious() { + return this.current > 1L; + } + + public boolean hasNext() { + return this.current < this.getPages(); + } + + public void recordsToVo(Class<K> kClass) { + this.recordsVo = this.records.stream() + .map(any -> BeanUtil.toBean(any, kClass)) + .collect(Collectors.toList()); + } + + @Override + public String countId() { + return this.getCountId(); + } + + @Override + public Long maxLimit() { + return this.getMaxLimit(); + } + + public PagePlus<T, K> addOrder(OrderItem... items) { + this.orders.addAll(Arrays.asList(items)); + return this; + } + + public PagePlus<T, K> addOrder(List<OrderItem> items) { + this.orders.addAll(items); + return this; + } + + @Override + public List<OrderItem> orders() { + return this.getOrders(); + } + + @Override + public boolean optimizeCountSql() { + return this.optimizeCountSql; + } + + @Override + public boolean isSearchCount() { + return this.total < 0L ? false : this.isSearchCount; + } + + public PagePlus<T, K> setSearchCount(boolean isSearchCount) { + this.isSearchCount = isSearchCount; + return this; + } + +} + diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/page/TableDataInfo.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/page/TableDataInfo.java index 72c284f..4e71fc1 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/page/TableDataInfo.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/page/TableDataInfo.java @@ -1,6 +1,7 @@ package com.ruoyi.common.core.page; -import lombok.*; +import lombok.Data; +import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serializable; @@ -37,7 +38,7 @@ * @param list 鍒楄〃鏁版嵁 * @param total 鎬昏褰曟暟 */ - public TableDataInfo(List<T> list, int total) + public TableDataInfo(List<T> list, long total) { this.rows = list; this.total = total; diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/PageUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/PageUtils.java new file mode 100644 index 0000000..e484f6d --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/PageUtils.java @@ -0,0 +1,101 @@ +package com.ruoyi.common.utils; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.http.HttpStatus; +import com.baomidou.mybatisplus.core.metadata.OrderItem; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.ruoyi.common.core.page.PagePlus; +import com.ruoyi.common.core.page.TableDataInfo; +import com.ruoyi.common.utils.sql.SqlUtil; + +import java.util.List; + +/** + * 鍒嗛〉宸ュ叿 + * + * @author Lion Li + */ +public class PageUtils { + + /** + * 褰撳墠璁板綍璧峰绱㈠紩 + */ + public static final String PAGE_NUM = "pageNum"; + + /** + * 姣忛〉鏄剧ず璁板綍鏁� + */ + public static final String PAGE_SIZE = "pageSize"; + + /** + * 鎺掑簭鍒� + */ + public static final String ORDER_BY_COLUMN = "orderByColumn"; + + /** + * 鎺掑簭鐨勬柟鍚� "desc" 鎴栬�� "asc". + */ + public static final String IS_ASC = "isAsc"; + + public static <T, K> PagePlus<T, K> buildPagePlus() { + Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM); + Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE); + String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN); + String isAsc = ServletUtils.getParameter(IS_ASC); + PagePlus<T, K> page = new PagePlus<>(pageNum, pageSize); + if (StrUtil.isNotBlank(orderByColumn)) { + String orderBy = SqlUtil.escapeOrderBySql(orderByColumn); + if ("asc".equals(isAsc)) { + page.addOrder(OrderItem.asc(orderBy)); + } else if ("desc".equals(isAsc)) { + page.addOrder(OrderItem.desc(orderBy)); + } + } + return page; + } + + public static <T> Page<T> buildPage() { + Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM); + Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE); + String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN); + String isAsc = ServletUtils.getParameter(IS_ASC); + Page<T> page = new Page<>(pageNum, pageSize); + if (StrUtil.isNotBlank(orderByColumn)) { + String orderBy = SqlUtil.escapeOrderBySql(orderByColumn); + if ("asc".equals(isAsc)) { + page.addOrder(OrderItem.asc(orderBy)); + } else if ("desc".equals(isAsc)) { + page.addOrder(OrderItem.desc(orderBy)); + } + } + return page; + } + + public static <T, K> TableDataInfo<K> buildDataInfo(PagePlus<T, K> page) { + TableDataInfo<K> rspData = new TableDataInfo<>(); + rspData.setCode(HttpStatus.HTTP_OK); + rspData.setMsg("鏌ヨ鎴愬姛"); + rspData.setRows(page.getRecordsVo()); + rspData.setTotal(page.getTotal()); + return rspData; + } + + public static <T> TableDataInfo<T> buildDataInfo(Page<T> page) { + TableDataInfo<T> rspData = new TableDataInfo<>(); + rspData.setCode(HttpStatus.HTTP_OK); + rspData.setMsg("鏌ヨ鎴愬姛"); + rspData.setRows(page.getRecords()); + rspData.setTotal(page.getTotal()); + return rspData; + } + + public static <T> TableDataInfo<T> buildDataInfo(List<T> list) { + TableDataInfo<T> rspData = new TableDataInfo<>(); + rspData.setCode(HttpStatus.HTTP_OK); + rspData.setMsg("鏌ヨ鎴愬姛"); + rspData.setRows(list); + rspData.setTotal(list.size()); + return rspData; + } + +} -- Gitblit v1.9.3