SpringBoot:整合PageHelper分页插件

郎家岭伯爵 2023年02月01日 418次浏览

理论

PageHelper 是基于 Mybatis 提供的一个第三方分页插件,在基于 Mybatis 的项目中使用非常方便。

实践

pom.xml引入依赖

PageHelper 的依赖:

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.8</version>
</dependency>

需要引入的其它依赖:

Knife4j 和 Springfox 为 Knife4j 依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>


<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <!--在引用时请在maven中央仓库搜索3.X最新版本号-->
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

application.properties

PageHelper 配置项:

# 分页框架
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

需要开启的其它配置项:

# 解决 Springfox 3.X 与 SpringBoot 2.6.X 的版本冲突问题
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

# 开启增强配置
knife4j.enable=true
# 开启Swagger的Basic认证功能,默认是false
knife4j.basic.enable=false
knife4j.basic.username=langjialing
knife4j.basic.password=123456

spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver


mybatis.mapper-locations=classpath:mapper/*.xml

# 开启 mybatis 驼峰映射
mybatis.configuration.map-underscore-to-camel-case=true

Controller层

使用 PageHelper 的注意点:

  1. 调用静态方法 PageHelper.startPage(pageNo, pageSize) 后只有下一次查询会使用分页,调用过一次查询分页参数自动清除
  2. PageHelper.startPage(pageNo, pageSize) 必须放在查询之前,需知道是调用查询时使用到了分页,是属于服务端分页,而不是客户端分页。
package com.langjialing.springbootpagehelperdemo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.langjialing.springbootpagehelperdemo.entity.User;
import com.langjialing.springbootpagehelperdemo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 郎家岭伯爵
 * @since 2023-01-11
 */
@Api(tags = "用户操作")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("查询所有用户")
    @GetMapping("/getAllUsers")
    public PageInfo<User> getAllUsers(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "5") int pageSize){
        PageHelper.startPage(pageNo, pageSize);
        List<User> allUsers = userService.getAllUsers();
        return new PageInfo<>(allUsers);
    }

}

至此,PageHelper 分页插件的使用在 Controller 层就已经结束了,其它部分代码无变化。但为了保持 DEMO 的完整性,本文接下来会补充其它部分代码。

User实体类

package com.langjialing.springbootpagehelperdemo.entity;

import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @author 郎家岭伯爵
 * @since 2023-01-11
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value="User对象", description="用户表")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "用户ID")
    private Integer userId;

    @ApiModelProperty(value = "用户名")
    private String userName;

    @ApiModelProperty(value = "用户密码")
    private String password;

}

Service层

Service 接口类:

package com.langjialing.springbootpagehelperdemo.service;

import com.langjialing.springbootpagehelperdemo.entity.User;

import java.util.List;

/**
 * <p>
 * 用户表 服务类
 * </p>
 *
 * @author 郎家岭伯爵
 * @since 2023-01-11
 */
public interface IUserService {

    /**
     * 分页查询用户。
     * @return
     */
    List<User> getAllUsers();
}

Service 接口实现类:

package com.langjialing.springbootpagehelperdemo.service.impl;

import com.langjialing.springbootpagehelperdemo.entity.User;
import com.langjialing.springbootpagehelperdemo.mapper.UserMapper;
import com.langjialing.springbootpagehelperdemo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 郎家岭伯爵
 * @since 2023-01-11
 */
@Service
public class UserServiceImpl  implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUsers() {
        return userMapper.getAllUsers();
    }
}

mapper文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.langjialing.springbootpagehelperdemo.mapper.UserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.langjialing.springbootpagehelperdemo.entity.User">
        <result column="user_name" property="userName" />
        <result column="password" property="password" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        user_id, user_name, password
    </sql>

    <select id="getAllUsers" resultType="com.langjialing.springbootpagehelperdemo.entity.User">
        select <include refid="Base_Column_List"></include>
        from `user`
    </select>

</mapper>

mapper类

package com.langjialing.springbootpagehelperdemo.mapper;

import com.langjialing.springbootpagehelperdemo.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author 郎家岭伯爵
 * @since 2023-01-11
 */
@Mapper
public interface UserMapper {

    /**
     * 分页查询用户。
     * @return
     */
    List<User> getAllUsers();
}

总结

SpringBoot 整合 PageHelper 插件使用。

赞助页面示例