Mybatis cursor query

Mybatis cursor query

Usually when processing a large amount of data in a table, due to the large amount of data, paging and batch query processing must be used. Otherwise, the data amount is too large, which will cause OOM and other problems. However, if paging cannot be used, how to process it? In this case, the Mybatis cursor must be used. Inquire

principle

Cursor implements the Closeable and Iterable interfaces. We can obtain data for processing through iterators.

Example

Check for phrases

Write the corresponding query statement on the Mapper

//PriceIndexEntity是查出的实体对象
//ResultSetType表示游标的滚动模式 resultSetType = ResultSetType.FORWARD_ONLY 只能向前滚动
//fetchSize表示每次执行的记录数 fetchSize = Integer.MIN_VALUE
public interface CursorDemoMapper extends BaseMapper<PriceIndexEntity> {
    
    
    @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = Integer.MIN_VALUE)
    @Select("select * from t_scf_price_index ")
    Cursor<PriceIndexEntity> scrollResult();
}

business processing

public class DemoService implements IBusinessService{
    
    
    @Resource
    SqlSessionFactory sqlSessionFactory;
    @Override
    public void doVerify(ServiceHandlerContext context) {
    
    

    }

    @Override
    public void doWork(ServiceHandlerContext context) {
    
    
        try(    //对应mysql数据库不会自动开启连接  所以需要手动开启连接,执行完成后自动关闭连接
                SqlSession sqlSession=sqlSessionFactory.openSession();
                //2、在连接中开启游标查询
                Cursor<PriceIndexEntity> priceIndexEntities = sqlSession.getMapper(CursorDemoMapper.class).scrollResult();) {
    
    
            //buffer的大小建议设置在1000左右。
            AtomicInteger index= new AtomicInteger(1);
            int bufferSize=1000;
            List<PriceIndexEntity> buffer =new ArrayList<>(bufferSize);
            priceIndexEntities.forEach(priceIndexEntity -> {
    
    
            //将查出的数据放到buffer中
                buffer.add(priceIndexEntity);
                if(index.getAndIncrement() ==bufferSize){
    
    
                //业务处理
                    doPartWork(buffer);
                    buffer.clear();
                    index.set(1);
                }
            });
            //处理最后不足1000的数据
            if(buffer.size()>0){
    
    
                doPartWork(buffer);
            }
        } catch (IOException e) {
    
    
            log.error("XXX查询异常:【{}】",e.getMessage(),e);
        }
        ServiceHandlerContextExt.createSuccessResponseWithMsg(this,context,"sd");
    }
    public void doPartWork(List<PriceIndexEntity> buffer){
    
    
 //业务处理       System.out.println("================================="+buffer.size());
    }

Guess you like

Origin blog.csdn.net/csdn_mycsdn/article/details/121723674