此篇博客为上篇博客的优化——将分页逻辑分离,方案是:使用抽象类抽离分页逻辑、使用Filter面向切面、使用ThreadLocal保存分页变量,详细如下:
如果不分离,从Action传到DAO需要必须的条件外,还需要传递从第几行开始、每页多少行这样的参数,为了减少二者之间显式传递的参数个数,使用ThreadLocal对其封装,在DAO层直接使用即可。
package com.tgb.oa; /***** * 使用ThreadLocal封装开始数据行offset和页容量pagesize * @author lidab_000 * */ public class SystemContext { private static ThreadLocal offset = new ThreadLocal(); private static ThreadLocal pagesize = new ThreadLocal(); public static int getOffset(){ Integer os = (Integer)offset.get(); if(os == null){ return 0; } return os; } public static void setOffset(int offsetvalue){ offset.set(offsetvalue); } public static void removeOffset(){ offset.remove(); } public static int getPagesize(){ Integer ps = (Integer)pagesize.get(); if(ps == null){ return Integer.MAX_VALUE; } return ps; } public static void setPagesize(int pagesizevalue){ pagesize.set(pagesizevalue); } public static void removePagesize(){ pagesize.remove(); } }
很多界面都需要分页逻辑,所以在此抽离抽象类,使用时,继承该抽象类即可。
package com.tgb.oa.manager.impl; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.tgb.oa.PagerModel; import com.tgb.oa.SystemContext; import com.tgb.oa.manager.SystemException; public class AbstractManager extends HibernateDaoSupport { public PagerModel searchPaginated(String hql){ return searchPaginated(hql,null,SystemContext.getOffset(),SystemContext.getPagesize()); } //其它类型的复写 public PagerModel searchPaginated(String hql,Object obj,int offset,int pagesize){ return searchPaginated(hql,new Object[]{obj},offset,pagesize); } /** * 根据HQL语句进行分页查询 * @param hql HQL语句 * @param params HQL语句带的多个参数值 * @param offset 从第几条记录开始查询 * @param pagesize 每页显示多少行 * @return */ public PagerModel searchPaginated(String hql,Object[] params,int offset,int pagesize){ //获取记录总数 String countHql = getCountQuery(hql); Query query = getSession().createQuery(countHql); if(params != null && params.length > 0){ for(int i=0; i<params.length; i++){ query.setParameter(i, params[i]); } } int total = ((Long)query.uniqueResult()).intValue(); //获取当前页的结果集 query = getSession().createQuery(hql); if(params != null && params.length > 0){ for(int i=0; i<params.length; i++){ query.setParameter(i, params[i]); } } query.setFirstResult(offset); query.setMaxResults(pagesize); List datas = query.list(); PagerModel pm = new PagerModel(); pm.setTotal(total); pm.setDatas(datas); return pm; } /** * 根据HQL语句,获得查找总记录数的HQL语句 * 如: * select ... from Orgnization o where o.parent is null * 经过转换,可以得到: * select count(*) from Orgnization o where o.parent is null * @param hql * @return */ private String getCountQuery(String hql){ int index = hql.indexOf("from"); if(index != -1){ return "select count(*) " + hql.substring(index); } throw new SystemException("无效的HQL查询语句!"); } }
如:
继承如上抽象类封装的分页逻辑。
public class OrgManagerImpl extends AbstractManager implements OrgManager { public PagerModel findOrgs(int parentId) { //如果parentId=0,则查找顶级机构列表 if(parentId == 0){ return searchPaginated("from Orgnization o where o.parent is null"); } return searchPaginated("from Orgnization o where o.parent.id = ?", parentId); } }
使用Filter,将Threadlocal中封装的两个分页参数初始化及销毁。
public class PagerFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest)request; SystemContext.setOffset(getOffset(httpRequest)); SystemContext.setPagesize(getPagesize(httpRequest)); try{ chain.doFilter(request, response); }finally{ //清空ThreadLocal中的值 SystemContext.removeOffset(); SystemContext.removePagesize(); } } protected int getOffset(HttpServletRequest request){ int offset = 0; try { offset = Integer.parseInt(request.getParameter("pager.offset")); } catch (NumberFormatException ignore) { } return offset; } protected int getPagesize(HttpServletRequest request){ return 10; } public void init(FilterConfig arg0) throws ServletException { } }
<filter> <filter-name>pagerFilter</filter-name> <filter-class>com.tgb.oa.web.PagerFilter</filter-class> </filter> <filter-mapping> <filter-name>pagerFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
当然,这不一定是最好的优化方案,但提供一个思路:何时解耦不相关的业务、如何解开类似的耦合。