流式查询
概述
mapper接口
Cursor<Map> streamQuery();
Cursor源码解读
Cursor就是光标的意思,集成了Iterable,意味着Iterable的所有的方法,它都有了
public interface Cursor<T> extends Closeable, Iterable<T> {
/**
* @return true if the cursor has started to fetch items from database.
*/
boolean isOpen();
/**
*
* @return true if the cursor is fully consumed and has returned all elements matching the query.
*/
boolean isConsumed();
/**
* Get the current item index. The first item has the index 0.
* @return -1 if the first cursor item has not been retrieved. The index of the current item retrieved.
*/
int getCurrentIndex();
}
Iterable的方法
Closeable只有一个方法
Closeable是可以关闭的数据源或目标。
调用 close 方法来释放对象持有的资源(例如打开的文件)
public void close() throws IOException;
xml
mybaitsx这个idea插件提供了,自动生成xml文件的功能,非常的方便,如图
<select id="streamQuery" resultType="java.util.Map" fetchSize="10">
select * from xxx_log;
</select>
具体使用
@Autowired
private TblMallOrderDao tblMallOrderDao;
@Test
//不加这个注解会报错
@Transactional
public void getLargeData() {
Cursor<Map> maps = tblMallOrderDao.streamQuery();
maps.forEach(e->{
int currentIndex = maps.getCurrentIndex();
log.info("变量 currentIndex = [ {} ]", JSON.toJSONString(currentIndex));
log.info("变量 e = [ {} ]", JSON.toJSONString(e));
});
}