赞
踩
使用pageHelper实现分页查询,共两步:
一、导入依赖; pom.xml添加依赖:
-
- <!--分页插件 pagehelper -->
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper-spring-boot-starter</artifactId>
- <!-- 特别注意版本问题 <version>1.2.3</version> 123版本报错,循环依赖问题,换成141就OK-->
- <version>1.4.1</version>
- </dependency>
二、添加配置; application.properties添加配置:
- 分页pageHelper
- pagehelper.helper-dialect=mysql
- pagehelper.reasonable=true
- pagehelper.support-methods-arguments=true
-
Mapper层
- /**
- * 查询所有数据
- * @return
- */
- List<User> queryUserInfo();
Mapper.xml
- <select id="queryUserInfo" resultMap="UserEntityMap">
- select * from t_user
- </select>
IUserService层
-
- //查询所有
- List<User> queryUserInfo();
- //查询部分数据
- List<User> findAllUserByPageF(int pageNum,int pageSize);
- //查询详细数据
- PageInfo<User> findAllUserByPageS(int pageNum, int pageSize);
UserServiceImpl实现层
-
- @Override
- public List<User> queryUserInfo() {
- return userMapper.queryUserInfo();
- }
- @Override
- public List<User> findAllUserByPageF(int pageNum, int pageSize) {
- // TODO Auto-generated method stub
- PageHelper.startPage(pageNum, pageSize);
- List<User> lists = userMapper.queryUserInfo();
- return lists;
- }
-
- @Override
- public PageInfo<User> findAllUserByPageS(int pageNum, int pageSize) {
- // TODO Auto-generated method stub
- PageHelper.startPage(pageNum, pageSize);
- List<User> lists = userMapper.queryUserInfo();
- PageInfo<User> pageInfo = new PageInfo<User>(lists);
- return pageInfo;
-
- }
UserController层
-
- //分页有统计版
- @GetMapping("/testPageHelper1")
- public PageInfo<User> testPageHelper1(int pageNum,int pageSize){
- PageInfo<User> data = userService.findAllUserByPageS(pageNum, pageSize);
- return data;
- }
- //分页部分版
- @GetMapping("/testPageHelper2")
- public List<User> testPageHelper2(int pageNum,int pageSize){
- List<User> data = userService.findAllUserByPageF(pageNum, pageSize);
- return data;
- }
- //分页最终版
- @GetMapping("testPageHelper3")
- public JsonResult<PageInfo<User>> testPageHelper3(int pageNum,int pageSize) {
- PageInfo<User> data = userService.findAllUserByPageS(pageNum, pageSize);
- return new JsonResult<PageInfo<User>>(OK, data);
- }
最后请求结果
参考链接
原文链接:https://blog.csdn.net/qq_35387940/article/details/91530234
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。