google app: 使用时间类型字段做分页

本文介绍了一种基于时间戳的分页查询实现方法,利用postDate字段进行排序,并通过指定的时间点来获取上一页或下一页的数据。这种方法适用于博客等按时间顺序展示内容的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

如果向上翻页 用postDate > 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值