大話分頁(補充)——Threadlocal封裝offSet和pageSize簡化分頁工具類

來源:互聯網
上載者:User

大話分頁一、大話分頁二)的介紹,我認為我想介紹的東西已經介紹完了,不過想精益求精的童鞋可以繼續看本篇文章。

大話分頁一),從實現功能上來說,它沒有任何問題,不過就OO思想來說,這個類是有一點問題的:實現分頁,我們是要用到offSet和pageSize兩個參數,也就是說他們是必須的。可是我們每當使用到分頁的時候,除了穿商務邏輯所需的參數,還需要傳遞這兩個參數。這樣,方法未免有些太過冗長,有沒有一種方法,可以讓它自動傳遞這兩個參數,開發人員不用手動去管它呢?它就是threadlocal。

SystemContext這個類裡面就用Threadlocal封裝了offSet和pageSize兩個參數,並依次定義了他們的get、set和remove方法。

package com.lzq;/** * 使用threadLocal簡化API * 由於封裝的分頁類AbstractPageManager,中的方法參數offSet和pageSize必要但是每個方法都必須寫 * 所以使用threadLocal簡化 * @author lzq * */public class SystemContext {//定義兩個threadLocal變數:offSet和pageSizeprivate static ThreadLocal<Integer> offSet = new ThreadLocal<Integer>();private static ThreadLocal<Integer> pageSize = new ThreadLocal<Integer>();/* * offset :get、set、remove */public static int getOffSet() {Integer os =offSet.get();if (os == null) {return 0;}return os;}public static void setOffSet(int offSetValue) {offSet.set(offSetValue);}public static void removeOffSet(){offSet.remove();} /* * pageSize :get、set、remove */public static int getPageSize() {Integer ps = pageSize.get();if (ps == null) {return 0;}return ps;}public static void setPageSize(int pageSizeValue) {pageSize.set(pageSizeValue);}public static void removePageSize(){pageSize.remove();}}


package com.lzq.web;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import com.lzq.SystemContext;/** * 分頁Filter,用於擷取offSet和pageSize的值 * @author lzq * */public class PageFilter implements Filter {@Overridepublic void destroy() {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {HttpServletRequest httpRequest=(HttpServletRequest)request;SystemContext.setOffSet(getOffSet(httpRequest));SystemContext.setPageSize(getPageSize());try {chain.doFilter(request, response);} //使用完Threadlocal,將其刪除。使用finally確保一定將其刪除finally{SystemContext.removeOffSet();SystemContext.removePageSize();}}/** * 獲得pager.offset參數的值 * @param request * @return */protected int getOffSet(HttpServletRequest request) {int offSet = 0;try {String pageOff = request.getParameter("pager.offset");if (pageOff != null) {offSet =Integer.parseInt(pageOff);}} catch (NumberFormatException e) {e.printStackTrace();}return offSet;}/** * 設定預設每頁大小 * @return */protected int getPageSize() {return 4;}@Overridepublic void init(FilterConfig arg0) throws ServletException {}}

  <filter>  <filter-name>PageFilter</filter-name>  <filter-class>com.lzq.web.PageFilter</filter-class>  </filter>  <filter-mapping>  <filter-name>PageFilter</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>


package com.lzq.manager.impl;import org.hibernate.Query;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.lzq.PageModel;import com.lzq.SystemContext;import com.lzq.manager.SystemException;/** * 簡化的分頁API * @author lzq * */public class AbstractPageManager extends HibernateDaoSupport {/** * 通過調用這些工具方法,簡化API * @param hql * @return */public PageModel searchPaginate(String hql){return searchPaginate(hql,null,SystemContext.getOffSet(),SystemContext.getPageSize());}public PageModel searchPaginate(String hql, Object param){return searchPaginate(hql,new Object[]{param},SystemContext.getOffSet(),SystemContext.getPageSize());}public PageModel searchPaginate(String hql, int offSet, int pageSize){return searchPaginate(hql,null,offSet,pageSize);}public PageModel searchPaginate(String hql, Object param, int offSet, int pageSize){return searchPaginate(hql,new Object[]{param},offSet,pageSize);}}

/** * 測試方法 *  * @param parentId * @return */@Overridepublic PageModel findOrgList(int id) {                PageModel pageModel = null;                String txtSQL =  "from Organization o where o.parent.id= ?";                pageModel = searchPaginate(txtSQL, id);return pageModel; }



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.