9

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

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

如您所见,我没有使用这两个参数,在 SQL 世界中,我将其用作 LIMIT 和 OFFSET。

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

我想我必须做一个query.startCursor(<my_"from"_parameter>)

然后迭代“TO”次,即页面大小。

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

4

2 回答 2

15

来自文档:游标让您在查询结果集中获取“检查点”,将检查点存储在其他地方,然后从您迟到的地方恢复

由于您只需要限制/偏移量,因此您必须使用Objectify Querylimit()offset()方法。喜欢:

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);
于 2011-08-11T14:09:50.840 回答
0

我只想确保您的代码中没有任何错误,因为您可以复制并粘贴 Igor Artamonov 代码。这是来自 Objectify Wiki 的更简洁的代码,错误更少,并且有一些文档:

// create the query and set the limit to 1000
Query<Car> query = ofy().load().type(Car.class).limit(1000);

// Here you get the cursor (if exists) from the request
// For the first request, i-e the first page, this parameter(cursor) will be null
String cursorStr = request.getParameter("cursor");

// Here you check if cursor is not null and not empty
// If so, we start our query from the last check point
if (cursorStr != null && !cursorStr.isEmpty())
    query = query.startAt(Cursor.fromWebSafeString(cursorStr));

// We need this variable to know when we have been loaded all the entries
boolean remaining = false;
QueryResultIterator<Car> iterator = query.iterator();
while (iterator.hasNext()) {
    Car car = iterator.next();

    ... // your code here

    // We have found entries, so we set this variable to true.
    // That means, we have probably another page to fetch
    remaining = true;
}

// If we have found entries, we send the last check point
if (remaining) {
    // we take the last check point by calling "toWebSafeString()" from the iterator's cursor
    Cursor cursor = iterator.getCursor();
    Queue queue = QueueFactory.getDefaultQueue();
    queue.add(url("/pathToThisServlet").param("cursor", cursor.toWebSafeString()));
}
于 2018-10-13T20:37:49.453 回答