gpt4 book ai didi

java - App Engine Java : syntax for setting query limit and start offset.

转载 作者:行者123 更新时间:2023-11-30 07:32:02 27 4
gpt4 key购买 nike

我在 GAE 数据存储中有一个游戏列表,我想查询固定数量的游戏,从某个偏移量开始,即从 ID 为“75”的表单条目开始获取接下来的 25 个游戏。

PersistenceManager pm = PMF.get().getPersistenceManager(); // from Google examples
Query query = pm.newQuery(Game.class); // objects of class Game are stored in datastore
query.setOrdering("creationDate asc");
/* querying for open games, not created by this player */
query.setFilter("state == Game.STATE_OPEN && serverPlayer.id != :playerId");
String playerId = "my-player-id";
List<Game> games = query.execute(playerId); // if there's lots of games, returned list has more entries, than user needs to see at a time
//...

现在我需要扩展该查询以仅获取 25 个游戏,并且仅获取 ID 为“75”的条目之后的游戏。因此用户可以浏览打开的游戏,一次只能获取其中的 25 个。我知道有很多关于 GAE 数据存储的例子,但这些大多都是用 Python 编写的,包括用于设置查询限制的示例代码。我正在寻找有效的 Java 代码示例,但目前找不到。

最佳答案

听起来您想通过查询游标来促进分页。请参阅:http://code.google.com/appengine/docs/java/datastore/queries.html#Query_Cursors

来自 the Google doc :

public class ListPeopleServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query q = new Query("Person");
PreparedQuery pq = datastore.prepare(q);
int pageSize = 15;

resp.setContentType("text/html");
resp.getWriter().println("<ul>");

FetchOptions fetchOptions = FetchOptions.Builder.withLimit(pageSize);
String startCursor = req.getParameter("cursor");

// If this servlet is passed a cursor parameter, let's use it
if (startCursor != null) {
fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor));
}

QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);
for (Entity entity : results) {
resp.getWriter().println("<li>" + entity.getProperty("name") + "</li>");
}
resp.getWriter().println("</ul>");

String cursor = results.getCursor().toWebSafeString();

// Assuming this servlet lives at '/people'
resp.getWriter().println(
"<a href='/people?cursor=" + cursor + "'>Next page</a>");
}
}

关于java - App Engine Java : syntax for setting query limit and start offset.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6867720/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com