ADD file via upload

main
pe9vkn3zc 5 months ago
parent 2d0cc10483
commit e79c396d64

@ -0,0 +1,236 @@
package com.utils;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.plugins.Page;
/**
*
*
* MyBatis-Plus
*
*
* 1. LinkedHashMap
* 2. JQPageInfoMap
* 3. SQL
* 4.
*
*
* - MyBatis-PlusPage
* - SQL
* -
*/
public class Query<T> extends LinkedHashMap<String, Object> {
// 序列化版本ID
private static final long serialVersionUID = 1L;
/**
* mybatis-plus
*/
private Page<T> page;
/**
*
*/
private int currPage = 1;
/**
*
*/
private int limit = 10;
/**
* JQPageInfo
* JQPageInfo
*
* @param pageInfo
*/
public Query(JQPageInfo pageInfo) {
// 分页参数解析
if (pageInfo.getPage() != null) {
currPage = pageInfo.getPage(); // 设置当前页码
}
if (pageInfo.getLimit() != null) {
limit = pageInfo.getLimit(); // 设置每页条数
}
// 防止SQL注入因为sidx、order是通过拼接SQL实现排序的会有SQL注入风险
String sidx = SQLFilter.sqlInject(pageInfo.getSidx()); // 排序字段安全过滤
String order = SQLFilter.sqlInject(pageInfo.getOrder()); // 排序方式安全过滤
// mybatis-plus分页对象初始化
this.page = new Page<>(currPage, limit);
// 排序参数设置
if (StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)) {
this.page.setOrderByField(sidx); // 设置排序字段
this.page.setAsc("ASC".equalsIgnoreCase(order)); // 设置排序方向
}
}
/**
* Map
* Map
*
* @param params Map
*/
public Query(Map<String, Object> params) {
// 将参数添加到当前Map中
this.putAll(params);
// 分页参数解析
if (params.get("page") != null) {
// 解析当前页码
currPage = Integer.parseInt((String) params.get("page"));
}
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注入排序字段和排序方向的安全过滤
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分页对象初始化
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
*
* @return MyBatis-Plus
*/
public Page<T> getPage() {
return page;
}
/**
*
*
* @return
*/
public int getCurrPage() {
return currPage;
}
/**
*
*
* @return
*/
public int getLimit() {
return limit;
}
// 可以添加的增强方法:
/**
*
*
* @return
*/
public String getSidx() {
return (String) this.get("sidx");
}
/**
*
*
* @return ASC/DESC
*/
public String getOrder() {
return (String) this.get("order");
}
/**
* SQL LIMIT
*
* @return
*/
public int getOffset() {
return (int) this.get("offset");
}
/**
*
*
* @return
*/
public boolean hasSort() {
return StringUtils.isNotBlank(getSidx()) && StringUtils.isNotBlank(getOrder());
}
/**
* SQL
*
* @return SQL"create_time DESC"
*/
public String getOrderByClause() {
if (hasSort()) {
return getSidx() + " " + getOrder().toUpperCase();
}
return null;
}
/**
*
*
* @param key
* @param value
* @return
*/
public Query<T> addCondition(String key, Object value) {
this.put(key, value);
return this;
}
/**
*
*
* @param conditions Map
* @return
*/
public Query<T> addConditions(Map<String, Object> conditions) {
this.putAll(conditions);
return this;
}
/**
*
*
* @return
*/
public boolean isValid() {
return currPage > 0 && limit > 0 && limit <= 1000; // 限制每页最大1000条
}
/**
* 10
*
* @return
*/
public static <T> Query<T> defaultQuery() {
Map<String, Object> params = new LinkedHashMap<>();
params.put("page", "1");
params.put("limit", "10");
return new Query<>(params);
}
}
Loading…
Cancel
Save