2008-07-16
对分页的封装
package com.bjsxt.oa.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.bjsxt.oa.SystemContext;
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 {
}
}
package com.bjsxt.oa;
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.bjsxt.oa.manager.impl;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bjsxt.oa.PagerModel;
import com.bjsxt.oa.SystemContext;
import com.bjsxt.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 param){
return searchPaginated(hql,new Object[]{param},SystemContext.getOffset(),SystemContext.getPagesize());
}
public PagerModel searchPaginated(String hql,Object[] params){
return searchPaginated(hql,params,SystemContext.getOffset(),SystemContext.getPagesize());
}
public PagerModel searchPaginated(String hql,int offset,int pagesize){
return searchPaginated(hql,null,offset,pagesize);
}
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查询语句!");
}
}
package com.bjsxt.oa;
import java.util.List;
public class PagerModel {
/**
* 总记录数
*/
private int total;
/**
* 当前页结果集
*/
private List datas;
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
发表评论
- 浏览: 30496 次
- 性别:

- 来自: 大连

- 详细资料
搜索本博客
我的相册
strutsWorkflow
共 3 张
共 3 张
最近加入圈子
链接
- 冰封太子
- 个人博客
- eclipse-API 函数
- eclipse-SWT资源
- Eclipse-下载
- hibernate-官方网站
- html-教程与代码
- html-教程与代码
- html-蓝色理想
- html-语言教程
- Javascript-基础教程
- PowerDesigner视频
- 流星絮语 JAVA学习笔记
- AppServNetword
- JDBC、SQLJ 的Oracle 驱动
- BeanSoft's Java Blog
- 面试时候经常会问的一些问题(不断补充中)
- Hibernate 3入門
- java学习
- 上帝的笔记本
- xiao
- 视频
- 尚学堂
- Intellij Idea下的iBatis
- vssplugin
- XDoclet最新下载包
- prototype.js官方
- 在线视频
- Ruby
- Aptana Studio
- 面试题集合
- 徐风城
- 最好的java源码
- java blog
最新评论
-
JSP购物车实例讲解
能不能写写struts2做成的购物车模块,把购物车放在session里面?
-- by chucai -
Struts 源码学习之Action ...
initChain()真是看不明白用了Commons Chain来搞了个CoR模 ...
-- by dvdface -
IntelliJ IDEA使用技巧一 ...
学习了!
-- by ziscloud -
ssh分页实例
else if ("next".equals(state)) 可以无止境的n ...
-- by figeonline -
js应用
这文章说什么的啊?能不能给个效果图啊?晕哎
-- by zhangzldipan






评论排行榜