使用游标对象化分页 [英] Objectify paging with Cursors

查看:173
本文介绍了使用游标对象化分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的RPC服务中有这个方法:

I have this method in my RPC service:

@Override
    public Entrata[] getEntrate(int from, int to) {
        List<Entrata> data = entrateDao.list();
        return data.toArray(new Entrata[0]);
    }

正如你所看到的,我不使用这两个参数,一个SQL世界,我将使用LIMIT和OFFSET。

As you can see, I am not using the two parameters, which, in a SQL world, I would use as LIMIT and OFFSET.

现在还不清楚我要做什么,我开始阅读:
http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#Cursors

It's not completely clear what I have to do now, I started reading this: http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#Cursors

我想我必须做一个query.startCursor()

I think I have to do a query.startCursor()

然后迭代TO页面大小。

Then iterate for "TO" times, the page size.

好吧?你能帮我一些片段吗? :)

All right? Can you help me with some snippets? :)

推荐答案

游标让您在查询结果集中使用检查点,存储检查点

From docs: Cursors let you take a "checkpoint" in a query result set, store the checkpoint elsewhere, and then resume from where you left off late

由于您只需要限制/抵消,您必须使用 limit ) offset()的方法。喜欢:

As you need just limit/offset, you have to use limit() and offset() method of Objectify Query. Like:

ob.query(Entrata.class).limit(to - from).offset(from)

或者,当您有游标时:

String cursor = // get it from request
Query<Entrata> query = ob.query(Entrata.class);
Query q = query.startCursor(Cursor.fromWebSafeString(cursor));
q.limit(x);
QueryResultIterator<Entrate> iterator = query.iterator()
List<Entrate> data = // fetch data
String newCursor = iterrator.getStartCursor().toWebSafeString()
return new EntrataListWithCursor(data, cursor);

这篇关于使用游标对象化分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