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

com.github.dennisit.vplus.data.utils.PaginationUtils Maven / Gradle / Ivy

/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
 */
package com.github.dennisit.vplus.data.utils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.IService;
import com.github.dennisit.vplus.data.page.Pagination;
import com.google.common.collect.Lists;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Created by Elon.su on 17/10/28.
 */
public class PaginationUtils {

    /**
     * 将mybatisplus分页模型转换成vplus分页模型
     * @param page mybatisplus分页模型
     * @param  对象集合
     * @return 目标分页实体
     */
    public static  Pagination builder(Page page){
        if(null == page){
            return Pagination.empty();
        }

        return new Pagination<>(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    /**
     * 构建分页查询对象
     * @param service 服务接口
     * @param page 页码
     * @param size 页量
     * @param wrapper 查询条件
     * @param  泛型约束
     * @return page对象
     */
    public static  Pagination selectPagination(IService service, int page, int size, EntityWrapper wrapper){
        Assert.notNull(service, "服务接口实现不能为空");
        Assert.notNull(wrapper, "查询条件不能为空");

        return builder(service.selectPage(new Page<>(Math.abs(page), Math.abs(size)), wrapper));
    }


    /**
     * 获取分页数据集合
     * @param page 分页实体
     * @param  对象元素
     * @return page页中的集合
     */
    public static  List items(Page page){
        if(null == page){
            return Lists.newArrayList();
        }

        return page.getRecords();
    }


    /**
     * 获取分页数据集合
     * @param pagination 分页实体
     * @param  对象元素
     * @return page页中的集合
     */
    public static  List items(Pagination pagination){
        if(null == pagination){
            return Lists.newArrayList();
        }

        return pagination.getItems();
    }


    /**
     * 抽取分页查询后的list集合
     * @param service 业务接口
     * @param page 页码
     * @param size 页量
     * @param wrapper 查询条件
     * @param  查询实体
     * @return page页中的集合
     */
    public static  List items(IService service, int page, int size, EntityWrapper wrapper){
        Assert.notNull(service, "服务接口实现不能为空");
        Assert.notNull(wrapper, "查询条件不能为空");

        return items(service.selectPage(new Page<>(Math.abs(page), Math.abs(size)), wrapper));
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy