SpringBoot集成pagehelper分页
SpringBoot集成pagehelper分页
1.添加pagehelper包:
<!--pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
2.yml配置文件中添加配置:
#pagehelper pagehelper: helper-dialect: mysql reasonable: false supportMethodsArguments: true pageSizeZero: false params: count=countSql
3.Service中添加代码:
@Override public Page<LogModel> pageList(LogModel record, int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); return iLogDao.pageList(record); }
PageHelper是通用,会在执行pageList sql 之前执行count语句。
pageNum页码
pageSize每页显示条数
record查询条件
pageList的查询语句跟普通的list查询无异。
4.PageInfo:
PageInfo可以使用PageHelper的,也可以自己定义。主要是定义返回的数据结构的。
我前端使用的是LayUI,返回的数据结构是自己定义的。
import java.io.Serializable; import java.util.Collection; import java.util.List; import com.github.pagehelper.Page; public class PageInfo<T> implements Serializable{ private static final long serialVersionUID = 6502920935250686910L; private int pageNum; private int pageSize; private long total; private int pages; private List<T> datas; private boolean isFirstPage = false; private boolean isLastPage = false; private int status = 0; public PageInfo() { super(); } public PageInfo(List<T> list) { if(list instanceof Page) { Page<T> page = (Page<T>) list; this.pageNum = page.getPageNum(); this.pageSize = page.getPageSize(); this.pages = page.getPages(); this.total = page.getTotal(); this.datas = page; } else if (list instanceof Collection) { this.pageNum = 1; this.pageSize = list.size(); this.pages = 1; this.total = list.size(); this.datas = list; } if(list instanceof Collection) { judgePageBoudary(); } } /** * 判定页面边界 */ private void judgePageBoudary() { this.isFirstPage = this.pageNum == 1; this.isLastPage = pageNum == pages; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public int getPages() { return pages; } public void setPages(int pages) { this.pages = pages; } public List<T> getDatas() { return datas; } public void setDatas(List<T> datas) { this.datas = datas; } public boolean isFirstPage() { return isFirstPage; } public void setFirstPage(boolean isFirstPage) { this.isFirstPage = isFirstPage; } public boolean isLastPage() { return isLastPage; } public void setLastPage(boolean isLastPage) { this.isLastPage = isLastPage; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } }
5.Controller:
@RequestMapping(value = "/user/pageList", method = RequestMethod.GET) @ResponseBody public Object pageList(HttpServletRequest request, UserModel model, @RequestParam int pageNum, @RequestParam int pageSize) { List<UserModel> list = iUserService.pageList(model, pageNum, pageSize); PageInfo<UserModel> pageInfo = new PageInfo<UserModel>(list); return pageInfo; }
SpringBoot集成PageHelper分页完成。
作者:Se7end
声明:本博客文章为原创,只代表本人在工作学习中某一时间内总结的观点或结论。转载时请在文章页面明显位置给出原文链接。