---
title: "google app: 使用时间类型字段做分页"
author: "fosa0989"
date: "2009-11-20T17:30:06Z"
tags: []
categories: ["编程语言"]
slug: "google-app"
draft: false
lastmod: "2009-11-20T17:30:06Z"
id: 521584
url: "https://hchen229.iteye.com/blog/521584"
---

实现思路是用postDate字段进行排序，使用一个时间点限制取出一页数据。

如果是向下翻页  用postDate &lt; offset  按照postDate desc排序的方法取出下一页数据。

如果向上翻页 用postDate &gt; offset 按照postDate asc 排序的方法取出上一页数据。

存在的问题是如果时间相同的两个记录可能会有一个读取不到。

 

1 Model

```
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class BlogItem {
	@PrimaryKey    
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	@Persistenti
	private String itemTitle;
	@Persistent
	private Text content;
	@Persistent
	private Date postDate;
	@Persistent
	private Date lastUpdateDate;
	@Persistent
	private Long categoryId;
	
	....
}
```

 2 DAO

```
	public List<BlogItem> pageList(long offset,boolean down,
			long catId, int countPerPage, boolean loadContent){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<BlogItem> bcs;
		Query query = pm.newQuery(BlogItem.class);		
		String filter = "";
		query.setRange(0, countPerPage);
		
		if(catId > 0){
			filter +=(" categoryId==" + catId + " ");
		}
		
		if(offset > 0){
			Date off = new Date(offset);
			if(filter.length()>0) filter +=" && ";	
			if(down){
				filter += (" postDate < offDate ");
				query.setOrdering(" postDate desc");
			}else{
				filter += (" postDate > offDate ");
				query.setOrdering(" postDate asc");
			}

			query.setFilter(filter);			
			query.declareImports("import java.util.Date");
			query.declareParameters("Date offDate");			
			bcs = (List<BlogItem>)query.execute(off);
		}else{
			if(filter.length()>0) query.setFilter(filter);	
			query.setOrdering(" postDate desc");
			bcs = (List<BlogItem>)query.execute();
		}
		
		if(loadContent && bcs != null && bcs.size()>0){
			for(BlogItem bi : bcs)
				bi.getContent();
		}
		
		if(offset > 0 && down == false){
			List<BlogItem> tbcs = new ArrayList<BlogItem>();
			for(BlogItem bi : bcs){
				tbcs.add(0, bi);
			}
			return tbcs;
		}
		
		return bcs;
	}
```

 3 Controller

```
	@RequestMapping(value ="/welcome" ,method = RequestMethod.GET)
    public String welcome(Model model,HttpServletRequest request) {
		List<BlogItem> bcs = blogItemService.pageList(-1,true,-1, COUNT_PER_PAGE,true);
        model.addAttribute("blogItems", bcs);
        if(bcs.size() == COUNT_PER_PAGE){
        	BlogItem last = (BlogItem)bcs.get(bcs.size()-1);
        	model.addAttribute("last",last.getPostDate().getTime());
        }
        setup(model);
		return "public/index";
    }

	@RequestMapping(value="/page/{direc}/{offset}",method = RequestMethod.GET)
    public String list(@PathVariable String direc,@PathVariable long offset,Model model,HttpServletRequest request) {
		boolean down = false;
		if(direc.equals("down"))
			down=true;		
		List<BlogItem> bcs = blogItemService.pageList(offset,down,-1, COUNT_PER_PAGE,true);
        model.addAttribute("blogItems", bcs);
        if(bcs.size() == COUNT_PER_PAGE){
        	BlogItem first = (BlogItem)bcs.get(0);        	
        	BlogItem last = (BlogItem)bcs.get(bcs.size()-1);
        	model.addAttribute("first",first.getPostDate().getTime());
        	model.addAttribute("last",last.getPostDate().getTime());
        }else if(bcs.size() > 0){
        	if(down){
            	BlogItem first = (BlogItem)bcs.get(0);        	
            	model.addAttribute("first",first.getPostDate().getTime());        		
        	}else{
        		BlogItem last = (BlogItem)bcs.get(bcs.size()-1);
        		model.addAttribute("last",last.getPostDate().getTime());
        	}
        }else{
        	return "redirect:/welcome";
        }        
        setup(model);
        return "public/index";
    }
```

 网址：  [http://novel-fun.appspot.com](http://novel-fun.appspot.com "http://novel-fun.appspot.com")