springboot pagehelper mybatis分页
pagehelper分页工具,实现了分页功能,使用起来也很方便
1,在pom.xml文件中增加pagehelper引入,代码如下:
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
2,springboot的配置文件中增加pagehelper的配置参数:
# pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql;
3,mybatis配置文件中增加查询的xml配置
<select id="findList" resultMap="BaseResultMap">
select * from emp_info
</select>
4,增加分页工具类,分别是请求类:PageRequest.java, 分页参数类:PageResult.java, 分页工具类:PageUtils.java
PageRequest.java
package com.example.utils; public class PageRequest { /** * 当前页码 */ private int pageNum; /** * 每页数量 */ private int pageSize; 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 PageRequest() { this.pageSize = 5; } }
PageResult.java
package com.example.utils; import java.util.List; public class PageResult { /** * 当前页码 */ private int pageNum; /** * 每页数量 */ private int pageSize; /** * 记录总数 */ private long totalSize; /** * 页码总数 */ private int totalPages; /** * 数据模型 */ private List<?> content; //当前页面第一个元素在数据库中的行号 private int startRow; //当前页面最后一个元素在数据库中的行号 private int endRow; //前一页 private int prePage; //下一页 private int nextPage; //是否为第一页 private boolean isFirstPage; //是否为最后一页 private boolean isLastPage; //是否有前一页 private boolean hasPreviousPage; //是否有下一页 private boolean hasNextPage; //导航页码数 private int navigatePages; //所有导航页号 private int[] navigatepageNums; //导航条上的第一页 private int navigateFirstPage; //导航条上的最后一页 private int navigateLastPage; 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 getTotalSize() { return totalSize; } public void setTotalSize(long totalSize) { this.totalSize = totalSize; } public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public List<?> getContent() { return content; } public void setContent(List<?> content) { this.content = content; } public int getPrePage() { return prePage; } public void setPrePage(int prePage) { this.prePage = prePage; } public int getNextPage() { return nextPage; } public void setNextPage(int nextPage) { this.nextPage = nextPage; } 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 boolean isHasPreviousPage() { return hasPreviousPage; } public void setHasPreviousPage(boolean hasPreviousPage) { this.hasPreviousPage = hasPreviousPage; } public boolean isHasNextPage() { return hasNextPage; } public void setHasNextPage(boolean hasNextPage) { this.hasNextPage = hasNextPage; } public int getNavigatePages() { return navigatePages; } public void setNavigatePages(int navigatePages) { this.navigatePages = navigatePages; } public int[] getNavigatepageNums() { return navigatepageNums; } public void setNavigatepageNums(int[] navigatepageNums) { this.navigatepageNums = navigatepageNums; } public int getNavigateFirstPage() { return navigateFirstPage; } public void setNavigateFirstPage(int navigateFirstPage) { this.navigateFirstPage = navigateFirstPage; } public int getNavigateLastPage() { return navigateLastPage; } public void setNavigateLastPage(int navigateLastPage) { this.navigateLastPage = navigateLastPage; } public int getStartRow() { return startRow; } public void setStartRow(int startRow) { this.startRow = startRow; } public int getEndRow() { return endRow; } public void setEndRow(int endRow) { this.endRow = endRow; } }
PageUtils.java
package com.example.utils; import com.github.pagehelper.PageInfo; public class PageUtils { /** * 将分页信息封装到统一的接口 * @param pageRequest * @param pageInfo * @return */ public static PageResult getPageResult(PageRequest pageRequest, PageInfo<?> pageInfo){ PageResult pageResult = new PageResult(); pageResult.setPageNum(pageInfo.getPageNum()); pageResult.setPageSize(pageInfo.getPageSize()); pageResult.setTotalSize(pageInfo.getTotal()); pageResult.setTotalPages(pageInfo.getPages()); pageResult.setContent(pageInfo.getList()); pageResult.setNavigatepageNums(pageInfo.getNavigatepageNums()); pageResult.setHasNextPage(pageInfo.isHasNextPage()); pageResult.setHasPreviousPage(pageInfo.isHasPreviousPage()); pageResult.setPrePage(pageInfo.getPrePage()); pageResult.setNextPage(pageInfo.getNextPage()); return pageResult; } }
EmpInfoDao.java代码:
package com.example.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.example.entity.EmpInfo; @Mapper public interface EmpInfoDao { int addEmpInfo(EmpInfo empInfo); EmpInfo empLogin(EmpInfo empInfo); List<EmpInfo> findList(); }
EmpInfoService.java
package com.example.dao.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.dao.EmpInfoDao; import com.example.dao.service.EmpInfoService; import com.example.entity.EmpInfo; import com.example.utils.PageRequest; import com.example.utils.PageResult; import com.example.utils.PageUtils; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @Service public class EmpInfoServiceImp implements EmpInfoService{ @Autowired private EmpInfoDao dao; public int addEmpInfo(EmpInfo empInfo) { return dao.addEmpInfo(empInfo); } public EmpInfo empLogin(EmpInfo empInfo){ return dao.empLogin(empInfo); } public PageResult findList(PageRequest pageRequest) { return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest)); } /** * 调用分页插件完成分页 * @param pageQuery * @return */ private PageInfo<EmpInfo> getPageInfo(PageRequest pageRequest) { int pageNum = pageRequest.getPageNum(); int pageSize = pageRequest.getPageSize(); PageHelper.startPage(pageNum, pageSize); List<EmpInfo> sysMenus = dao.findList(); return new PageInfo<EmpInfo>(sysMenus); } }
EmpInfoService.java
package com.example.dao.service; import org.springframework.stereotype.Service; import com.example.entity.EmpInfo; import com.example.utils.PageRequest; import com.example.utils.PageResult; @Service public interface EmpInfoService { int addEmpInfo(EmpInfo empInfo); EmpInfo empLogin(EmpInfo empInfo); PageResult findList(PageRequest pageRequest); }
EmpInfoController.java
package com.example.controller; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.example.dao.service.EmpInfoService; import com.example.entity.EmpInfo; import com.example.utils.CommonUtils; import com.example.utils.PageRequest; import com.example.utils.PageResult; import com.example.utils.StringUtils; @Controller public class EmpInfoController { @Autowired @Qualifier("empInfoServiceImp") private EmpInfoService service; @RequestMapping("emplist") public String findList(Model model, @RequestParam Map<String, Object> map){ Logger logger =LoggerFactory.getLogger(this.getClass()); PageRequest pageInfo = new PageRequest(); pageInfo.setPageNum(CommonUtils.toSafeInt(map.get("pageNum"))); //pageInfo.setPageSize(CommonUtils.toSafeInt(map.get("pageSize"))); logger.debug("pageNum======" + pageInfo.getPageNum()); logger.debug("pageSize======" + pageInfo.getPageSize()); PageResult pageResult = service.findList(pageInfo); model.addAttribute("pageInfo", pageResult); return "user/emplist"; } }
页面显示 emplist.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Title</title> <link rel="stylesheet" href="test.css" type="text/css" /> </head> <body> <h1>employee information</h1> <!--显示分页信息--> <div class="modal-footer no-margin-top"> <div> <table class="table table-hover" id="dataTable1" border="1px" width="80%"> <tr> <th>用户ID</th> <th>用户名</th> <th>出生年月</th> <th>年龄</th> <th>电子邮箱</th> </tr> <tr th:each="oss:${pageInfo.content}"> <td th:text="${oss.empId}"></td> <td th:text="${oss.empName}"></td> <td th:text="${oss.empBrithDate}"></td> <td th:text="${oss.empAge}"></td> <td th:text="${oss.empEmail}"></td> <th> <a class="btn btn-default" href="index.html" role="button"> <span class="glyphicon glyphicon-floppy-remove" aria-hidden="true"></span> 删除</a> </th> </tr> </table> </div> <div> <a class="btn btn-default" href="/register" role="button"> <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> 新增</a> </div> <div class="col-md-6"> 当前第 [[${pageInfo.pageNum}]]页,共 [[${pageInfo.totalPages}]] 页.一共 [[${pageInfo.totalSize}]] 条记录 </div> <ul class="pagination pull-right no-margin"> <li th:if="${pageInfo.hasPreviousPage}"> <a th:href="'/emplist?pageNum=1'">首页</a> </li> <li class="prev" th:if="${pageInfo.hasPreviousPage}"> <a th:href="'/emplist?pageNum='+${pageInfo.prePage}"> <i class="ace-icon fa fa-angle-double-left">上一页</i> </a> </li> <!--遍历条数--> <li th:each="nav:${pageInfo.navigatepageNums}"> <a th:href="'/emplist?pageNum='+${nav}" th:text="${nav}" th:if="${nav != pageInfo.pageNum}"></a> <span style="font-weight: bold;background: #6faed9;" th:if="${nav == pageInfo.pageNum}" th:text="${nav}" ></span> </li> <li class="next" th:if="${pageInfo.hasNextPage}"> <a th:href="'/emplist?pageNum='+${pageInfo.nextPage}"> <i class="ace-icon fa fa-angle-double-right">下一页</i> </a> </li> <li> <a th:href="'/emplist?pageNum='+${pageInfo.totalPages}">尾页</a> </li> </ul> </div> <div>当前页号:<span th:text="${pageInfo.pageNum}"></span></div> <div>每页条数:<span th:text="${pageInfo.pageSize}"></span></div> <div>起始行号:<span th:text="${pageInfo.startRow}"></span></div> <div>终止行号:<span th:text="${pageInfo.endRow}"></span></div> <div>总结果数:<span th:text="${pageInfo.totalSize}"></span></div> <div>总页数:<span th:text="${pageInfo.totalPages}"></span></div> <hr /> <div>是否为第一页:<span th:text="${pageInfo.isFirstPage}"></span></div> <div>是否为最后一页:<span th:text="${pageInfo.isLastPage}"></span></div> <div>是否有前一页:<span th:text="${pageInfo.hasPreviousPage}"></span></div> <div>是否有下一页:<span th:text="${pageInfo.hasNextPage}"></span></div> </body> </html>
推荐阅读
-
Mybatis实现分页的注意点
-
Java使用MyBatis框架分页的5种方式
-
springboot+springmvc+mybatis项目整合
-
IDEA搭建Springboot+SpringMVC+Mybatis+Mysql(详细、易懂)
-
springmvc+mybatis 做分页sql 语句实例代码
-
MyBatis中使用RowBounds对查询结果集进行分页
-
详解mall整合SpringBoot+MyBatis搭建基本骨架
-
SpringBoot + Mybatis增删改查实战记录
-
springboot使用Mybatis(xml和注解)过程全解析
-
Spring Boot入门系列八(SpringBoot 整合Mybatis)