JavaWeb开发_Day10

参考课程:

黑马程序员 JavaWeb开发教程】

[https://www.bilibili.com/video/BV1m84y1w7Tb]

@ZZHow(ZZHow1024)

综合案例-准备工作

  • 需求说明

    • 部门管理
      • 查询部门列表
      • 删除部门
      • 新增部门
      • 修改部门
    • 员工管理
      • 查询员工列表(分页、条件)
      • 删除员工
      • 新增员工
      • 修改员工
  • 环境搭建

    • 准备数据库表(dept、emp)。
    • 创建 SpringBoot 工程,引入对应的起步依赖(Web、MyBatis Framework、MySQL Driver、Lombok)。
    • 配置文件 application.properties 中引入 MyBatis 的配置信息,准备对应的实体类。
    • 准备对应的 Mapper、Service(接口、实现类)、Controller 基础结构。

    综合案例环境搭建

    综合案例环境搭建

  • 开发规范

    案例基于当前最为主流的前后端分离模式进行开发。

    • Restful
      • REST(REpresentational State Transfer),表述性状态转换,它是一种软件架构风格。
    • 传统风格
    http://1ocalhost:8080/user/getById?id=1        GET:查询 id 为 1 的用户
    http://localhost:8080/user/saveUser            POST:新增用户
    http://1ocalhost:8080/user/updateUser          POST:修改用户
    http://localhost:8080/user/deleteUser?id=1     GET:删除 id 为 1 的用户
    
    • REST 风格(简洁、规范、优雅)
    http://localhost:8080/users/1                  GET:查询 id 为 1 的用户
    http://localhost:8080/users                    POST:新增用户
    http://localhost:8080/users                    PUT:修改用户
    http://localhost:8080/users/1                  DELETE:删除 id 为 1 的用户
    
    • 统一响应结果 Result

      @Data
      @AllArgsConstructor
      @NoArgsConstructor
      public class Result {
          private Integer code;
          private String msg;
          private Object data;
      
          public static Result success() {
              return new Result(200, "success", null);
          }
      
          public static Result success(Object data) {
              return new Result(200, "success", data);
          }
      
          public static Result success(String msg) {
              return new Result(200, "success", msg);
          }
      
          public static Result error() {
              return new Result(500, "error", null);
          }
      
          public static Result error(String msg) {
              return new Result(500, "error", msg);
          }
      
          public static Result error(String msg, Object data) {
              return new Result(500, "error", msg);
          }
      }
      
  • 开发流程

    查看页面原型明确需求 → 阅读接口文档 → 思路分析 → 接口开发 → 接口测试 → 前后端联调

综合案例-部门管理

  • 查询全部部门
    • 思路
      • Browser
      • DeptController
        • 注解:@GetMapping
        1. 接收请求
        2. 调用 service 查询部门
        3. 响应
      • DeptService
        1. 调用 mapper 接口查询
      • DeptMapper
        1. select * from dept;
      • Database
  • Postman 测试
  • 前后端联调
  • 删除部门
    • 思路
      • Browser
      • DeptController
        • 注解:@DeleteMapping、@PathVariable
        1. 接收请求参数 id
        2. 调用 service 删除部门
        3. 响应
      • DeptService
        1. 调用 mapper 接口执行删除操作
      • DeptMapper
        1. delete from dept where id = ?;
      • Database
  • 添加部门
    • 思路
      • Browser
      • DeptController
        • 注解:@PostMapping、@RequestBody
        1. 接收请求参数
        2. 调用 service 新增部门
        3. 响应
      • DeptService
        1. 补充基础属性
        2. 调用 mapper 接口执行新增操作
      • DeptMapper
        1. insert into dept values (?, ?, ?);
      • Database
  • 查询指定 ID 的部门
    • 思路
      • Browser
      • DeptController
        • 注解:@GetMapping
        1. 接收请求参数
        2. 调用 service 查询指定 ID 的部门
        3. 响应
      • DeptService
        1. 调用 mapper 接口执行查询操作
      • DeptMapper
        1. select * from dept where id = ?
      • Database
  • 修改部门
    • 思路
      • Browser
      • DeptController
        • 注解:@PutMapping、@RequestBody
        1. 接收请求参数
        2. 调用 service 修改部门
        3. 响应
      • DeptService
        1. 修改 update_time
        2. 调用 mapper 接口执行修改操作
      • DeptMapper
        1. update dept set name = ?, update_time = ? where id = ?
      • Database
  • 优化 Controller 层
    • 抽取公共请求路径

      一个完整的请求路径是类上的 @RequestMapping 的 value 属性 + 方法上的 @RequestMapping 的 value 属性。

综合案例-员工管理

  • 分页查询
    • 请求参数:页码、每页展示记录数
    • 响应结果:总记录数、结果列表(PageBean)
    • 思路
      • Browser
      • EmpController
        • 注解:@GetMapping
        1. 接收分页参数 page、pageSize
        2. 调用 service 进行分页查询,获取 PageBean
        3. 响应
      • EmpService
        1. 调用 mapper 接口查询总记录数 total
        2. 调用 mapper 接口获取数据列表 rows
        3. 封装 PageBean 对象,返回
      • EmpMapper
        1. select count(*) from emp;
        2. select * from emp limit ?, ?;
      • Database
    • @RequestParam 的属性 defaultValue 可以来设置参数的默认值。
  • 分页插件 PageHelper
    • 添加 Maven 坐标

      <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper-spring-boot-starter</artifactId>
          <version>1.4.7</version>
      </dependency>
      
    • 使用

      • PageHelper.startPage(pageNum, pageSize);
      • List<Emp> list = empMapper.list();
      • Page<Emp> page = (Page<Emp>)list;
  • 条件分页查询
    • 思路
      • Browser
      • EmpController
        • 注解:@GetMapping
        1. 接收分页参数 page、pageSize
        2. 调用 service 进行分页查询,获取 PageBean
        3. 响应
      • EmpService
        1. 调用 mapper 接口查询总记录数 total
        2. 调用 mapper 接口获取数据列表 rows
        3. 封装 PageBean 对象,返回
      • EmpMapper
        1. select * from emp where name like concat('%', ?, '%') and gender = ? and entrydate between ? and ? order by update_time desc; (动态 SQL 实现)
      • Database
  • 删除员工
    • 思路
      • Browser
      • EmpController
        • 注解:@DeleteMapping、@PathVariable
        1. 接收路径参数 id 数组
        2. 调用 service 进行批量删除
        3. 响应
      • EmpService
        1. 调用 mapper 接口进行批量删除操作
      • EmpMapper
        • 标签:<foreach>
        1. delete from emp where id in (?, ?, ?);
      • Database
  • 新增员工
    • 思路
      • Browser
      • EmpController
        • 注解:@PostMapping、@RequestBody
        1. 接收并封装参数
        2. 调用 service 方法保存数据
        3. 响应
      • EmpService
        1. 补充实体基础属性
        2. 调用 mapper 接口进行保存数据操作
      • EmpMapper
        1. insert into emp(…) values(?, ?, ?);
      • Database
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值