【PageInfo】 分页中有关PageInfo、PageHelper、Page等相关操作
温馨提示:
本文最后更新于 2018年11月23日,已超过 2,184 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
重点1:PageInfo的输出结果
1、PageInfo的源代码
public class PageInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//由于startRow和endRow不常用,这里说个具体的用法
//可以在页面中"显示startRow到endRow 共size条数据"
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List<T> list;
//前一页
private int prePage;
//下一页
private int nextPage;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
//导航条上的第一页
private int navigateFirstPage;
//导航条上的最后一页
private int navigateLastPage;
public PageInfo() {
}
/**
* 包装Page对象
*
* @param list
*/
public PageInfo(List<T> list) {
this(list, 8);
}
/**
* 包装Page对象
*
* @param list page结果
* @param navigatePages 页码数量
*/
public PageInfo(List<T> list, int navigatePages) {
if (list instanceof Page) {
Page page = (Page) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.pages = page.getPages();
this.list = page;
this.size = page.size();
this.total = page.getTotal();
//由于结果是>startRow的,所以实际的需要+1
if (this.size == 0) {
this.startRow = 0;
this.endRow = 0;
} else {
this.startRow = page.getStartRow() + 1;
//计算实际的endRow(最后一页的时候特殊)
this.endRow = this.startRow - 1 + this.size;
}
} else if (list instanceof Collection) {
this.pageNum = 1;
this.pageSize = list.size();
this.pages = this.pageSize > 0 ? 1 : 0;
this.list = list;
this.size = list.size();
this.total = list.size();
this.startRow = 0;
this.endRow = list.size() > 0 ? list.size() - 1 : 0;
}
if (list instanceof Collection) {
this.navigatePages = navigatePages;
//计算导航页
calcNavigatepageNums();
//计算前后页,第一页,最后一页
calcPage();
//判断页面边界
judgePageBoudary();
}
}
}
重点2:具体的使用
//使用分页插件
//传入查询的页码,以及显示的条数
PageHelper.startPage(pn, 10);
List<Book> bookList = bookService.findBookByCategory(cId);
//使用pageInfo包装查询后的结果,封装了详细的查询数据,其中参数5是页码导航连续显示的页数
PageInfo pageInfo = new PageInfo(bookList,5);
//使用model进行返回
//model.addAttribute("pageInfo", page);
//对于ajax返回json数据,可以使用一个Msg类,方便浏览器更方便解析
return Msg.success().add("pageInfo", pageInfo);
可以直接返回pageInfo,可以得到如下的信息:
{“pageNum”:1,”pageSize”:10,”size”:10,”startRow”:1,”endRow”:10,”total”:64,”pages”:7,”list”:[{“bId”:2,”bName”:”java学习”,”price”:20.0,”author”:”jj”,”cId”:1,”category”:{“cId”:1,”cName”:”学习用书”}},{“bId”:12,”bName”:”dang_9_书1”,”price”:20.1,”author”:”saka1”,”cId”:1,”category”:{“cId”:1,”cName”:”学习用书”}},{“bId”:18,”bName”:”dang_15_书1”,”price”:20.1,”author”:”saka1”,”cId”:1,”category”:{“cId”:1,”cName”:”学习用书”}},{“bId”:21,”bName”:”dang_18_书1”,”price”:20.1,”author”:”saka1”,”cId”:1,”category”:{“cId”:1,”cName”:”学习用书”}},{“bId”:24,”bName”:”dang_21_书1”,”price”:20.1,”author”:”saka1”,”cId”:1,”category”:{“cId”:1,”cName”:”学习用书”}},{“bId”:27,”bName”:”dang_24_书1”,”price”:20.1,”author”:”saka1”,”cId”:1,”category”:{“cId”:1,”cName”:”学习用书”}},{“bId”:30,”bName”:”dang_27_书1”,”price”:20.1,”author”:”saka1”,”cId”:1,”category”:{“cId”:1,”cName”:”学习用书”}},{“bId”:33,”bName”:”dang_30_书1”,”price”:20.1,”author”:”saka1”,”cId”:1,”category”:{“cId”:1,”cName”:”学习用书”}},{“bId”:36,”bName”:”dang_33_书1”,”price”:20.1,”author”:”saka1”,”cId”:1,”category”:{“cId”:1,”cName”:”学习用书”}},{“bId”:39,”bName”:”dang_36_书1”,”price”:20.1,”author”:”saka1”,”cId”:1,”category”:{“cId”:1,”cName”:”学习用书”}}],”prePage”:0,”nextPage”:2,”isFirstPage”:true,”isLastPage”:false,”hasPreviousPage”:false,”hasNextPage”:true,”navigatePages”:5,”navigatepageNums”:[1,2,3,4,5],”navigateFirstPage”:1,”navigateLastPage”:5,”lastPage”:5,”firstPage”:1}
在此可以看出,list中存放的信息时在使用的第三个构造方法所传递的所查询的book的信息
再比如:
/**
* 分页查询
*
* @param vo
* @return
*/
@Override
public PageInfo<Article> findPageBreakByCondition(ArticleConditionVO vo) {
PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
List<BizArticle> list = bizArticleMapper.findPageBreakByCondition(vo);
if (CollectionUtils.isEmpty(list)) {
return null;
}
List<Long> ids = new ArrayList<>();
for (BizArticle bizArticle : list) {
ids.add(bizArticle.getId());
}
List<BizArticle> listTag = bizArticleMapper.listTagsByArticleId(ids);
// listTag, 重新组装数据为{id: Article}
Map<Long, BizArticle> tagMap = new LinkedHashMap<>(listTag.size());
for (BizArticle bizArticle : listTag) {
tagMap.put(bizArticle.getId(), bizArticle);
}
List<Article> boList = new LinkedList<>();
for (BizArticle bizArticle : list) {
BizArticle tagArticle = tagMap.get(bizArticle.getId());
bizArticle.setTags(tagArticle.getTags());
boList.add(new Article(bizArticle));
}
PageInfo bean = new PageInfo<BizArticle>(list);
bean.setList(boList);
return bean;
}
再或者
package com.app.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.app.entity.User;
import com.app.servcie.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/insertUser")
@ResponseBody
public User insertUser(User user) {
userService.saveUser(user);
User resultUser = userService.findUserById(user.getId());
return resultUser;
}
@RequestMapping("/getAllUser")
@ResponseBody
public List<User> getAllUser(@RequestParam(required = false, defaultValue = "1") Integer startPage,
@RequestParam(required = false, defaultValue = "5") Integer PageSize) {
PageHelper.startPage(startPage, PageSize);
List<User> users = new ArrayList<>();
users = userService.findAllUser();
PageInfo<User> pi = new PageInfo<>(users);
return users;
}
}
重点3:Page
public class Page<E> extends ArrayList<E> implements Closeable {
private static final long serialVersionUID = 1L;
/**
* 页码,从1开始
*/
private int pageNum;
/**
* 页面大小
*/
private int pageSize;
/**
* 起始行
*/
private int startRow;
/**
* 末行
*/
private int endRow;
/**
* 总数
*/
private long total;
/**
* 总页数
*/
private int pages;
/**
* 包含count查询
*/
private boolean count = true;
/**
* 分页合理化
*/
private Boolean reasonable;
/**
* 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
*/
private Boolean pageSizeZero;
/**
* 进行count查询的列名
*/
private String countColumn;
/**
* 排序
*/
private String orderBy;
/**
* 只增加排序
*/
private boolean orderByOnly;
public Page() {
super();
}
public Page(int pageNum, int pageSize) {
this(pageNum, pageSize, true, null);
}
public Page(int pageNum, int pageSize, boolean count) {
this(pageNum, pageSize, count, null);
}
private Page(int pageNum, int pageSize, boolean count, Boolean reasonable) {
super(0);
if (pageNum == 1 && pageSize == Integer.MAX_VALUE) {
pageSizeZero = true;
pageSize = 0;
}
this.pageNum = pageNum;
this.pageSize = pageSize;
this.count = count;
calculateStartAndEndRow();
setReasonable(reasonable);
}
}
注意:page是一个ArryaList的抽象类!
重点4:Pagehelper
public class PageHelper extends PageMethod implements Dialect {
private PageParams pageParams;
private PageAutoDialect autoDialect;
@Override
public boolean skip(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {
if(ms.getId().endsWith(MSUtils.COUNT)){
throw new RuntimeException("在系统中发现了多个分页插件,请检查系统配置!");
}
Page page = pageParams.getPage(parameterObject, rowBounds);
if (page == null) {
return true;
} else {
//设置默认的 count 列
if(StringUtil.isEmpty(page.getCountColumn())){
page.setCountColumn(pageParams.getCountColumn());
}
autoDialect.initDelegateDialect(ms);
return false;
}
}
@Override
public boolean beforeCount(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {
return autoDialect.getDelegate().beforeCount(ms, parameterObject, rowBounds);
}
@Override
public String getCountSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey countKey) {
return autoDialect.getDelegate().getCountSql(ms, boundSql, parameterObject, rowBounds, countKey);
}
@Override
public boolean afterCount(long count, Object parameterObject, RowBounds rowBounds) {
return autoDialect.getDelegate().afterCount(count, parameterObject, rowBounds);
}
@Override
public Object processParameterObject(MappedStatement ms, Object parameterObject, BoundSql boundSql, CacheKey pageKey) {
return autoDialect.getDelegate().processParameterObject(ms, parameterObject, boundSql, pageKey);
}
.............
}
前端使用:
<#-- 分页组件 -->
<#macro pageBar>
<#if page?exists && (page.pages > 1)>
<nav class="pagination"
data-url="${config.siteUrl}/${url?if_exists}"
data-search="${(model.keywords == null || model.keywords == '')?string('false', 'true')}"
data-total-page="${page.pages?c}"
data-current-page="${page.pageNum?c}"
data-pre="${page.prePage}"
data-next="${page.nextPage}"></nav>
</#if>
</#macro>
正文到此结束
- 本文标签: JavaWeb Mybatis
- 本文链接: http://www.unknowtime.top/article/31
- 版权声明: 本文由仓颉大哥原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权