有没有一种可移植的方式来“选择第一个10 *来自T”语义? [英] Is there a portable way to have "SELECT FIRST 10 * FROM T" semantic?

查看:81
本文介绍了有没有一种可移植的方式来“选择第一个10 *来自T”语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中读取10k记录块中的数据。

I want to read data in blocks of say 10k records from a database.

我发现结果限制,很明显这不能用sql以便携方式完成。

I found Result limits on wikipedia and it seems obvious that this can't done with sql in a portable way.

另一种方法可能是 JdbcTemplate 提供了许多查询方法,但我怎么能确定已经读取了足够的行。通过RowMapper和ResultSetExtractor等回调,无法指示已读取足够的数据。

Another approach could be JdbcTemplate which offers many methods for queries, but how could I decide that enough rows have been read. Through the callbacks like RowMapper and ResultSetExtractor it can't be indicated, that enough data has been read.

编辑:我在寻找JdbcTemplate的解决方案
这个帖子建议使用 setMaxRows 我忽略了。

I was looking for a solution for JdbcTemplate This post suggests to use setMaxRows which I had overlooked.

推荐答案

抓取 Hibernate JPA 。两者都熟悉各种数据库方言,并且会透明地处理令人讨厌的数据库细节。

Grab Hibernate or JPA. Both are familiar with various database dialects and will handle the nasty DB specifics under the hoods transparently.

在Hibernate中你可以使用 条件#setFirstResult() 标准#setMaxResults() 。例如

In Hibernate you can paginate using Criteria#setFirstResult() and Criteria#setMaxResults(). E.g.

List users = session.createCriteria(User.class)
    .addOrder(Order.asc("id"))
    .setFirstResult(0) // Index of first row to be retrieved.
    .setMaxResults(10) // Amount of rows to be retrieved.
    .list();

在JPA中你可以使用 查询#setFirstResult() Query#setMaxResults ()

In JPA you can do similar using Query#setFirstResult() and Query#setMaxResults().

List users = em.createQuery("SELECT u FROM User u ORDER BY u.id");
    .setFirstResult(0) // Index of first row to be retrieved.
    .setMaxResults(10) // Amount of rows to be retrieved.
    .getResultList();

这篇关于有没有一种可移植的方式来“选择第一个10 *来自T”语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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