Java:Springboot整合PageHelper实现分页

举报
彭世瑜 发表于 2021/08/14 00:42:57 2021/08/14
【摘要】 项目结构 测试代码:https://github.com/mouday/SpringBoot-MyBaits-PageHelper 新建SpringBoot项目 $ tree . ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── example │ │ └── demo │ ...

项目结构

测试代码:https://github.com/mouday/SpringBoot-MyBaits-PageHelper

新建SpringBoot项目

$ tree
.
├── pom.xml
└── src ├── main │   ├── java │   │   └── com │   │ └── example │   │ └── demo │   │ ├── Application.java │   │ ├── bean │   │ │   ├── RequestPage.java │   │ │   └── User.java │   │ ├── controller │   │ │   └── UserController.java │   │ ├── dao │   │ │   └── UserDao.java │   │ └── service │   │ ├── UserService.java │   │ └── impl │   │ └── UserServiceImpl.java │   └── resources │ └── application.properties └── test

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

依赖和配置

pom.xml

<!--MyBatis-->
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version>
</dependency>

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope>
</dependency>

<!--分页插件-->
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version>
</dependency>

<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional>
</dependency>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

application.properties


spring.datasource.url=jdbc:mysql://localhost:3306/data
spring.datasource.username=root
spring.datasource.password=123456

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

表结构

CREATE TABLE `user` (
  `id` int(13) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(33) DEFAULT NULL COMMENT '姓名',
  `age` int(3) DEFAULT NULL COMMENT '年龄',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

相关代码

User.java

package com.example.demo.bean;

import lombok.Data;

@Data
public class User { private Integer id; private String name; private Integer age;
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

RequestPage.java

package com.example.demo.bean;

import org.springframework.util.StringUtils;

/**
 * 分页所需参数
 */
public class RequestPage { private Integer page; private Integer size; /** * 页码,为非必传参数,默认值为 1 */ public Integer getPage() { return StringUtils.isEmpty(page) ? 1 : page; } public void setPage(Integer page) { this.page = page; } /** * 大小,非必传参数,默认值为 10 */ public Integer getSize() { return StringUtils.isEmpty(size) ? 10 : size; } public void setSize(Integer size) { this.size = size; }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

UserDao.java

package com.example.demo.dao;


import com.example.demo.bean.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserDao { @Select("select * from user") Page<User> selectAllUserPage(); @Select("select * from user") List<User> selectAllUserList();
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

UserService.java

package com.example.demo.service;

import com.example.demo.bean.User;
import com.github.pagehelper.Page;

import java.util.List;

public interface UserService { Page<User> selectAllUserPage(); List<User> selectAllUserList();
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

UserServiceImpl.java

package com.example.demo.service.impl;

import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;
import com.example.demo.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService { @Autowired UserDao userDao; @Override public Page<User> selectAllUserPage() { return userDao.selectAllUserPage(); } @Override public List<User> selectAllUserList() { return userDao.selectAllUserList(); }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

UserController.java

package com.example.demo.controller;

import com.example.demo.bean.RequestPage;
import com.example.demo.bean.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController { @Autowired UserService userService; /** * 返回数据列表和分页信息 * @param requestPage * @return */ @GetMapping("selectAllUserPage") public PageInfo<User> selectAllUserPage(RequestPage requestPage){ PageHelper.startPage(requestPage.getPage(), requestPage.getSize()); Page<User> page = userService.selectAllUserPage(); PageInfo<User> pageInfo = new PageInfo<>(page); return pageInfo; } /** * 返回数据列表 * @param requestPage * @return */ @GetMapping("selectAllUserList") public List<User> selectAllUserList(RequestPage requestPage){ PageHelper.startPage(requestPage.getPage(), requestPage.getSize()); List<User> list = userService.selectAllUserList(); return list; }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

访问测试

GET http://localhost:8080/selectAllUserList?page=2&size=1

[ { id: 2, name: "Tom", age: 25 }
]


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

执行sql

==>  Preparing: SELECT count(0) FROM user
==> Parameters: 
<== Columns: count(0)
<== Row: 2
<== Total: 1
==>  Preparing: select * from user LIMIT ?, ?
==> Parameters: 1(Integer), 1(Integer)
<== Columns: id, name, age, money
<== Row: 2, Tom, 25, 30.5
<== Total: 1

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
GET http://localhost:8080/selectAllUserPage?page=2&size=1

{ total: 2, list: [ { id: 2, name: "Tom", age: 25 } ], pageNum: 2, pageSize: 1, size: 1, startRow: 2, endRow: 2, pages: 2, prePage: 1, nextPage: 0, isFirstPage: false, isLastPage: true, hasPreviousPage: true, hasNextPage: false, navigatePages: 8, navigatepageNums: [ 1, 2 ], navigateFirstPage: 1, navigateLastPage: 2
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
==>  Preparing: SELECT count(0) FROM user
==> Parameters: 
<== Columns: count(0)
<== Row: 2
<== Total: 1
==>  Preparing: select * from user LIMIT ?, ?
==> Parameters: 1(Integer), 1(Integer)
<== Columns: id, name, age, money
<== Row: 2, Tom, 25, 30.5
<== Total: 1

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

参考
springboot整合pagehelper实现分页

文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/108019956

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。