Mybatis流式查询

存有一个需要遍历处理的需求,因数据量过大不能一次性将结果集装入客户端内存,就可以考虑使用流式查询或者分页查询, 本文仅讨论流式查询.

话不多说  直接上代码

方式一: 基于mybatis的游标规则  类似于oracle的fetchsize缓冲区

mapper层:

package com.abi.mapper;

import com.abi.entity.WholesalerBase;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface WholesalerBaseMapper extends BaseMapper {

    /**
     * 流式查询
     * @param resultHandler
     */
    @ResultType(WholesalerBase.class)
    @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000)
    void getBaseList(ResultHandler resultHandler);


    /**
     * 流式查询  查询上一周的更新数据
     * @param resultHandler
     */
    @ResultType(WholesalerBase.class)
    @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000)
    void getBaseWeekList(ResultHandler resultHandler);

}

xml层

 

service层

package com.abi.service.impl;



@Slf4j
@Service
@RequiredArgsConstructor
public class WholesalerBaseServiceImpl extends ServiceImpl implements WholesalerBaseService {


    @Override
    @Transactional(readOnly = true)
    public void fixReuseSendMsgNew() {
        this.baseMapper.getBaseWeekList(resultContext -> {
           WholesalerBase base = resultContext.getResultObject();
                try {
                     //逻辑代码
                     log.info("base-->{}",base);
                } catch (Exception e) {
                    log.error("XX发生错误,原因 = {}" e.getMessage());
                    e.printStackTrace();
                }
        });
    }
}

基于Cursor构建的方式 

方式二: SqlSession 方式

mapper层

    @Select("select * from wholesaler_base base WHERE base.verify_status != 3 " +
            "                and base.deleted=0 and base.level=2")
    Cursor scan();

测试程序

    @Test
    public void t5() {
        SqlSession sqlSession = null;
        try {
            sqlSession = this.sqlSessionFactory.openSession();
            Cursor cursors =                                         
            sqlSession.getMapper(WholesalerBaseMapper.class).scan();
            cursor.forEach(base -> {
                    //业务需求
                    System.out.println("base = " + base);
                });
        } finally {
            if (sqlSession != null) {

                sqlSession.close();
            }
        }
    }

方式三: TransactionTemplate

测试程序

    @Resource
    private PlatformTransactionManager platformTransactionManager;    


    @Test
    void t3() {
        TransactionTemplate transactionTemplate = new TransactionTemplate(platformTransactionManager);

        transactionTemplate.execute(status -> {
            try (Cursor cursor = wholesalerBaseMapper.scan()) {
                cursor.forEach(base -> {
                    //需求代码
                    System.out.println("base = " + base);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        });
    }

方式四: @Transactional 注解

    @Test
    @Transactional(readOnly = true)
    void t4() {
        try (Cursor cursor = wholesalerBaseMapper.scan()) {
            cursor.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(mybatis,oracle,java,mysql)