自学内容网 自学内容网

04-SpringBootWeb案例(下)

3. 员工管理

完成了部门管理的功能开发之后,我们进入到下一环节员工管理功能的开发。
在这里插入图片描述
基于以上原型,我们可以把员工管理功能分为:

  1. 分页查询(今天完成)
  2. 带条件的分页查询(今天完成)
  3. 删除员工(今天完成)
  4. 新增员工(后续完成)
  5. 修改员工(后续完成)

那下面我们就先从分页查询功能开始学习。

3.1 分页查询

3.1.1 基础分页

3.1.1.1 需求分析

我们之前做的查询功能,是将数据库中所有的数据查询出来并展示到页面上,试想如果数据库中的数据有很多(假设有十几万条)的时候,将数据全部展示出来肯定不现实,那如何解决这个问题呢?

使用分页解决这个问题。每次只展示一页的数据,比如:一页展示10条数据,如果还想看其他的数据,可以通过点击页码进行查询。

在这里插入图片描述
要想从数据库中进行分页查询,我们要使用 LIMIT 关键字,格式为:limit 开始索引 每页显示的条数

查询第1页数据的SQL语句是:
select * from emp limit 0,10;
查询第2页数据的SQL语句是:
select * from emp limit 10,10;
查询第3页的数据的SQL语句是:
select * from emp limit 20,10;
观察以上SQL语句,发现: 开始索引一直在改变 , 每页显示条数是固定的
开始索引的计算公式: 开始索引 = (当前页码 - 1) * 每页显示条数

我们继续基于页面原型,继续分析,得出以下结论:

  1. 前端在请求服务端时,传递的参数
    • 当前页码 page
    • 每页显示条数 pageSize
  2. 后端需要响应什么数据给前端
    • 所查询到的数据列表(存储到List 集合中)
    • 总记录数

在这里插入图片描述

后台给前端返回的数据包含:List集合(数据列表)、total(总记录数) 而这两部分我们通常封装到PageBean对象中,并将该对象转换为json格式的数据响应回给浏览器。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {
private Long total; //总记录数
private List rows; //当前页数据列表
}
3.1.1.2 接口文档

员工列表查询

  • 基本信息

请求路径:/emps
请求方式:GET
接口描述:该接口用于员工列表数据的条件分页查询

  • 请求参数

参数格式:queryString
参数说明:

参数名是否必须示例备注
name姓名
gender1性别 , 1 男 , 2 女
begin2010-01-01范围匹配的开始时间(入职日期)
end2020-01-01范围匹配的结束时间(入职日期)
page1分页查询的页码,如果未指定,默认为1
pageSize10分页查询的每页记录数,如果未指定,默认为10

请求数据样例:

/emps?name=张&gender=1&begin=2007-09-01&end=2022-09-
01&page=1&pageSize=10
  • 响应数据

参数格式:application/json
参数说明:

参数名类型是否必须默认值备注其他信息
codenumber必须响应码,1 代表成功,0 代表失败
msgstring非必须提示信息
dataobject[ ]非必须返回的数据
l- totalnumber必须总记录数
l- rowsobject[]必须iditem 类型:object
l- idnumber非必须id
l- usernamestring非必须用户名
l- namestring非必须姓名
l- passwordstring非必须密码
l- entrydatestring非必须入职日期
l- gendernumber非必须性别,1 男 2女
l- imagestring非必须职位, 说明: 1 班主任,2 讲师,3 学工主管, 4 教研主管, 5 咨询师
l- jobnumber非必须图像
l- deptIdnumber非必须部门id
l- createTimestring非必须创建时间
l- updateTimestring非必须更新时间

响应数据样例:

{
"code": 1,
"msg": "success",
"data": {
"total": 2,
"rows": [
 {
"id": 1,
"username": "jinyong",
"password": "123456",
"name": "金庸",
"gender": 1,
"image": "https://web-framework.oss-cnhangzhou.aliyuncs.com/2022-09-02-00-27-53B.jpg",
"job": 2,
"entrydate": "2015-01-01",
"deptId": 2,
"createTime": "2022-09-01T23:06:30",
"updateTime": "2022-09-02T00:29:04"
 },
 {
"id": 2,
"username": "zhangwuji",
"password": "123456",
"name": "张无忌",
"gender": 1,
"image": "https://web-framework.oss-cnhangzhou.aliyuncs.com/2022-09-02-00-27-53B.jpg",
"job": 2,
"entrydate": "2015-01-01",
"deptId": 2,
"createTime": "2022-09-01T23:06:30",
"updateTime": "2022-09-02T00:29:04"
 }
 ]
 }
}
3.1.1.3 思路分析

在这里插入图片描述
分页查询需要的数据,封装在PageBean对象中:
在这里插入图片描述

3.1.1.4 功能开发

