All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.opencodes.vo.Query Maven / Gradle / Ivy

package cn.opencodes.vo;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import cn.opencodes.utils.StringUtils;
import cn.opencodes.xss.SQLFilter;

/**
 * 查询参数
 * @author HJ
 */
public class Query extends LinkedHashMap {
	private static final long serialVersionUID = 1L;
	//当前页码
    private int currPage = 1;
    //每页条数
    private int pageSize = 10;
    //起始页
	private int pageStart;
	//排序
	private String orderBy;
	
	
	public Query(Object ...objs){
		Map params = new HashMap<>();
		if (objs != null) {
			for (int i = 0; i < objs.length; i=i+2) {
				params.put(objs[i].toString(), objs[i+1]);
			}
			processForMap(params);
		}
	}

    public Query(Map params){
    	processForMap(params);
    }

	private void processForMap(Map params) {
		//没有值不做处理
		if (params==null || params.size()<1) {
			return;
		}
		//获取字符串两边空格
    	for (Map.Entry e : params.entrySet()) {
    		if (e.getValue() != null && e.getValue() instanceof String) {
    			this.put(e.getKey(), e.getValue().toString().trim());
			}else {
				this.put(e.getKey(), e.getValue());
			}
		}
        //处理分页参数
        this.currPage = getInt(params.get("currPage"), this.currPage);
        this.pageSize = getInt(params.get("pageSize"), this.pageSize);
        this.pageStart = (currPage - 1) * pageSize;
        this.put("pageStart", pageStart);
        this.put("pageSize", pageSize);
        //防止SQL注入(因为sidx、order是通过拼接SQL实现排序的,会有SQL注入风险)
        orderBy = (String)params.get("orderBy");
        if(StringUtils.isNotBlank(orderBy)){
        	orderBy = SQLFilter.sqlInject(orderBy);
            this.put("orderBy", orderBy);
        }else {
        	orderBy = null;
		}
	}

    
    public Query put(String key, Object value) {
		//防止排序注入
		if (key.equals("orderBy") && value!=null && StringUtils.isNotBlank(value.toString())) {
			value = SQLFilter.sqlInject(value.toString());
		}
		super.put(key, value);
		return this;
	}
    
    public int getCurrPage() {
        return currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageStart() {
		return pageStart;
	}

	public void setPageStart(int pageStart) {
		this.pageStart = pageStart;
	}
	
    private int getInt(Object param, int defalut){
    	int v = defalut;
    	try {
    		if (param != null) {
    			v = Integer.parseInt(param.toString());
    		}
		} catch (Exception e) {
		}
    	return v;
    }

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy