mybatis-plus流式查询

21 篇文章 0 订阅
1 篇文章 0 订阅

流式查询

原始文章

这篇文章也不错

概述

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的方法

Iterable的方法

Closeable只有一个方法

Closeable是可以关闭的数据源或目标。
调用 close 方法来释放对象持有的资源(例如打开的文件)

public void close() throws IOException;

xml

mybaitsx这个idea插件提供了,自动生成xml文件的功能,非常的方便,如图

自动生成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));
        });
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值