|
|
|
@ -0,0 +1,110 @@
|
|
|
|
|
// 声明该类所在的包为 com.utils
|
|
|
|
|
package com.utils;
|
|
|
|
|
|
|
|
|
|
// 导入 LinkedHashMap 类,用于存储键值对,且能保持插入顺序
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
// 导入 Map 接口,用于定义键值对存储的通用规范
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
// 导入 Apache Commons Lang3 库中的 StringUtils 类,用于字符串操作
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
|
|
|
|
// 导入 MyBatis-Plus 框架的 Page 类,用于分页操作
|
|
|
|
|
import com.baomidou.mybatisplus.plugins.Page;
|
|
|
|
|
|
|
|
|
|
// 定义一个泛型类 Query,继承自 LinkedHashMap,用于处理查询参数
|
|
|
|
|
public class Query<T> extends LinkedHashMap<String, Object> {
|
|
|
|
|
// 定义序列化版本号,用于序列化和反序列化时的版本验证
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
// 定义 MyBatis-Plus 的 Page 对象,用于分页操作
|
|
|
|
|
private Page<T> page;
|
|
|
|
|
// 定义当前页码,默认为 1
|
|
|
|
|
private int currPage = 1;
|
|
|
|
|
// 定义每页显示的记录数,默认为 10
|
|
|
|
|
private int limit = 10;
|
|
|
|
|
|
|
|
|
|
// 构造函数,根据 JQPageInfo 对象初始化查询参数
|
|
|
|
|
public Query(JQPageInfo pageInfo) {
|
|
|
|
|
// 处理分页参数
|
|
|
|
|
// 如果 JQPageInfo 中的 page 不为空,则更新当前页码
|
|
|
|
|
if (pageInfo.getPage() != null) {
|
|
|
|
|
currPage = pageInfo.getPage();
|
|
|
|
|
}
|
|
|
|
|
// 如果 JQPageInfo 中的 limit 不为空,则更新每页记录数
|
|
|
|
|
if (pageInfo.getLimit() != null) {
|
|
|
|
|
limit = pageInfo.getLimit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 防止 SQL 注入,对排序字段和排序顺序进行过滤
|
|
|
|
|
String sidx = SQLFilter.sqlInject(pageInfo.getSidx());
|
|
|
|
|
String order = SQLFilter.sqlInject(pageInfo.getOrder());
|
|
|
|
|
|
|
|
|
|
// 初始化 MyBatis-Plus 的 Page 对象
|
|
|
|
|
this.page = new Page<>(currPage, limit);
|
|
|
|
|
|
|
|
|
|
// 设置排序信息
|
|
|
|
|
if (StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)) {
|
|
|
|
|
// 设置排序字段
|
|
|
|
|
this.page.setOrderByField(sidx);
|
|
|
|
|
// 判断排序顺序是否为升序
|
|
|
|
|
this.page.setAsc("ASC".equalsIgnoreCase(order));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造函数,根据 Map 类型的参数初始化查询参数
|
|
|
|
|
public Query(Map<String, Object> params) {
|
|
|
|
|
// 将传入的参数添加到当前对象中
|
|
|
|
|
this.putAll(params);
|
|
|
|
|
|
|
|
|
|
// 处理分页参数
|
|
|
|
|
// 如果参数中包含 "page",则更新当前页码
|
|
|
|
|
if (params.get("page") != null) {
|
|
|
|
|
currPage = Integer.parseInt(String.valueOf(params.get("page")));
|
|
|
|
|
}
|
|
|
|
|
// 如果参数中包含 "limit",则更新每页记录数
|
|
|
|
|
if (params.get("limit") != null) {
|
|
|
|
|
limit = Integer.parseInt(String.valueOf(params.get("limit")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算偏移量并添加到参数中
|
|
|
|
|
this.put("offset", (currPage - 1) * limit);
|
|
|
|
|
// 添加当前页码到参数中
|
|
|
|
|
this.put("page", currPage);
|
|
|
|
|
// 添加每页记录数到参数中
|
|
|
|
|
this.put("limit", limit);
|
|
|
|
|
|
|
|
|
|
// 防止 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 的 Page 对象
|
|
|
|
|
this.page = new Page<>(currPage, limit);
|
|
|
|
|
|
|
|
|
|
// 设置排序信息
|
|
|
|
|
if (StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)) {
|
|
|
|
|
// 设置排序字段
|
|
|
|
|
this.page.setOrderByField(sidx);
|
|
|
|
|
// 判断排序顺序是否为升序
|
|
|
|
|
this.page.setAsc("ASC".equalsIgnoreCase(order));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取 MyBatis-Plus 的 Page 对象
|
|
|
|
|
public Page<T> getPage() {
|
|
|
|
|
return page;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前页码
|
|
|
|
|
public int getCurrPage() {
|
|
|
|
|
return currPage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取每页记录数
|
|
|
|
|
public int getLimit() {
|
|
|
|
|
return limit;
|
|
|
|
|
}
|
|
|
|
|
}
|