关于java:工具类之分页查询设计

38次阅读

共计 3092 个字符,预计需要花费 8 分钟才能阅读完成。

设置分页查问 PageUtil 须要用到配置两个工具类:ServletUtil 和 StringUtil 当然这两个工具类不仅仅服务于 PageUtil
1. 分页查问设置 pageSize 和 pageCurrent 两个参数时须要应用 ServletRequest 接口的 getParameter 办法

Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.
You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues.
以字符串模式返回申请参数的值,如果参数不存在,则返回 null。申请参数是与申请一起发送的额定信息。对于 HTTP servlet,参数蕴含在查问字符串或提交的表单数据中。只有当您确定参数只有一个值时,才应该应用此办法。如果参数可能有多个值,则应用 getParameterValues。public String getParameter(String name);

2. 应用 getParameter 办法须要获取 request 申请
我在 ServletUtil 类中应用 getRequest()办法
ServletUtil 类:

package com.cy.pj.common.util;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ServletUtil {
    /*
 * 获取 request
 * */ public static HttpServletRequest getRequest(){return getRequestAttributes().getRequest();}
    /*
 * 获取 response
 * */ public static HttpServletResponse getReponse(){return getRequestAttributes().getResponse();}
    /*
 * 获取 session
 * */ public static HttpSession getSession(){return getRequest().getSession();}
    public static ServletRequestAttributes getRequestAttributes(){
        RequestAttributes attributes=
                RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }
}

2.1web 依赖中的类 RequestContextHolder 的 getRequestAttributes()办法获取申请参数并强转为 ServletRequestAttributes 类型

Return the RequestAttributes currently bound to the thread.
Returns:
the RequestAttributes currently bound to the thread, or null if none bound
返回以后绑定到线程的 RequestAttributes。返回: 以后绑定到线程的 RequestAttributes,如果没有绑定则返回 null
@Nullable
public static RequestAttributes getRequestAttributes() {RequestAttributes attributes = requestAttributesHolder.get();
   if (attributes == null) {attributes = inheritableRequestAttributesHolder.get();
   }
   return attributes;
}

3.PageUtil 类:

package com.cy.pj.common.util;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import javax.servlet.http.HttpServletRequest;
public class PageUtil {public static <T>Page<T> startPage(){
        HttpServletRequest request=
                ServletUtil.getRequest();
        // 页面大小(每页最多显示多少条记录)String pageSizeStr=request.getParameter("pageSize");
        // 以后页码值(要查第几页的数据包)String pageCurrentStr=request.getParameter("pageCurrent");
        System.out.println("pageSize="+pageSizeStr);
        System.out.println("pageCurrent="+pageCurrentStr);
        /*
 * 在此地位调用 pageHelper 中的一个办法启动分页
 * 在我的项目中去增加一个 PageHelper 依赖(后缀是 starter)*/
 Integer pageCurrent=
                StringUtil.isEmpty(pageCurrentStr)?1:Integer.parseInt(pageCurrentStr);
        Integer pageSize=
                StringUtil.isEmpty(pageSizeStr)?10:Integer.parseInt(pageSizeStr);
        return PageHelper.startPage(pageCurrent, pageSize);
    }
}

4. 应用 StringUtil 类的 isEmpty(String str)办法来判断获取的字符串类型的申请参数是否为 null 或者空字符,而后将申请参数通过 Integer 的 parseInt 办法转换为 Integer 类型

StringUtil 类:

package com.cy.pj.common.util;
public class StringUtil {public static boolean isEmpty(String str){return str==null||"".equals(str);
    }
}

正文完
 0