mysql java 实现分页

来源:互联网 发布:可口可乐昵称瓶 数据 编辑:程序博客网 时间:2024/06/07 00:07

service方法中如下方法:

    @Override                                                                   //查询条件query中要包含pageNo和PageSize参数等查询条件
    public BaseSearchResultDTO<PageFile> queryAuditPages(LandPageQuery query) {
        //总行数
        int records = auditPageDao.queryAuditPageListCount(query);
        if (records == 0) {
            return new BaseSearchResultDTO<PageFile>(query.getPageNo(), query.getPageSize(), 0,
                    new ArrayList<PageFile>());
        }

        List<PageFile> pageFileList = auditPageDao.queryAuditPageList(query); //查询实体类

       //查询结果进行封装返回给前台
        return new BaseSearchResultDTO<PageFile>(query.getPageNo(), query.getPageSize(), records,
                pageFileList);
    }

查询条件 LandPageQuery里封装了分页信息如下:

public class LandPageQuery extends BaseQuery

我把分页信息单独封装出来,所有要分页的类继承这个BaseQuery,BaseQuery内容如下:

     /**
     * 必填,每页数量。
     */
    private Integer pageSize;
    /**
     * 必填,第几页。
     */
    private Integer pageNo;
    /**
     *此处要计算起始页,SQL中要用,写在get方法里就行了
     * 起始页。
     * @return
     */
    public Integer getPageStart() {
        if (pageNo != null && pageNo >= 1) {
            return (pageNo - 1) * pageSize;
        }
        return 0;
    }

   //其他属性的get().......set().....


sql 中要有分页限制:select  *  from ... where .. limit #{pageStart},#{pageSize}


返回结果的封装:

BaseSearchResultDTO属性和构造函数封装如下:

        /**
* 每页记录数
*/
private int countPerPage;
/**
* 当前页码
*/
private int pageIndex;
/**
* 总页数
*/
private int pageCount;
/**
* 总记录数
*/
private int totalCount;
/**
* 当前页上的结果列表
*/
private List<T> resultList;

public BaseSearchResultDTO() {
}
public BaseSearchResultDTO(int pageIndex, int countPerPage, int totalCount,
List<T> resultList) {
this.pageIndex = pageIndex;
this.countPerPage = countPerPage;
this.totalCount = totalCount;
this.resultList = resultList;
this.pageCount = this.calcPageCount(totalCount, countPerPage);
}

        //计算页数

private int calcPageCount(int totalCount, int countPerPage) {
int pCount = totalCount % countPerPage == 0 ? totalCount / countPerPage
: totalCount / countPerPage + 1;
return pCount;
}

       //get().....set().....




原创粉丝点击