我目前正在开展一个迁移项目,将数据从旧数据库迁移到新数据库(请不要问我为什么要为此使用 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
发布评论
评论(2)
我认为这样的应用程序的自然选择是 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
你的意思是……吗?像这样?
或者这个:
Do you mean sth. like this?
Or this: