大話分頁一、大話分頁二)的介紹,我認為我想介紹的東西已經介紹完了,不過想精益求精的童鞋可以繼續看本篇文章。
大話分頁一),從實現功能上來說,它沒有任何問題,不過就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; }