使用PageInfo进行数据封装
public PageInfo<Admin> getPageInfo(String keyword, Integer pageNum, Integer pageSize) { // 1.调用PageHelper的静态方法开启分页 PageHelper.startPage(pageNum,pageSize); // 2.执行查询 List<Admin> adminList = adminMapper.selectAdminByKeyword(keyword); // 3.封装到PageInfo对象 return new PageInfo<>(adminList); }测试
public class AdminServiceImplTest { @Autowired AdminService adminService; private Gson gson = new GsonBuilder().setPrettyPrinting().create(); @Test public void getPageInfo() { PageInfo<Admin> pageInfo = this.adminService.getPageInfo("tom", 1, 5); Logger logger = LoggerFactory.getLogger(AdminServiceImplTest.class); //以json格式输出方便查看 logger.info("result = {}", this.gson.toJson(pageInfo)); } }输出结果
[result = { "pageNum": 1, "pageSize": 5, "size": 5, "startRow": 1, "endRow": 5, "total": 16, "pages": 4, "list": [ { "id": 9, "loginAcct": "marry", "userPswd": "4297f44b13955235245b2497399d7a93", "userName": "迈瑞", "email": "marry@qq.com", "createTime": "2020-10-03 16:05:13.0" }, { "id": 10, "loginAcct": "marry1", "userPswd": "4297f44b13955235245b2497399d7a93", "userName": "迈瑞", "email": "marry1@qq.com", "createTime": "2020-10-03 16:05:13.0" }, { "id": 11, "loginAcct": "marry2", "userPswd": "4297f44b13955235245b2497399d7a93", "userName": "迈瑞", "email": "marry2@qq.com", "createTime": "2020-10-03 16:05:37.0" }, { "id": 12, "loginAcct": "marry3", "userPswd": "4297f44b13955235245b2497399d7a93", "userName": "迈瑞", "email": "marry3@qq.com", "createTime": "2020-10-03 16:05:37.0" }, { "id": 13, "loginAcct": "marry4", "userPswd": "4297f44b13955235245b2497399d7a93", "userName": "迈瑞", "email": "marry4@qq.com", "createTime": "2020-10-03 16:05:37.0" } ], "firstPage": 1, "prePage": 0, "nextPage": 2, "lastPage": 4, "isFirstPage": true, "isLastPage": false, "hasPreviousPage": false, "hasNextPage": true, "navigatePages": 8, "navigatepageNums": [ 1, 2, 3, 4 ] }]