通过查看接口文档:员工列表查询

请求路径:/emps
请求方式:GET
请求参数:跟随在请求路径后的参数字符串。 例:/emps?page=1&pageSize=10
响应数据:json格式

EmpController

package com.itheima.controller;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
public class EmpController {
    @Autowired
    private EmpService empService;

    // 条件分页查询
    @GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        // 记录日志
        log.info("分页查询,参数:{},{}", page, pageSize);
        // 调用业务层分页查询功能
        PageBean pageBean = empService.page(page, pageSize);
        // 响应
        return Result.success(pageBean);
    }
}

@RequestParam(defaultValue=“默认值”) //设置请求参数默认值

EmpService

package com.itheima.service;

import com.itheima.pojo.PageBean;
import org.springframework.stereotype.Service;

@Service
public interface EmpService {
    /**
     * 条件分页查询
     *
     * @param page     页码
     * @param pageSize 每页展示记录数
     * @return
     */

    PageBean page(Integer page, Integer pageSize);
}

EmpServiceImpl

package com.itheima.service.impl;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.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) {
        // 1、获取总记录数
        long count = empMapper.count();
        // 2、获取分页查询结果列表
        Integer start = (page - 1) * pageSize;  // 计算起始索引 , 公式:(页码-1)*页大小
        List<Emp> empList = empMapper.page(start, pageSize);
        // 3、封装PageBean对象
        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
    }
}

EmpMapper

package com.itheima.mapper;

import com.itheima.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 count();

    /**
     * 获取当前页的结果列表
     *
     * @param start
     * @param pageSize
     * @return
     */
    @Select("select * from emp limit #{start},#{pageSize}")
    public List<Emp> page(Integer start, Integer pageSize);
}
3.1.1.5 功能测试

功能开发完成后,重新启动项目,使用postman,发起POST请求:
在这里插入图片描述

3.1.1.6 前后端联调

打开浏览器,测试后端功能接口:
在这里插入图片描述

3.1.2 分页插件

3.1.2.1 介绍

前面我们已经完了基础的分页查询,大家会发现:分页查询功能编写起来比较繁琐。
在这里插入图片描述

在Mapper接口中定义两个方法执行两条不同的SQL语句:

  1. 查询总记录数
  2. 指定页码的数据列表

在Service当中,调用Mapper接口的两个方法,分别获取:总记录数、查询结果列表,然后在将 获?>取的数据结果封装到PageBean对象中。 大家思考下:在未来开发其他项目,只要涉及到分页查询 >功能(例:订单、用户、支付、商品),
都必须按照以上操作完成功能开发

结论:原始方式的分页查询,存在着"步骤固定"、"代码频繁"的问题
解决方案:可以使用一些现成的分页插件完成。对于Mybatis来讲现在最主流的就是PageHelper。

PageHelper是Mybatis的一款功能强大、方便易用的分页插件,支持任何形式的单标、多表的
分页查询。
官网:https://pagehelper.github.io/

在这里插入图片描述

在执行empMapper.list()方法时,就是执行:select * from emp 语句,怎么能够实现分页操作呢?
分页插件帮我们完成了以下操作:

  1. 先获取到要执行的SQL语句:select * from emp
  2. 把SQL语句中的字段列表,变为:count(*)
  3. 执行SQL语句:select count(*) from emp //获取到总记录数
  4. 再对要执行的SQL语句:select * from emp 进行改造,在末尾添加 limit ? ,?
  5. 执行改造后的SQL语句:select * from emp limit ? , ?
3.1.2.2 代码实现

当使用了PageHelper分页插件进行分页,就无需再Mapper中进行手动分页了。 在Mapper中我们只
需要进行正常的列表查询即可。在Service层中,调用Mapper的方法之前设置分页参数,在调用
Mapper方法执行查询之后,解析分页结果,并将结果封装到PageBean对象中返回。
1、在pom.xml引入依赖

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

2、EmpMapper

@Mapper
public interface EmpMapper {
//获取当前页的结果列表
@Select("select * from emp")
public List<Emp> page(Integer start, Integer pageSize);
}

3、EmpServiceImpl

@Override
public PageBean page(Integer page, Integer pageSize) {
// 设置分页参数
PageHelper.startPage(page, pageSize);
// 执行分页查询
List<Emp> empList = empMapper.list(name,gender,begin,end);
// 获取分页结果
Page<Emp> p = (Page<Emp>) empList;
//封装PageBean
PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
return pageBean;
}
3.1.2.3 测试

功能开发完成后,我们重启项目工程,打开postman,发起GET请求,访问 :http://localhost:8080/emps?page=1&pageSize=5
在这里插入图片描述

后端程序SQL输出:
在这里插入图片描述


原文地址:https://blog.csdn.net/ioncannontic/article/details/142692776

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!