From 7d51a13dd7a209a42a20e9cacff95cdcbbe8cabd Mon Sep 17 00:00:00 2001 From: MichaelWin Date: Wed, 28 Jan 2026 09:22:46 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91=E5=88=86?= =?UTF-8?q?=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../corewing/app/common/page/PageContext.java | 129 ++++++++++++++++-- .../app/common/page/PageInterceptor.java | 16 ++- 2 files changed, 133 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/corewing/app/common/page/PageContext.java b/src/main/java/com/corewing/app/common/page/PageContext.java index 435a37e..8d4f8d1 100644 --- a/src/main/java/com/corewing/app/common/page/PageContext.java +++ b/src/main/java/com/corewing/app/common/page/PageContext.java @@ -1,23 +1,134 @@ package com.corewing.app.common.page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import java.util.HashMap; +import java.util.Map; public class PageContext { - private static final ThreadLocal> PAGE_HOLDER = new ThreadLocal<>(); + private static final ThreadLocal PAGE_POOL_HOLDER = new ThreadLocal<>(); - // 设置分页对象 - public static void setPage(Page page) { - PAGE_HOLDER.set(page); + private static class PagePool { + private final Map> pool = new HashMap<>(); + private int counter = 0; + + public synchronized String generateKey() { + return "page_" + (counter++); + } + + public Page get(String key) { + return pool.get(key); + } + + public void put(String key, Page page) { + pool.put(key, page); + } + + public void remove(String key) { + pool.remove(key); + } + + public void clear() { + pool.clear(); + counter = 0; + } + + public boolean isEmpty() { + return pool.isEmpty(); + } } - // 泛型方法:获取指定类型的分页对象(消除警告) - @SuppressWarnings("unchecked") + /** + * 获取一个新的 Page 对象(带唯一key) + * 适合单个查询场景 + */ public static Page getPage(Class clazz) { - return (Page) PAGE_HOLDER.get(); + PagePool pool = getPagePool(); + String key = pool.generateKey(); + Page page = new Page<>(); + pool.put(key, page); + return page; } - // 清除线程变量 + /** + * 获取带有默认分页参数的 Page 对象 + */ + public static Page getPage(Class clazz, Integer current, Integer size) { + int pageNum = current != null && current > 0 ? current : 1; + int pageSize = size != null && size > 0 ? size : 10; + + PagePool pool = getPagePool(); + String key = pool.generateKey(); + Page page = new Page<>(pageNum, pageSize); + pool.put(key, page); + return page; + } + + /** + * 注册一个 Page 对象到池中(供 PageInterceptor 使用) + */ + public static void registerPage(Page page) { + PagePool pool = getPagePool(); + String key = pool.generateKey(); + pool.put(key, page); + } + + /** + * 获取默认的 Page 对象(第一个注册的) + * 保持向后兼容性 + */ + @SuppressWarnings("unchecked") + public static Page getDefaultPage(Class clazz) { + PagePool pool = PAGE_POOL_HOLDER.get(); + if (pool == null || pool.isEmpty()) { + return null; + } + + // 返回第一个 Page 对象 + return (Page) pool.pool.values().stream().findFirst().orElse(null); + } + + /** + * 通过 key 获取 Page 对象 + */ + @SuppressWarnings("unchecked") + public static Page getPageByKey(String key) { + PagePool pool = PAGE_POOL_HOLDER.get(); + if (pool == null) { + return null; + } + return (Page) pool.get(key); + } + + /** + * 获取 PagePool + */ + private static PagePool getPagePool() { + PagePool pool = PAGE_POOL_HOLDER.get(); + if (pool == null) { + pool = new PagePool(); + PAGE_POOL_HOLDER.set(pool); + } + return pool; + } + + /** + * 清除指定 key 的 Page 对象 + */ + public static void clearPage(String key) { + PagePool pool = PAGE_POOL_HOLDER.get(); + if (pool != null) { + pool.remove(key); + } + } + + /** + * 清除所有 + */ public static void clear() { - PAGE_HOLDER.remove(); + PagePool pool = PAGE_POOL_HOLDER.get(); + if (pool != null) { + pool.clear(); + } + PAGE_POOL_HOLDER.remove(); } } \ No newline at end of file diff --git a/src/main/java/com/corewing/app/common/page/PageInterceptor.java b/src/main/java/com/corewing/app/common/page/PageInterceptor.java index 74576e6..12c524b 100644 --- a/src/main/java/com/corewing/app/common/page/PageInterceptor.java +++ b/src/main/java/com/corewing/app/common/page/PageInterceptor.java @@ -8,6 +8,9 @@ import javax.servlet.http.HttpServletResponse; public class PageInterceptor implements HandlerInterceptor { + // 默认的 Page key,用于获取默认分页参数 + public static final String DEFAULT_PAGE_KEY = "default_page"; + @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 默认分页参数 @@ -39,9 +42,16 @@ public class PageInterceptor implements HandlerInterceptor { } } - // 创建 MyBatis-Plus 的 Page 对象并存储到 ThreadLocal + // 创建 MyBatis-Plus 的 Page 对象并注册到 PagePool Page page = new Page<>(pageNum, pageSize); - PageContext.setPage(page); + PageContext.registerPage(page); + + // 将分页参数存储到 request 属性中,方便其他地方使用 + request.setAttribute("pageNum", pageNum); + request.setAttribute("pageSize", pageSize); + request.setAttribute("pageCurrent", pageNum); // 兼容 current 参数名 + request.setAttribute("pageSize", pageSize); + return true; } @@ -50,4 +60,4 @@ public class PageInterceptor implements HandlerInterceptor { // 清除ThreadLocal中的数据,防止内存泄漏 PageContext.clear(); } -} +} \ No newline at end of file