diff --git a/back/src/main/java/com/utils/Query.java b/back/src/main/java/com/utils/Query.java index eb624a78..b6cd37fe 100644 --- a/back/src/main/java/com/utils/Query.java +++ b/back/src/main/java/com/utils/Query.java @@ -1,4 +1,3 @@ - package com.utils; import java.util.LinkedHashMap; @@ -9,90 +8,114 @@ import org.apache.commons.lang3.StringUtils; import com.baomidou.mybatisplus.plugins.Page; /** - * 查询参数 + * 查询参数类,继承自LinkedHashMap,用于封装查询相关的参数信息, + * 并且处理分页和排序等操作,同时对可能存在SQL注入风险的参数进行过滤。 */ public class Query extends LinkedHashMap { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; /** - * mybatis-plus分页参数 + * mybatis-plus分页参数对象,用于进行分页查询操作。 */ private Page page; /** - * 当前页码 + * 当前页码,默认为1。 */ private int currPage = 1; /** - * 每页条数 + * 每页条数,默认为10。 */ private int limit = 10; + /** + * 构造函数,根据JQPageInfo对象来初始化查询参数。 + * + * @param pageInfo 包含分页和排序信息的JQPageInfo对象。 + */ public Query(JQPageInfo pageInfo) { - //分页参数 - if(pageInfo.getPage()!= null){ + // 处理分页参数 + if (pageInfo.getPage() != null) { currPage = pageInfo.getPage(); } - if(pageInfo.getLimit()!= null){ + if (pageInfo.getLimit() != null) { limit = pageInfo.getLimit(); } - - //防止SQL注入(因为sidx、order是通过拼接SQL实现排序的,会有SQL注入风险) + // 防止SQL注入(因为sidx、order是通过拼接SQL实现排序的,会有SQL注入风险) String sidx = SQLFilter.sqlInject(pageInfo.getSidx()); String order = SQLFilter.sqlInject(pageInfo.getOrder()); - - //mybatis-plus分页 + // 创建mybatis-plus分页对象 this.page = new Page<>(currPage, limit); - //排序 - if(StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)){ + // 进行排序设置 + if (StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)) { this.page.setOrderByField(sidx); this.page.setAsc("ASC".equalsIgnoreCase(order)); } } - - - public Query(Map params){ + + /** + * 构造函数,根据Map类型的参数来初始化查询参数。 + * + * @param params 包含查询参数的Map,可能包含分页、排序等信息。 + */ + public Query(Map params) { + // 将传入的参数Map中的所有键值对放入当前对象中 this.putAll(params); - //分页参数 - if(params.get("page") != null){ - currPage = Integer.parseInt((String)params.get("page")); + // 处理分页参数 + if (params.get("page") != null) { + currPage = Integer.parseInt((String) params.get("page")); } - if(params.get("limit") != null){ - limit = Integer.parseInt((String)params.get("limit")); + if (params.get("limit") != null) { + limit = Integer.parseInt((String) params.get("limit")); } + // 计算偏移量,并将分页相关参数放入当前对象中 this.put("offset", (currPage - 1) * limit); this.put("page", currPage); this.put("limit", limit); - //防止SQL注入(因为sidx、order是通过拼接SQL实现排序的,会有SQL注入风险) - String sidx = SQLFilter.sqlInject((String)params.get("sidx")); - String order = SQLFilter.sqlInject((String)params.get("order")); + // 防止SQL注入(因为sidx、order是通过拼接SQL实现排序的,会有SQL注入风险) + String sidx = SQLFilter.sqlInject((String) params.get("sidx")); + String order = SQLFilter.sqlInject((String) params.get("order")); this.put("sidx", sidx); this.put("order", order); - //mybatis-plus分页 + // 创建mybatis-plus分页对象 this.page = new Page<>(currPage, limit); - //排序 - if(StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)){ + // 进行排序设置 + if (StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)) { this.page.setOrderByField(sidx); this.page.setAsc("ASC".equalsIgnoreCase(order)); } - } + /** + * 获取mybatis-plus的分页对象。 + * + * @return mybatis-plus的Page对象。 + */ public Page getPage() { return page; } + /** + * 获取当前页码。 + * + * @return 当前页码。 + */ public int getCurrPage() { return currPage; } + /** + * 获取每页条数。 + * + * @return 每页条数。 + */ public int getLimit() { return limit; } -} +} \ No newline at end of file