Compare commits

...

2 Commits

Author SHA1 Message Date
MichaelWin
3ae7b9ac52 【优化】产品接口 2026-01-28 11:50:44 +08:00
MichaelWin
7d51a13dd7 【优化】分页 2026-01-28 09:22:46 +08:00
9 changed files with 153 additions and 32 deletions

View File

@@ -1,23 +1,134 @@
package com.corewing.app.common.page; package com.corewing.app.common.page;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.HashMap;
import java.util.Map;
public class PageContext { public class PageContext {
private static final ThreadLocal<Page<?>> PAGE_HOLDER = new ThreadLocal<>(); private static final ThreadLocal<PagePool> PAGE_POOL_HOLDER = new ThreadLocal<>();
// 设置分页对象 private static class PagePool {
public static void setPage(Page<?> page) { private final Map<String, Page<?>> pool = new HashMap<>();
PAGE_HOLDER.set(page); 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 <T> Page<T> getPage(Class<T> clazz) { public static <T> Page<T> getPage(Class<T> clazz) {
return (Page<T>) PAGE_HOLDER.get(); PagePool pool = getPagePool();
String key = pool.generateKey();
Page<T> page = new Page<>();
pool.put(key, page);
return page;
} }
// 清除线程变量 /**
* 获取带有默认分页参数的 Page 对象
*/
public static <T> Page<T> getPage(Class<T> 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<T> page = new Page<>(pageNum, pageSize);
pool.put(key, page);
return page;
}
/**
* 注册一个 Page 对象到池中(供 PageInterceptor 使用)
*/
public static <T> void registerPage(Page<T> page) {
PagePool pool = getPagePool();
String key = pool.generateKey();
pool.put(key, page);
}
/**
* 获取默认的 Page 对象(第一个注册的)
* 保持向后兼容性
*/
@SuppressWarnings("unchecked")
public static <T> Page<T> getDefaultPage(Class<T> clazz) {
PagePool pool = PAGE_POOL_HOLDER.get();
if (pool == null || pool.isEmpty()) {
return null;
}
// 返回第一个 Page 对象
return (Page<T>) pool.pool.values().stream().findFirst().orElse(null);
}
/**
* 通过 key 获取 Page 对象
*/
@SuppressWarnings("unchecked")
public static <T> Page<T> getPageByKey(String key) {
PagePool pool = PAGE_POOL_HOLDER.get();
if (pool == null) {
return null;
}
return (Page<T>) 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() { public static void clear() {
PAGE_HOLDER.remove(); PagePool pool = PAGE_POOL_HOLDER.get();
if (pool != null) {
pool.clear();
}
PAGE_POOL_HOLDER.remove();
} }
} }

View File

@@ -8,6 +8,9 @@ import javax.servlet.http.HttpServletResponse;
public class PageInterceptor implements HandlerInterceptor { public class PageInterceptor implements HandlerInterceptor {
// 默认的 Page key用于获取默认分页参数
public static final String DEFAULT_PAGE_KEY = "default_page";
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 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); 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; return true;
} }
@@ -50,4 +60,4 @@ public class PageInterceptor implements HandlerInterceptor {
// 清除ThreadLocal中的数据防止内存泄漏 // 清除ThreadLocal中的数据防止内存泄漏
PageContext.clear(); PageContext.clear();
} }
} }

View File

@@ -25,8 +25,12 @@ public class BizProduct extends CommonEntity {
private String hotStatus; private String hotStatus;
private String showType;
private String content; private String content;
private String tag;
private String detailUrl; private String detailUrl;
private BigDecimal amount; private BigDecimal amount;
@@ -42,6 +46,4 @@ public class BizProduct extends CommonEntity {
@TableField(exist = false) @TableField(exist = false)
private String categoryId; private String categoryId;
} }

View File

@@ -8,6 +8,8 @@ import com.corewing.app.common.base.CommonEntity;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.util.List;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Data @Data
@TableName("biz_product_category") @TableName("biz_product_category")
@@ -34,5 +36,4 @@ public class BizProductCategory extends CommonEntity {
@TableField(exist = false) @TableField(exist = false)
Page<BizProduct> productPage; Page<BizProduct> productPage;
} }

View File

@@ -11,5 +11,5 @@ import java.util.List;
@Mapper @Mapper
public interface BizProductCategoryMapper extends BaseMapper<BizProductCategory> { public interface BizProductCategoryMapper extends BaseMapper<BizProductCategory> {
List<BizProductCategory> list(@Param("productCategoryRequest") ProductCategoryRequest productCategoryRequest);
} }

View File

@@ -1,5 +1,7 @@
package com.corewing.app.service.impl; package com.corewing.app.service.impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corewing.app.dto.website.ProductCategoryRequest; import com.corewing.app.dto.website.ProductCategoryRequest;
@@ -25,13 +27,17 @@ public class BizProductCategroyServiceImpl extends ServiceImpl<BizProductCategor
@Override @Override
public List<BizProductCategory> list(ProductCategoryRequest productCategoryRequest) { public List<BizProductCategory> list(ProductCategoryRequest productCategoryRequest) {
List<BizProductCategory> bizProductCategory = bizProductCategoryMapper.list(productCategoryRequest); LambdaQueryWrapper<BizProductCategory> queryWrapper = new LambdaQueryWrapper<>();
bizProductCategory.forEach(item -> { queryWrapper.like(StrUtil.isNotBlank(productCategoryRequest.getSearchKey()), BizProductCategory::getCategoryTitle, productCategoryRequest.getSearchKey());
ProductPageRequest query = new ProductPageRequest(); List<BizProductCategory> bizProductCategories = bizProductCategoryMapper.selectList(queryWrapper);
query.setCategoryId(item.getId());
Page<BizProduct> page = bizProductService.page(query); bizProductCategories.forEach(item -> {
ProductPageRequest productPageRequest = new ProductPageRequest();
productPageRequest.setCategoryId(item.getId());
Page<BizProduct> page = bizProductService.page(productPageRequest);
item.setProductPage(page); item.setProductPage(page);
}); });
return bizProductCategory;
return bizProductCategories;
} }
} }

View File

@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corewing.app.common.page.PageContext; import com.corewing.app.common.page.PageContext;
import com.corewing.app.dto.website.ProductPageRequest; import com.corewing.app.dto.website.ProductPageRequest;
import com.corewing.app.entity.AppModel;
import com.corewing.app.entity.BizProduct; import com.corewing.app.entity.BizProduct;
import com.corewing.app.mapper.BizProductMapper; import com.corewing.app.mapper.BizProductMapper;
import com.corewing.app.service.BizProductService; import com.corewing.app.service.BizProductService;

View File

@@ -9,15 +9,6 @@
FROM biz_product_category pc FROM biz_product_category pc
</sql> </sql>
<!-- 分页查询 -->
<select id="list" resultType="com.corewing.app.entity.BizProductCategory">
<include refid="selectVOSql"/>
<where>
<if test="productCategoryRequest.searchKey != null and productCategoryRequest.searchKey != ''">
and pc.category_title like CONCAT('%', #{productCategoryRequest.searchKey}, '%')
</if>
</where>
</select>
</mapper> </mapper>

View File

@@ -21,6 +21,7 @@
<if test="productPageRequest.searchKey != null and productPageRequest.searchKey != ''"> <if test="productPageRequest.searchKey != null and productPageRequest.searchKey != ''">
and p.title like CONCAT('%', #{productPageRequest.searchKey}, '%') and p.title like CONCAT('%', #{productPageRequest.searchKey}, '%')
</if> </if>
and delete_flag = 'NOT_DELETE'
</where> </where>
</select> </select>