PageBean封装分页对象

package com.cpy.common.util;

import com.github.pagehelper.PageInfo;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 分页数据对象实体
 * @version: 1.0
 */
public class PageBean {

    /**
     * 当前页号
     */
    private Integer page;
    /**
     * 总页数
     */
    private Integer total;
    /**
     * 总记录数
     */
    private Long records;

    /**
     * 数据列表
     */
    private List<Map<String, Object[]>> rows = null;

    /**
     * @description 设置分页数据
     * @param pageInfo 分页对象
     * @return void
     */
    private void setPageInfo(PageInfo<?> pageInfo) {
        this.page = pageInfo.getPageNum();
        this.total = pageInfo.getPages();
        this.records = pageInfo.getTotal();
    }

    public PageBean() {
    }

    public PageBean(List<?> list, String[] propertyNames) {
        try {
            this.rows = buildRows(list, propertyNames);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PageBean(List<?> list, String[] propertyNames, String orderName) {
        try {
            this.rows = buildRowsWithOrder(list, propertyNames, orderName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PageBean(PageInfo<?> pageInfo) {
        setPageInfo(pageInfo);
    }

    /**
     * @description 构建分页列表对象
     * @param pageInfo 分页对象
     * @param propertyNames 列表展示属性
     * @return
     */
    public PageBean(PageInfo<?> pageInfo, String[] propertyNames) {
        setPageInfo(pageInfo);
        try {
            this.rows = buildRows(pageInfo.getList(), propertyNames);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @description 构建带自增列的分页对象
     * @param pageInfo 分页对象
     * @param propertyNames 列表展示属性
     * @param orderName 自增列的属性名
     * @return
     */
    public PageBean(PageInfo<?> pageInfo, String[] propertyNames, String orderName) {
        setPageInfo(pageInfo);
        try {
            this.rows = buildRowsWithOrder(pageInfo.getList(), propertyNames, orderName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @description: 根据属性顺序构建返回的列表数据
     * @param: [pageList, propertyNames]
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object [ ]>>
     */
    public List<Map<String, Object[]>> buildRows(List<?> pageList, String[] propertyNames) throws InvocationTargetException, IllegalAccessException, IntrospectionException {
        int rowCount = pageList.size();
        List<Map<String, Object[]>> rows = new ArrayList<Map<String, Object[]>>(rowCount);
        if (rowCount > 0) {
            Method[] methods = BeanUtils.getReadMethods(pageList.get(0).getClass(), propertyNames);
            int columnLen = methods.length;
            Map<String, Object[]> rowMap = null;
            Object[] colums = null;
            Object bean = null;
            Object value = null;
            int row = 0, col = 0;
            for (row = 0; row < rowCount; row++) {
                bean = pageList.get(row);
                colums = new Object[columnLen];
                for (col = 0; col < columnLen; col++) {
                    value = methods[col].invoke(bean);
                    colums[col] = value ==  null ? "" : String.valueOf(value);
                }
                rowMap = new HashMap<String, Object[]>(1);
                rowMap.put("cell", colums);
                rows.add(rowMap);
            }
        }
        return rows;
    }

    /**
     * @description 根据属性顺序构建返回的列表数据
     * @param pageList 列表数据
     * @param propertyNames 列表展示属性
     * @param orderProperty 排序属性名
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object [ ]>>
     */
    public List<Map<String, Object[]>> buildRowsWithOrder(List<?> pageList, String[] propertyNames, String orderProperty) throws InvocationTargetException, IllegalAccessException, IntrospectionException {
        int rowCount = pageList.size();
        List<Map<String, Object[]>> rows = new ArrayList<Map<String, Object[]>>(rowCount);
        if (rowCount > 0) {
            Method[] methods = BeanUtils.getReadMethods(pageList.get(0).getClass(), propertyNames);
            int columnLen = methods.length;
            Map<String, Object[]> rowMap = null;
            Object[] colums = null;
            Object bean = null;
            Object value = null;
            int row = 0, col = 0;
            for (row = 0; row < rowCount; row++) {
                bean = pageList.get(row);
                colums = new Object[columnLen];
                for (col = 0; col < columnLen; col++) {
                    if (propertyNames[col].equals(orderProperty)) { //自增序号列
                        value = row + 1;
                    } else {
                        value = methods[col].invoke(bean);
                    }
                    colums[col] = value ==  null ? "" : String.valueOf(value);
                }
                rowMap = new HashMap<String, Object[]>(1);
                rowMap.put("cell", colums);
                rows.add(rowMap);
            }
        }
        return rows;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Long getRecords() {
        return records;
    }

    public void setRecords(Long records) {
        this.records = records;
    }

    public List<Map<String, Object[]>> getRows() {
        return rows;
    }

    public void setRows(List<Map<String, Object[]>> rows) {
        this.rows = rows;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值