package com.xmkgsy.mybatis;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
@ApiModel("查询条件")
public class PageParams<T> {
@ApiModelProperty(value = "查询参数", required = false)
private T model;
@ApiModelProperty(value = "页面大小", example = "10")
private long size = 10;
@ApiModelProperty(value = "当前页", example = "1")
private long current = 1;
@ApiModelProperty(value = "排序", example = "id")
private String sort = "id";
@ApiModelProperty(value = "排序规则, 默认descending", allowableValues = "descending,ascending", example = "descending")
private String order = "descending";
@ApiModelProperty("扩展参数")
private Map<String, String> map = new HashMap<>(1);
@JsonIgnore
public IPage buildPage() {
return buildPage(true, true);
}
@JsonIgnore
public IPage buildCountPage() {
return buildPage(true, true);
}
@JsonIgnore
public IPage buildCountPage(boolean camel2Underline) {
return buildPage(true, camel2Underline);
}
@JsonIgnore
public IPage buildNotCountPage() {
return buildPage(false, true);
}
@JsonIgnore
public IPage buildNotCountPage(boolean camel2Underline) {
return buildPage(false, camel2Underline);
}
@JsonIgnore
private IPage buildPage(boolean isSearchCount, boolean camel2Underline) {
PageParams params = this;
if (StrUtil.isEmpty(params.getSort())) {
Page page = new Page(params.getCurrent(), params.getSize(), isSearchCount);
return page;
}
Page page = new Page(params.getCurrent(), params.getSize(), isSearchCount);
List<OrderItem> orders = new ArrayList<>();
String[] sortArr = StrUtil.split(params.getSort(), ",");
String[] orderArr = StrUtil.split(params.getOrder(), ",");
int len = sortArr.length < orderArr.length ? sortArr.length : orderArr.length;
for (int i = 0; i < len; i++) {
String humpSort = sortArr[i];
String underlineSort = camel2Underline ? StrUtil.toUnderlineCase(humpSort) : humpSort;
if (!StrUtil.equalsAny(humpSort, "create_time", "update_time")) {
underlineSort = AntiSqlFilter.getSafeValue(underlineSort);
}
orders.add("ascending".equals(orderArr[i]) ? OrderItem.asc(underlineSort) : OrderItem.desc(underlineSort));
}
page.setOrders(orders);
return page;
}
}
package com.xmkgsy.mybatis;
import cn.hutool.core.util.ArrayUtil;
import java.util.HashMap;
import java.util.Map;
public class AntiSqlFilter {
private static final String[] KEY_WORDS = {";", "\"", "\'", "/*", "*/", "--", "exec",
"select", "update", "delete", "insert", "alter", "drop", "create", "shutdown"};
public static Map<String, String[]> getSafeParameterMap(Map<String, String[]> parameterMap) {
Map<String, String[]> map = new HashMap<>(parameterMap.size());
for (String key : parameterMap.keySet()) {
String[] oldValues = parameterMap.get(key);
map.put(key, getSafeValues(oldValues));
}
return map;
}
public static String[] getSafeValues(String[] oldValues) {
if (ArrayUtil.isNotEmpty(oldValues)) {
String[] newValues = new String[oldValues.length];
for (int i = 0; i < oldValues.length; i++) {
newValues[i] = getSafeValue(oldValues[i]);
}
return newValues;
}
return null;
}
public static String getSafeValue(String oldValue) {
if (oldValue == null || "".equals(oldValue)) {
return oldValue;
}
StringBuilder sb = new StringBuilder(oldValue);
String lowerCase = oldValue.toLowerCase();
for (String keyWord : KEY_WORDS) {
int x;
while ((x = lowerCase.indexOf(keyWord)) >= 0) {
if (keyWord.length() == 1) {
sb.replace(x, x + 1, " ");
lowerCase = sb.toString().toLowerCase();
continue;
}
sb.delete(x, x + keyWord.length());
lowerCase = sb.toString().toLowerCase();
}
}
return sb.toString();
}
}