Java Mybatis分页工具

  • Post author:
  • Post category:java


BasicPaginationQueryDTO.class
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.z8art.com.util.LocalDateTimeUtil;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 基础分页查询DTO
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class BasicPaginationQueryDTO implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 关键字模糊搜索
     */
    @ApiModelProperty("关键字模糊搜索")
    private String keyword;

    /**
     * 排序依据,支持多参数 ("表字段"+"下滑线"+"升序或降序") tableField_ASC | DESC
     */
    @ApiModelProperty("排序依据,支持多参数 (\"表字段\"+\"下滑线\"+\"升序或降序\") tableField_ASC | DESC")
    private String sortBy;

    /**
     * 开始时间字符串
     */
    @ApiModelProperty("开始时间字符串")
    private String beginTime;

    /**
     * 结束时间字符串
     */
    @ApiModelProperty("结束时间字符串")
    private String endTime;

    /**
     * 当前页
     */
    @ApiModelProperty("当前页")
    private Long pageNumber;

    /**
     * 获取条数
     */
    @ApiModelProperty("获取条数")
    private Long pageSize;

    public LocalDateTime getBeginTime() {
        return LocalDateTimeUtil.toLocalDateTime(beginTime);
    }

    public LocalDateTime getEndTime() {
        return LocalDateTimeUtil.toLocalDateTime(endTime);
    }
MybatisPageableUtil.class

/**
 * Mybatis分页工具
 *
 * @author Revisit-Moon
 * @version 1.0
 * @date 2021/3/10 1:30 下午
 */
public class MybatisPageableUtil {

    public static <T> Page<T> getPage(BasicPaginationQueryDTO basicPaginationQueryDTO) {
        //CommonDictionary 常量
        long pageNumber = CommonDictionary.DEFAULT_QUERY_PAGE.longValue();

        long pageSize = CommonDictionary.DEFAULT_QUERY_ROWS.longValue();

        if (basicPaginationQueryDTO.getPageNumber() != null) {
            pageNumber = basicPaginationQueryDTO.getPageNumber() < CommonDictionary.ONE ?
                    CommonDictionary.ONE : basicPaginationQueryDTO.getPageNumber();
        }

        if (basicPaginationQueryDTO.getPageSize() != null) {
            pageSize = basicPaginationQueryDTO.getPageSize() < CommonDictionary.ONE ?
                    CommonDictionary.ONE : basicPaginationQueryDTO.getPageSize();
        }

        Page<T> page = new Page<>(pageNumber, pageSize);
        if (StringUtils.isNotBlank(basicPaginationQueryDTO.getSortBy())) {
            page.addOrder(getSort(basicPaginationQueryDTO.getSortBy()));
        }

        return page;
    }

    private static List<OrderItem> getSort(String sortString) {

        List<OrderItem> sortMethodList = new LinkedList<>();

        if (StringUtils.isNotBlank(sortString)) {
            List<String> sortList = StandardizedUtil.stringToList(sortString);
            if (sortList != null && !sortList.isEmpty()) {
                for (String orderBySort : sortList) {
                    String[] sortMethod = StringUtil.split(orderBySort, SymbolEnum.UNDERLINE.getValue(true));
                    if (sortMethod != null && sortMethod.length > 1) {
                        switch (sortMethod[1]) {
                            case CommonDictionary.POSITIVE_ORDER: {
                                sortMethodList.add(OrderItem.asc(StrUtil.toUnderlineCase(sortMethod[0])));
                                break;
                            }
                            case CommonDictionary.REVERSE_ORDER: {
                                sortMethodList.add(OrderItem.desc(StrUtil.toUnderlineCase(sortMethod[0])));
                                break;
                            }
                            default: {
                                break;
                            }
                        }
                    } else {
                        sortMethodList.add(OrderItem.asc(orderBySort));
                    }
                }
            }
        }

        if (sortMethodList.isEmpty()) {
            sortMethodList.add(OrderItem.asc("id"));
            return sortMethodList;
        } else {
            return new LinkedList<>(sortMethodList);
        }
    }

    public static <T> BasicPageable<T> getPageResult(Page<T> page) {
        List<T> records = page.getRecords();
        if (records == null) {
            records = new ArrayList<>();
        }
        BasicPageable<T> basicPageable = new BasicPageable<>();
        basicPageable.setTotalPages(page.getPages());
        basicPageable.setTotalElements(page.getTotal());
        basicPageable.setCurrentPage(page.getCurrent());
        basicPageable.setCurrentPageTotalElements(records.size());
        basicPageable.setIndexPage(!page.hasPrevious());
        basicPageable.setLastPage(!page.hasNext());
        basicPageable.setHasNextPage(page.hasNext());
        basicPageable.setPreviousPage(page.hasPrevious());
        basicPageable.setList(records);
        return basicPageable;
    }

    public static <T,S> BasicPageable<T> getPageResultVO(Page<S> page, Supplier<T> resultVO) {
        List<S> records = page.getRecords();
        if (records == null) {
            records = new ArrayList<>();
        }

        BasicPageable<T> basicPageableVO = new BasicPageable<>();
        basicPageableVO.setTotalPages(page.getPages());
        basicPageableVO.setTotalElements(page.getTotal());
        basicPageableVO.setCurrentPage(page.getCurrent());
        basicPageableVO.setCurrentPageTotalElements(records.size());
        basicPageableVO.setIndexPage(!page.hasPrevious());
        basicPageableVO.setLastPage(!page.hasNext());
        basicPageableVO.setHasNextPage(page.hasNext());
        basicPageableVO.setPreviousPage(page.hasPrevious());
        for (S record : records) {
            try {
                T vo = resultVO.get();
                BeanExpandUtil.copyProperties(record,vo, CopyOptions.create().ignoreNullValue());
                basicPageableVO.getList().add(vo);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return basicPageableVO;
    }

    public static <T> BasicPageable<T> getPageResult(Page<?> page, List<T> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        BasicPageable<T> basicPageable = new BasicPageable<>();
        basicPageable.setTotalPages(page.getPages());
        basicPageable.setTotalElements(page.getTotal());
        basicPageable.setCurrentPage(page.getCurrent());
        basicPageable.setCurrentPageTotalElements(list.size());
        basicPageable.setIndexPage(!page.hasPrevious());
        basicPageable.setLastPage(!page.hasNext());
        basicPageable.setHasNextPage(page.hasNext());
        basicPageable.setPreviousPage(page.hasPrevious());
        basicPageable.setList(list);
        return basicPageable;
    }

}



版权声明:本文为ZM_Crazy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。