Search in sources :

Example 1 with IPage

use of com.github.pagehelper.IPage in project Mybatis-PageHelper by pagehelper.

the class PageParams method getPage.

/**
 * 获取分页参数
 *
 * @param parameterObject
 * @param rowBounds
 * @return
 */
public Page getPage(Object parameterObject, RowBounds rowBounds) {
    Page page = PageHelper.getLocalPage();
    if (page == null) {
        if (rowBounds != RowBounds.DEFAULT) {
            if (offsetAsPageNum) {
                page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount);
            } else {
                page = new Page(new int[] { rowBounds.getOffset(), rowBounds.getLimit() }, rowBoundsWithCount);
                // offsetAsPageNum=false的时候,由于PageNum问题,不能使用reasonable,这里会强制为false
                page.setReasonable(false);
            }
            if (rowBounds instanceof PageRowBounds) {
                PageRowBounds pageRowBounds = (PageRowBounds) rowBounds;
                page.setCount(pageRowBounds.getCount() == null || pageRowBounds.getCount());
            }
        } else if (parameterObject instanceof IPage || supportMethodsArguments) {
            try {
                page = PageObjectUtil.getPageFromObject(parameterObject, false);
            } catch (Exception e) {
                return null;
            }
        }
        if (page == null) {
            return null;
        }
        PageHelper.setLocalPage(page);
    }
    // 分页合理化
    if (page.getReasonable() == null) {
        page.setReasonable(reasonable);
    }
    // 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
    if (page.getPageSizeZero() == null) {
        page.setPageSizeZero(pageSizeZero);
    }
    return page;
}
Also used : IPage(com.github.pagehelper.IPage) PageRowBounds(com.github.pagehelper.PageRowBounds) IPage(com.github.pagehelper.IPage) Page(com.github.pagehelper.Page)

Example 2 with IPage

use of com.github.pagehelper.IPage in project Mybatis-PageHelper by pagehelper.

the class PageObjectUtil method getPageFromObject.

/**
 * 对象中获取分页参数
 *
 * @param params
 * @return
 */
public static <T> Page<T> getPageFromObject(Object params, boolean required) {
    if (params == null) {
        throw new PageException("无法获取分页查询参数!");
    }
    if (params instanceof IPage) {
        IPage pageParams = (IPage) params;
        Page page = null;
        if (pageParams.getPageNum() != null && pageParams.getPageSize() != null) {
            page = new Page(pageParams.getPageNum(), pageParams.getPageSize());
        }
        if (StringUtil.isNotEmpty(pageParams.getOrderBy())) {
            if (page != null) {
                page.setOrderBy(pageParams.getOrderBy());
            } else {
                page = new Page();
                page.setOrderBy(pageParams.getOrderBy());
                page.setOrderByOnly(true);
            }
        }
        return page;
    }
    int pageNum;
    int pageSize;
    MetaObject paramsObject = null;
    if (hasRequest && requestClass.isAssignableFrom(params.getClass())) {
        try {
            paramsObject = MetaObjectUtil.forObject(getParameterMap.invoke(params, new Object[] {}));
        } catch (Exception e) {
        // 忽略
        }
    } else {
        paramsObject = MetaObjectUtil.forObject(params);
    }
    if (paramsObject == null) {
        throw new PageException("分页查询参数处理失败!");
    }
    Object orderBy = getParamValue(paramsObject, "orderBy", false);
    boolean hasOrderBy = false;
    if (orderBy != null && orderBy.toString().length() > 0) {
        hasOrderBy = true;
    }
    try {
        Object _pageNum = getParamValue(paramsObject, "pageNum", required);
        Object _pageSize = getParamValue(paramsObject, "pageSize", required);
        if (_pageNum == null || _pageSize == null) {
            if (hasOrderBy) {
                Page page = new Page();
                page.setOrderBy(orderBy.toString());
                page.setOrderByOnly(true);
                return page;
            }
            return null;
        }
        pageNum = Integer.parseInt(String.valueOf(_pageNum));
        pageSize = Integer.parseInt(String.valueOf(_pageSize));
    } catch (NumberFormatException e) {
        throw new PageException("分页参数不是合法的数字类型!", e);
    }
    Page page = new Page(pageNum, pageSize);
    // count查询
    Object _count = getParamValue(paramsObject, "count", false);
    if (_count != null) {
        page.setCount(Boolean.valueOf(String.valueOf(_count)));
    }
    // 排序
    if (hasOrderBy) {
        page.setOrderBy(orderBy.toString());
    }
    // 分页合理化
    Object reasonable = getParamValue(paramsObject, "reasonable", false);
    if (reasonable != null) {
        page.setReasonable(Boolean.valueOf(String.valueOf(reasonable)));
    }
    // 查询全部
    Object pageSizeZero = getParamValue(paramsObject, "pageSizeZero", false);
    if (pageSizeZero != null) {
        page.setPageSizeZero(Boolean.valueOf(String.valueOf(pageSizeZero)));
    }
    return page;
}
Also used : PageException(com.github.pagehelper.PageException) IPage(com.github.pagehelper.IPage) MetaObject(org.apache.ibatis.reflection.MetaObject) IPage(com.github.pagehelper.IPage) Page(com.github.pagehelper.Page) MetaObject(org.apache.ibatis.reflection.MetaObject) PageException(com.github.pagehelper.PageException)

Aggregations

IPage (com.github.pagehelper.IPage)2 Page (com.github.pagehelper.Page)2 PageException (com.github.pagehelper.PageException)1 PageRowBounds (com.github.pagehelper.PageRowBounds)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1