使用JdbcTemplate/JDBC流式读取数据库
日期: 2020-05-30 分类: 跨站数据 374次阅读
JDBC原生实现
注意MySql中
conn.prepareStatement("select * from table",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
// 这里因为MySQL驱动实现使用Integer.MIN_VALUE来判断是否使用流的方式
preparedStatement.setFetchSize(Integer.MIN_VALUE);
@Test
public void testCursor() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager
.getConnection("jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=utf8&useSSL=false",
"root", "root");
PreparedStatement preparedStatement =
connection.prepareStatement("select * from table",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
preparedStatement.setFetchSize(Integer.MIN_VALUE);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.err.println(resultSet.getString("id"));
}
connection.close();
}复制代码
JdbcTemplate使用
void query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException;复制代码
关键点在于参数PreparedStatementCreator与RowCallbackHandler,我们可以控制PreparedStatement的创建与ResultSet的提取,这样就简单了,具体代码实现如下:
MySQL
jdbcTemplate.query(con -> {
PreparedStatement preparedStatement =
con.prepareStatement("select * from table",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
preparedStatement.setFetchSize(Integer.MIN_VALUE);
preparedStatement.setFetchDirection(ResultSet.FETCH_FORWARD);
return preparedStatement;
}, rs -> {
while (rs.next()) {
System.err.println(resultSet.getString("id"));
}
});
手动控制提交
ransactionSynchronizationManager.initSynchronization();
DataSource dataSource = ((JdbcTemplate)jdbcTemplate.getJdbcOperations()).getDataSource();
Connection connection = DataSourceUtils.getConnection(dataSource);
try {
connection.setAutoCommit(false);
//需要操作数据库的两个insert,或者提供回调给业务开发人员
connection.commit();
} catch (SQLException e) {
} finally {
try {
TransactionSynchronizationManager.clearSynchronization();
} catch (IllegalStateException e) {
}
try {
connection.setAutoCommit(true);
} catch (SQLException e) {
}
}
除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog
标签:DB
精华推荐