SpringBoot案例-员工管理-分页查询-PageHelper插件

分页插件PageHepler

在分页功能实现的文章中,文章传送门:SpringBoot案例-员工管理-分页查询-实现_熵240的博客-CSDN博客

其中实现分页功能的主要代码如下:

Mapper接口

package com.example.tlias.mapper;

import com.example.tlias.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface EmpMapper {
    /**
     * 获取数据总数
     *
     * @return
     */
    @Select("select count(*) from emp")
    public long CountAll();

    /**
     * 获取每页数据信息
     *
     * @return
     */
    @Select("select * from emp limit #{start},#{pageSize}")
    public List<Emp> GetData(Integer start, Integer pageSize);

}

 业务实现层

package com.example.tlias.service.impl;

import com.example.tlias.mapper.EmpMapper;
import com.example.tlias.pojo.Emp;
import com.example.tlias.pojo.PageBean;
import com.example.tlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean Page(Integer page, Integer pageSize) {
        // 获取总记录数
        long count = empMapper.CountAll();
        Integer start = page * pageSize - pageSize;
        // 获取分页查询结果列表
        List<Emp> list = empMapper.GetData(start, pageSize);
        // 封装PageBean对象
        PageBean pageBean = new PageBean(count, list);
        return pageBean;

    }
}

使用MyBatis的分页插件PageHepler,可以完成统计总记录数、指定分页参数的功能,我们只需要进行正常的查询功能就可,详细了解PageHepler插件见:如何使用分页插件 (pagehelper.github.io)icon-default.png?t=N6B9https://pagehelper.github.io/docs/howtouse/

具体关键代码如下

Mappper接口

package com.example.tlias.mapper;

import com.example.tlias.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface EmpMapper {
    // todo 原始分页代码
    //    /**
//     * 获取数据总数
//     *
//     * @return
//     */
//    @Select("select count(*) from emp")
//    public long CountAll();
//
//    /**
//     * 获取每页数据信息
//     *
//     * @return
//     */
//    @Select("select * from emp limit #{start},#{pageSize}")
//    public List<Emp> GetData(Integer start, Integer pageSize);

    /**
     * 员工信息的查询
     * 使用pagehepler插件
     *
     * @return
     */
    @Select("select * from emp")
    public List<Emp> list();


}

业务实现层

package com.example.tlias.service.impl;

import com.example.tlias.mapper.EmpMapper;
import com.example.tlias.pojo.Emp;
import com.example.tlias.pojo.PageBean;
import com.example.tlias.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    // todo 原始方法代码
//    public PageBean Page(Integer page, Integer pageSize) {
//        // 获取总记录数
//        long count = empMapper.CountAll();
//        Integer start = page * pageSize - pageSize;
//        // 获取分页查询结果列表
//        List<Emp> list = empMapper.GetData(start, pageSize);
//        // 封装PageBean对象
//        PageBean pageBean = new PageBean(count, list);
//        return pageBean;
//
//    }
    // todo 使用pageHepler插件代码
    public PageBean Page(Integer page, Integer pageSize) {
        // 设置分页参数
        PageHelper.startPage(page, pageSize);
        // 执行正常查询操作
        List<Emp> empList = empMapper.list();
        Page<Emp> p = (Page<Emp>) empList;
        // 封装分页结果PageBean
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;

    }
}

在业务实现层中,首先调用PageHepler插件中的startPage()方法,设置查询页码(page)和页面容量(pageSize),然后调用Mapper接口中的list()方法,获取所有的结果集合,然后将结果集合进行类型转化,最后将查询结果进行封装,其中参数,可直接调用pageHepler中的gettotal()和getResult()方法进行设置。

gettotal()方法获取总数据容量数,执行的SQL语句为:select count(*) from 数据表

getReault()方法获取页面显示的数据列表:执行的SQL语句为:select * from 数据表 limit #{start},#{pageSize},其中start参数以及通过传递的page参数计算出来了。

运行测试

重启SpringBoot项目,依然是启动postman进行测试,请求地址以及请求参数如下:

运行结果为:

{
    "code": 1,
    "msg": "success",
    "data": {
        "total": 17,
        "rows": [
            {
                "id": 1,
                "username": "jinyong",
                "password": "123456",
                "name": "金庸",
                "gender": 1,
                "image": "1.jpg",
                "job": 4,
                "entrydate": "2000-01-01",
                "deptId": 2,
                "creteTime": null,
                "updateTime": "2023-08-07T15:44:50"
            },
            {
                "id": 2,
                "username": "zhangwuji",
                "password": "123456",
                "name": "张无忌",
                "gender": 1,
                "image": "2.jpg",
                "job": 2,
                "entrydate": "2015-01-01",
                "deptId": 2,
                "creteTime": null,
                "updateTime": "2023-08-07T15:44:50"
            },
            {
                "id": 3,
                "username": "yangxiao",
                "password": "123456",
                "name": "杨逍",
                "gender": 1,
                "image": "3.jpg",
                "job": 2,
                "entrydate": "2008-05-01",
                "deptId": 2,
                "creteTime": null,
                "updateTime": "2023-08-07T15:44:50"
            }
        ]
    }
}

 上述两条SQL语句,我们并没有编写,这都是PageHepler插件自动进行执行的,以获取所需的参数 

小结

  • 引入依赖
  • 使用
    •         // 设置分页参数
              PageHelper.startPage(pageNum, pageSize);
              // 执行正常查询操作
              List<Emp> empList = empMapper.list();
              Page<Emp> p = (Page<Emp>) empList;

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于创建独立的、基于Spring的应程序的框架。它简化了Spring应用程序的配置和部署过程,并提供了一套强大的开发工具和约定,使开发人员能够更快地构建高效的应用程序。 MyBatis是一个开源的持久层框架,它可以将SQL语句和数据库操作映射到Java对象中,使得开发人员可以通过简单的配置来实现数据访问层的操作。在Spring Boot中集成MyBatis可以通过添加相应的依赖和配置来实现。 PageHelper是一个用于实现分页功能的MyBatis插件。它可以通过拦截SQL语句并自动添加分页查询的相关信息,从而简化了分页查询的操作。在Spring Boot中集成PageHelper可以通过添加相应的依赖和配置来实现。 Swagger是一个用于生成、描述、调用和可视化RESTful风格的Web服务的工具集。它可以根据代码注解自动生成接口文档,并提供了一个用户友好的界面来测试和调试接口。在Spring Boot中集成Swagger可以通过添加相应的依赖和配置来实现。 下面是集成Spring Boot、MyBatis、PageHelper和Swagger的步骤: 1. 在pom.xml文件中添加Spring Boot、MyBatis、PageHelper和Swagger的依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- PageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> </dependency> <!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> </dependencies> ``` 2. 在application.properties或application.yml文件中配置数据库连接和MyBatis的相关配置: ```properties # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # MyBatis配置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.model # PageHelper配置 pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true # Swagger配置 swagger.enabled=true ``` 3. 创建MyBatis的Mapper接口和对应的XML文件,定义数据库操作的SQL语句和映射关系。 4. 创建Spring Boot的Controller类,定义接口的请求路径和处理方法。 5. 在启动类上添加@EnableSwagger2注解,启用Swagger。 ```java @SpringBootApplication @EnableSwagger2 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 6. 启动应用程序,访问http://localhost:8080/swagger-ui.html可以查看生成的接口文档,并进行接口的测试和调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值