多排序字段查询

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;

/**
 * @Author: LJ
 * @Created: 2023/12/22 16:31
 */
@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);

    /**
     * 支持多个字段排序,用法:
     * eg.1, 参数:{order:"name,id", order:"descending,ascending" }。 排序: name desc, id asc
     * eg.2, 参数:{order:"name", order:"descending,ascending" }。 排序: name desc
     * eg.3, 参数:{order:"name,id", order:"descending" }。 排序: name desc
     *
     * @return
     */
    @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;

            // 除了 create_time 和 updateTime 都过滤sql关键字
            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;

/**
 * @Author: LJ
 * @Created: 2023/12/22 16:37
 */
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();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值