idea使用pagehelper实现后端分页功能
第一步:在pom.xml中添加依赖
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.12</version> </dependency>
第二步:在idea的配置文件中添加相关配置:
pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql
第三步:在Dao层编写程序
/** *分页形式查询所有的用户信息 * @return */ @Select("select * from user") public List<Map<String,Object>> QueryPageUsers();
第四步:在服务层编写程序:
/** * 分页查询所有用户 * @param pagenum * @param pagesize * @return */ List<Map<String,Object>> FindUsers(int pagenum,int pagesize);
第五步:在服务实现层编写代码
/** * 分页形式查询所有的用户 * @param pagenum * @param pagesize * @return */ public List<Map<String, Object>> FindUsers(int pagenum, int pagesize) { PageHelper.startPage(pagenum,pagesize); List<Map<String, Object>> maps = selectUser.QueryPageUsers(); return maps; }
第六步:在Controller层编写代码:
/** * 分页形式查询所有的用户 * @param pagenum * @param pagesize * @return */ @RequestMapping(value = "GetPageUsers",method = RequestMethod.POST) public @ResponseBody PageInfo<Map<String, Object>> GetPageUsers(int pagenum,int pagesize){ List<Map<String, Object>> maps = getUserservice.FindUsers(pagenum, pagesize); PageInfo<Map<String, Object>> pagets = new PageInfo<Map<String, Object>>(maps); return pagets; }
第七步:使用Postman进行测试,结果如下:
{ "total": 6, "list": [ { "pass": "user", "level": 1, "sex": "男", "name": "user", "id": "1001", "age": 21 }, { "pass": "admin", "level": 10, "sex": "女", "name": "admin", "id": "1002", "age": 23 } ], "pageNum": 1, "pageSize": 2, "size": 2, "startRow": 1, "endRow": 2, "pages": 3, "prePage": 0, "nextPage": 2, "isFirstPage": true, "isLastPage": false, "hasPreviousPage": false, "hasNextPage": true, "navigatePages": 8, "navigatepageNums": [ 1, 2, 3 ], "navigateFirstPage": 1, "navigateLastPage": 3 }