在SpringBoot项目中我们经常需要对查询的数据进行分页,其实我们可以复用查询所有数据的数据层接口,然后借助PageHelper插件对查询的数据进行分页,然后返回给前端
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.1version>
parent>
<groupId>com.examplegroupId>
<artifactId>mongo-adminartifactId>
<version>0.0.1-SNAPSHOTversion>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.examplegroupId>
<artifactId>mongo-coreartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocity-engine-coreartifactId>
<version>2.3version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-boot-starterartifactId>
<version>3.0.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.22version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.3.0version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.ymlinclude>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
resource>
resources>
build>
project>
#默认datasource配置
#spring:
# datasource:
# type: com.alibaba.druid.pool.DruidDataSource
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/mongo?serverTimezone=UTC
# username: root
# password: root
#整合Druid连接池的datasource配置
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/mongo?characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 30000
validation-query: select 'x'
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
filters: stat,wall,slf4j
max-pool-prepared-statement-per-connection-size: 20
use-global-data-source-stat: true
connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#服务端口
server:
port: 80
#PageHelper配置
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
#Spring整合MP
mybatis-plus:
#定义别名包
type-aliases-package: com.example.mongoadmin.entity
#导入映射文件
mapper-locations: classpath:/mapper/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
#为com.example.mongoadmin.mapper包下的SQL执行打印日志
logging:
level:
com.example.mongoadmin.mapper: info
因为我们可以复用查询全部的数据层代码,即Mapper代码,然后使用PageHelper去做分页,所以我们只需要在Service层实现一个分页查询的服务即可
接口
package com.example.mongoadmin.service;
import com.example.mongoadmin.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.PageInfo;
import java.util.List;
/**
*
* 用户管理 服务类
*
*
* @author ZJ
* @since 2023-06-24
*/
public interface IUserService extends IService<User> {
List<User> findAll();
PageInfo findByPage(int pageNum, int pageSize);
}
具体实现
package com.example.mongoadmin.service.impl;
import com.example.mongoadmin.entity.User;
import com.example.mongoadmin.mapper.UserMapper;
import com.example.mongoadmin.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
*
* 用户管理 服务实现类
*
*
* @author ZJ
* @since 2023-06-24
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Resource
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public PageInfo findByPage(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> all = userMapper.findAll();
PageInfo<User> pageInfo = new PageInfo<User>(all);
return pageInfo;
}
}
说明:我们使用PageHelper.startPage进行分页,然后调用userMapper.findAll()查询分页数据,最后使用PageInfo封装数据返回
前端通过访问/user/findByPage接口并传递分页参数访问服务
package com.example.mongoadmin.controller;
import com.example.mongoadmin.entity.User;
import com.example.mongoadmin.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
*
* 用户管理 前端控制器
*
*
* @author ZJ
* @since 2023-06-24
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
@GetMapping("/findAll")
public List<User> findAll() {
return userService.findAll();
}
@GetMapping("/findByPage")
public PageInfo findByPage(int pageNum, int pageSize) {
return userService.findByPage(pageNum, pageSize);
}
}
使用ApiFox调用分页接口,查询第1页的5条数据
可以看到,后端也只是查到了5条数据,这其实是PageHelper在findAll方法的SQL调用前给它拼接了limit条件,所以只返回了我们需要的数据,而不是全部数据