All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.meteor.common.database.util.PageUtil Maven / Gradle / Ivy

There is a newer version: 1.0.16
Show newest version
package cn.meteor.common.database.util;// Copyright (C), Created on 2021-04-03

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.meteor.common.data.PageData;
import cn.meteor.common.enums.OrderType;
import cn.meteor.common.model.PageModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Objects;

/**
 * 分页查询工具
 *
 * @author ths
 * @since 1.0.0
 */
public class PageUtil {

	/**
	 * 请求分页数据
	 *
	 * @param request 分页请求数据
	 */
	public static void startPage(PageData request) {
		Page page = PageHelper.startPage(request.getCurrent(), request.getPageSize());
		if (StrUtil.isNotBlank(request.getSortField())) {
			page.setOrderBy(
				StrUtil.toUnderlineCase(request.getSortField())
					+ " "
					+ (Objects.equals(request.getSortBy(), OrderType.ASC.getCode()) ? OrderType.ASC.getDesc() : OrderType.DESC.getDesc())
			);
		}
	}

	/**
	 * 获取分页对象
	 * 

* 注意 pageHelper 插件查询,查询sql结果后,下一行必须获取分页参数 * * @param list DB中查询出的集合数据 * @param clazz 响应的Model类 * @param 泛型Entity * @param 泛型Model * @return 分页列表 */ public static PageModel toPage(List list, Class clazz) { PageInfo pageInfo = new PageInfo<>(list); PageModel pageModel = new PageModel<>(); pageModel.setCurrent(pageInfo.getPageNum()) .setPageSize(pageInfo.getPageSize()) .setPages(pageInfo.getPages()) .setTotal((int) pageInfo.getTotal()) .setData(Convert.toList(clazz, list)); return pageModel; } /** * 获取分页对象 *

* 注意 pageHelper 插件查询,查询sql结果后,下一行必须获取分页参数 * * @param list DB中查询出的集合数据 * @param 泛型Entity * @return 分页列表 */ public static PageModel toPage(List list) { PageInfo pageInfo = new PageInfo<>(list); PageModel pageModel = new PageModel<>(); pageModel.setCurrent(pageInfo.getPageNum()) .setPageSize(pageInfo.getPageSize()) .setPages(pageInfo.getPages()) .setTotal((int) pageInfo.getTotal()) .setData(list); return pageModel; } }