Mybatis 分页跑批处理数据简易框架

时间:2021-09-30 18:36:13

需求:解决跑批查询出来数据量太大,做了一个简单的分页处理数据框架

主要两个类:PageHelperService 和 AbstractPageHelperService

/**
 * ClassName:PageHelperService Function: 分页跑批数据
 * 
 * @author WEIJIA625
 * @version
 * @since Ver 1.1
 * @Date 2017年1月17日 下午4:31:49
 * 
 * @see
 */
public interface PageHelperService<T> {

    /**
     * 
     * queryAllData:(要实现的查询数据的方法)
     * 
     * @param @param query
     * @param @return 设定文件
     * @return PageInfo<T> DOM对象
     * @throws
     * @since CodingExample Ver 1.1
     */
    PageInfo<T> queryAllData(T query);

    /**
     * doPageData:(要实现的处理数据的方法)
     * 
     * @param @param aPageData 设定文件
     * @return void DOM对象
     * @throws
     * @since CodingExample Ver 1.1
     */
    void doPageData(PageInfo<T> aPageData);

    /**
     * 
     * queryPageData:(包装queryAllData实现分页-SQL尾加Limit)
     * 
     * @param @param query
     * @param @param pageSize
     * @param @param page
     * @param @return 设定文件
     * @return PageInfo<T> DOM对象
     * @throws
     * @since CodingExample Ver 1.1
     */
    PageInfo<T> queryPageData(T query, int pageSize, int page);

    /**
     * 
     * doLoopPageData:(包装了分页查询和处理数据的逻辑,查询和处理在queryAllData和doPageData方法)
     * 
     * @param @param query
     * @param @param pageSize 设定文件
     * @return void DOM对象
     * @throws
     * @since CodingExample Ver 1.1
     */
    void doLoopPageData(T query, int pageSize);

    /**
     * 
     * doLoopPageData:(包装了分页查询和处理数据的逻辑,查询和处理在queryAllData和doPageData方法)
     * 默认pageSize=AbstractPageHelperService.BATCH_SIZE
     * 
     * @param @param query 设定文件
     * @return void DOM对象
     * @throws
     * @since CodingExample Ver 1.1
     */
    void doLoopPageData(T query);
}


public abstract class AbstractPageHelperService<T> implements PageHelperService<T> {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private static final int BATCH_SIZE = 600;

    public PageInfo<T> queryPageData(T query, int pageSize, int page) {
        //只对紧跟的第一条SQL有效
        PageHelper.startPage(page, pageSize);
        return queryAllData(query);
    }

    public void doLoopPageData(T query) {
        doLoopPageData(query, BATCH_SIZE);
    }

    public void doLoopPageData(T query, int pageSize) {
        PageHelper.startPage(1, pageSize);
        PageInfo<T> pageData = queryAllData(query);
        doPageData(pageData);
        printPageDataInfo(pageData);
        for (int i = pageData.getPages(); i >= 2; i--) {
            //分页参数合理化
            PageHelper.startPage(i, pageSize, false, true);
            pageData = queryAllData(query);
            doPageData(pageData);
            printPageDataInfo(pageData);
        }
    }

    private void printPageDataInfo(PageInfo<T> pageData) {
        logger.info("id[{}] Total[{}] PageNum[{}] StartRow[{}] EndRow[{}]", //
                UUID.randomUUID(), pageData.getTotal(), pageData.getPageNum(), //
                pageData.getStartRow(), pageData.getEndRow());
    }

    @Override
    public void doPageData(PageInfo<T> aPageData) {
        logger.info("doPageData do not Override");
    }

import com.github.pagehelper.PageInfo;
import com.pingan.toa.asset.dao.mapper.bank.AssetBankCardMapper;
import com.pingan.toa.asset.model.po.bank.AssetBankCardPO;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("bizService")
public class BizService extends AbstractPageHelperService<AssetBankCardPO> {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private AssetBankCardMapper assetBankCardMapper;

    @Override
    public PageInfo<AssetBankCardPO> queryAllData(AssetBankCardPO aCondition) {

        // Example ex=new Example(AssetBankCardPO.class);
        // ex.setOrderByClause("customerOrgName desc");
        Map<String, Object> m = new HashMap<String, Object>();
        List<AssetBankCardPO> selectAll = assetBankCardMapper.query(m);
        logger.info("查询[{}]", selectAll.size());
        StringBuffer buf = new StringBuffer();
        for (AssetBankCardPO po : selectAll) {
            buf.append(po.getCustomerOrgName() + ",");
        }
        logger.info("数据[{}]", buf.toString());
        return new PageInfo<AssetBankCardPO>(selectAll);

    }

    @Override
    public void doPageData(PageInfo<AssetBankCardPO> aPageData) {
        StringBuffer buf = new StringBuffer();
        for (AssetBankCardPO po : aPageData.getList()) {
            buf.append(po.getCustomerOrgName() + ",");
        }
        logger.info("处理[{}]", aPageData.getSize());
        logger.info("数据[{}]", buf.toString());
    }

}