java 批量选择记录 Spring JDBCTemplate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16165176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:05:09  来源:igfitidea点击:

Select record in batches Spring JDBCTemplate

javaspringjdbcdb2

提问by coolbootgeek

I have a table which has around 5 million records. I want to read all the records from this table and do some processing on them. Now I want to query these records in batches say 1000 in one go, process them and fetch next 1000 records and so on.

我有一张桌子,里面有大约 500 万条记录。我想从这个表中读取所有记录并对它们做一些处理。现在我想批量查询这些记录,一次说 1000 条,处理它们并获取接下来的 1000 条记录等等。

However JDBCTemplate.query method only returns List containing all the records in the table. Obviously I can not have 5 million records in memory.

但是 JDBCTemplate.query 方法只返回包含表中所有记录的 List。显然我的内存中不能有 500 万条记录。

Is there a way address my problem using Spring JDBC? Underlying database is going to be DB2 if that helps.

有没有办法使用 Spring JDBC 解决我的问题?如果有帮助,基础数据库将是 DB2。

回答by JB Nizet

Read the javadoc of JdbcTemplate. There are plenty of other methods, also named query(), that don't return a list, and take a RowCallbackHandleror a ResultSetExtractoras argument. Use these ones.

阅读JdbcTemplatejavadoc。还有许多其他方法,也称为query(),它们不返回列表,而是将 aRowCallbackHandler或 aResultSetExtractor作为参数。使用这些。

To set the number of rows fetched at once by the resultset, override applyStatementSettings()and call Statement.setFetchSize()

要设置结果集一次获取的行数,请覆盖applyStatementSettings()并调用Statement.setFetchSize()

回答by Evgeniy Dorofeev

Try something like this

尝试这样的事情

    jdbcTemplate.query(sql, new RowCallbackHandler() {
        public void processRow(ResultSet arg0) throws SQLException {
            // ...
        }
    });

this way you can process any number of records one by one

这样你就可以一一处理任意数量的记录