jdbc和mybatis的流式查询使用方法

网友投稿 1151 2022-11-26

jdbc和mybatis的流式查询使用方法

jdbc和mybatis的流式查询使用方法

目录导语:jdbc流式查询:mybatis流式查询:

导语:

有些时候我们所需要查询的数据量比较大,但是jvm内存又是有限制的,数据量过大会导致内存http://溢出。这个时候就可以使用流式查询,数据一条条的返回,处理完一条在拿下一条数据,这样每次在内存里面的数据其实很小,不会导致内存溢出。

本文里面会讲到jdbc的流式查询和mybatis的流式查询。

jdbc流式查询:

jdbc的流式查询需要在生成PreparedStatement的时候设置三个参数。如下:

PreparedStatement stmt = jdbcTemplate.getDataSource().getConnection().prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

stmt.setFetchSize(Integer.MIN_VALUE);

主要使用到的是java.sql.Connection的prepareStatement方法。

PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException;

resultSetType和resultSetConcurrency我们要分别设置为ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY。

还有就是fetchSize设置为Integer.MIN_VALUE,一开始比较疑惑为啥是这个值。后来发现代码里面对这个值其实是有特殊处理的。

这个是com.mysql.cj.jdbc.StatementImpl的setFetchSize方法。

@Override

public void setFetchSize(int rows) throws SQLException {

synchronized (checkClosed().getConnectionMutex())CnUGB {

if (((rows < 0) && (rows != Integer.MIN_VALUE)) || ((this.maxRows > 0) && (rows > this.getMaxRows()))) {

throw SQLError.createSQLException(Messages.getString("Statement.7"), MysqlErrorNumbers.SQL_STATE_ILLcom.mysql.cj.jdbc.StatementImpl的方法EGAL_ARGUMENT, getExceptionInterceptor());

}

this.query.setResultFetchSize(rows);

}

}

resultSetType,有以下三种

/**

* The constant indicating the type for a ResultSet object

* whose cursor may move only forward.

* @since 1.2

*/

int TYPE_FORWARD_ONLY = 1003;

/**

* The constant indicating the type for a ResultSet object

* that is scrollable but generally not sensitive to changes to the data

* that underlies the ResultSet.

* @since 1.2

*/

int TYPE_SCROLL_INSENSITIVE = 1004;

/**

* The constant indicating the type for a ResultSet object

* that is scrollable and generally sensitive to changes to the data

* that underlies the ResultSet.

* @since 1.2

*/

int TYPE_SCROLL_SENSITIVE = 1005;

stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

stmt.setFetchSize(Integer.MIN_VALUE);

resultSetConcurrency有以下两种,流式查询要设置为只读的,数据不会被更新。

/**

* The constant indicating the concurrency mode for a

* ResultSet object that may NOT be updated.

* @since 1.2

*/

int CONCUR_READ_ONLY = 1007;

/**

* The constant indicating the concurrency mode for a

* ResultSet object that may be updated.

* @since 1.2

*/

int CONCUR_UPDATABLE = 1008;

mybatis流式查询:

mapper中的代码:

@Select("select * from xxx order by xx desc")

@Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = Integer.MIN_VALUE)

@ResultType(XxxObject.class)

void queryStreamResult(ResultHandler handler);

在查询方法上加入注解@Options和@ResultType。设置参数不用多说和上面jdbc的底层是一样的,参数值也一样。

只不过设置了@ResultType告诉程序应该要返回什么类型的对象。还有这个ResultHandler其实就是Consumer的函数式接口用来处理每一条返回的数据。

具体方法中的代码:

@Override

public Boolean dealDataList() {

mapper.queryStreamResult(resultContext -> {

dealSingleData(resultContext.getResultObject().getUid());

});

return true;

}

这里怎么使用每一条返回的数据只要在resultContext使用ResultObject就可以拿到上面mapper设置的XxxObject对象进行操作了。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python flask实战订餐系统微信小程序-25实现账号添加的功能
下一篇:Python量化交易实战-41EasyTrader自动化模拟真实交易
相关文章

 发表评论

暂时没有评论,来抢沙发吧~