使用 JdbcTemplate 进行分页查询

发布于 2024-10-04 13:24:02 字数 700 浏览 1 评论 0 原文

我目前正在开展一个迁移项目,将数据从旧数据库迁移到新数据库(请不要问我为什么要为此使用 Java 应用程序,这是客户所需要的)。

我现在正在更新一些初始代码。我要更改的一件事是使用 Spring 的 JdbcTemplate 而不是那里的样板代码。

不幸的是,我还没有找到在 JdbcTemplate 上执行分页查询的方法,类似于旧代码:

Statement statement = getConnection().createStatement(
    ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setFetchDirection(ResultSet.FETCH_FORWARD);
statement.setFetchSize(1000);
return statement.executeQuery();

getConnection() 只是返回一个 Connection< /code> 对象,用纯 JDBC 代码创建(它不是 SessionFactory 或框架实现的一部分)。

然后我将循环结果集,一次映射一行。有谁知道是否有一种简单的方法可以使用 JdbcTemplate 实现相同的功能?

TIA

I'm currently working on a migration project, to migrate data from the old db to the new one (please do not ask why I'm going through a Java application for this, it's what the customer requires).

There was some, initial, code which I'm updating now. One of the things I'm changing is using Spring's JdbcTemplate rather then the boiler-plate code which was there.

Unfortunately, I haven't found a way yet to execute paged queries on a JdbcTemplate, analogue to the old code:

Statement statement = getConnection().createStatement(
    ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setFetchDirection(ResultSet.FETCH_FORWARD);
statement.setFetchSize(1000);
return statement.executeQuery();

The getConnection() just return a Connection object, created in plain JDBC code (it's not part of a SessionFactory or a framework implementation).

I would then loop over the resultset, mapping the rows one at a time. Does anyone know if there's an easy way to achieve the same functionality with JdbcTemplate?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失而复得 2024-10-11 13:24:02

我认为这样的应用程序的自然选择是 Spring Batch(阅读令人印象深刻的功能页面

以下是​​与您相关的部分:

ItemReaders 和 ItemWriters >数据库
特别是 JdbcPagingItemReader

I think the natural choice for such an Application is Spring Batch (read the impressive Features page)

Here are the sections that should be relevant to you:

ItemReaders and ItemWriters > DataBase
and in particular JdbcPagingItemReader

醉酒的小男人 2024-10-11 13:24:02

你的意思是……吗?像这样?

SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource);

List<String> result = template.query("SELECT name FROM people WHERE id > ?",
    new RowMapper<String>() {

        public String mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getString("name");
        }

    }, 666
);

或者这个:

template.getJdbcOperations().query("SELECT name FROM people WHERE id > ?",
        new Object[] { 666 },
        new RowCallbackHandler() {

            public void processRow(ResultSet rs) throws SQLException {
                System.out.println(String.format(
                    "Got '%s'", rs.getString("name")));
            }

        }
);

Do you mean sth. like this?

SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource);

List<String> result = template.query("SELECT name FROM people WHERE id > ?",
    new RowMapper<String>() {

        public String mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getString("name");
        }

    }, 666
);

Or this:

template.getJdbcOperations().query("SELECT name FROM people WHERE id > ?",
        new Object[] { 666 },
        new RowCallbackHandler() {

            public void processRow(ResultSet rs) throws SQLException {
                System.out.println(String.format(
                    "Got '%s'", rs.getString("name")));
            }

        }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文