Search in sources :

Example 6 with Page

use of com.github.pagehelper.Page 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)

Example 7 with Page

use of com.github.pagehelper.Page in project tech by ffyyhh995511.

the class PeopleService method queryPage.

public OpenPage queryPage(Integer pageNum, Integer pageSize) {
    // 获取第1页,10条内容,默认查询总数count
    PageHelper.startPage(pageNum, pageSize);
    // 紧跟着的第一个select方法会被分页
    List<People> list = peopleMapper.queryAll();
    Page p = ((Page) list);
    return OpenPage.buildPage(p);
}
Also used : People(org.tech.domain.People) Page(com.github.pagehelper.Page) OpenPage(org.tech.commons.OpenPage)

Example 8 with Page

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

the class AbstractHelperDialect method afterCount.

@Override
public boolean afterCount(long count, Object parameterObject, RowBounds rowBounds) {
    Page page = getLocalPage();
    page.setTotal(count);
    if (rowBounds instanceof PageRowBounds) {
        ((PageRowBounds) rowBounds).setTotal(count);
    }
    // pageSize = 0 的时候,还需要执行后续查询,但是不会分页
    if (page.getPageSizeZero() != null) {
        // PageSizeZero=false&&pageSize<=0
        if (!page.getPageSizeZero() && page.getPageSize() <= 0) {
            return false;
        } else // PageSizeZero=true&&pageSize<0 返回 false,只有>=0才需要执行后续的
        if (page.getPageSizeZero() && page.getPageSize() < 0) {
            return false;
        }
    }
    // 页码>0 && 开始行数<总行数即可,不需要考虑 pageSize(上面的 if 已经处理不符合要求的值了)
    return page.getPageNum() > 0 && count > page.getStartRow();
}
Also used : PageRowBounds(com.github.pagehelper.PageRowBounds) Page(com.github.pagehelper.Page)

Example 9 with Page

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

the class SqlServerDialect method getPageSql.

/**
 * 分页查询,pageHelper转换SQL时报错with(nolock)不识别的问题,
 * 重写父类AbstractHelperDialect.getPageSql转换出错的方法。
 * 1. this.replaceSql.replace(sql);先转换成假的表名
 * 2. 然后进行SQL转换
 * 3. this.replaceSql.restore(sql);最后再恢复成真的with(nolock)
 */
@Override
public String getPageSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey pageKey) {
    String sql = boundSql.getSql();
    Page page = this.getLocalPage();
    String orderBy = page.getOrderBy();
    if (StringUtil.isNotEmpty(orderBy)) {
        pageKey.update(orderBy);
        sql = this.replaceSql.replace(sql);
        sql = OrderByParser.converToOrderBySql(sql, orderBy);
        sql = this.replaceSql.restore(sql);
    }
    return page.isOrderByOnly() ? sql : this.getPageSql(sql, page, pageKey);
}
Also used : Page(com.github.pagehelper.Page)

Example 10 with Page

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

the class AbstractHelperDialect method getPageSql.

@Override
public String getPageSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey pageKey) {
    String sql = boundSql.getSql();
    Page page = getLocalPage();
    // 支持 order by
    String orderBy = page.getOrderBy();
    if (StringUtil.isNotEmpty(orderBy)) {
        pageKey.update(orderBy);
        sql = OrderByParser.converToOrderBySql(sql, orderBy);
    }
    if (page.isOrderByOnly()) {
        return sql;
    }
    return getPageSql(sql, page, pageKey);
}
Also used : Page(com.github.pagehelper.Page)

Aggregations

Page (com.github.pagehelper.Page)11 IPage (com.github.pagehelper.IPage)2 PageRowBounds (com.github.pagehelper.PageRowBounds)2 MetaObject (org.apache.ibatis.reflection.MetaObject)2 PageException (com.github.pagehelper.PageException)1 ProviderSqlSource (org.apache.ibatis.builder.annotation.ProviderSqlSource)1 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)1 OpenPage (org.tech.commons.OpenPage)1 People (org.tech.domain.People)1