【Mybatis】PageUtils 工具类

此工具类主要是针对 需要 进行一些批量查询数据库,分页去处理数据的。

分页工具类

/**
 * @author fei.chen
 * @projectName data-submission
 * @description: 分页工具类
 * @date 2023/4/22上午 10:10
 */
/**
 * @author fei.chen
 * @projectName data-submission
 * @description: 分页工具类
 * @date 2023/4/22上午 10:10
 */
public class PageUtils {
    private PageUtils() {
        throw new AssertionError("No " + this.getClass().getName() + " instances for you!");
    }

    /**
     * 计算offset
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public static int offset(int pageNumber, int pageSize) {
        pageNumber = Math.max(1, pageNumber);
        pageSize = Math.max(1, pageSize);
        int offset = (pageNumber - 1) * pageSize;
        return offset;
    }

    /**
     * 计算页数
     * @param recordCount
     * @param pageSize
     * @return
     */
    public static int pageCount(int recordCount, int pageSize) {
        recordCount = Math.max(0, recordCount);
        pageSize = Math.max(1, pageSize);
        int page = (recordCount - 1) / pageSize + 1;
        return page;
    }
}

使用案例

此代码主要是数据量太大,然后要分页进行处理,最后要批量入库,所以先根据总数计算有多少页,然后循环,根据页数去通过offset 一批一批去查询。

我的数据库是:postgresql

 @Override
    public void asyncAssetProcess() {
        Integer counts = getCount();
        int pageSize = 200;
        int pageCount = PageUtils.pageCount(counts, pageSize);//计算页数

        for (int i = 1; i <= pageCount; i++) {
            //计算开始的位置 offset
            int offset = PageUtils.offset(i, pageSize);
            JSONObject jsonObject= ***Service.get***Devices(authorization, pageSize, offset)
        }
    }

猜你喜欢

转载自blog.csdn.net/daohangtaiqian/article/details/130301281
今日推荐