参考课程:
【黑马程序员 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); } }
- Restful
-
开发流程
查看页面原型明确需求 → 阅读接口文档 → 思路分析 → 接口开发 → 接口测试 → 前后端联调
综合案例-部门管理
- 查询全部部门
- 思路
- Browser
- DeptController
- 注解:@GetMapping
- 接收请求
- 调用 service 查询部门
- 响应
- DeptService
- 调用 mapper 接口查询
- DeptMapper
select * from dept;
- Database
- 思路
- Postman 测试
- 前后端联调
- 删除部门
- 思路
- Browser
- DeptController
- 注解:@DeleteMapping、@PathVariable
- 接收请求参数 id
- 调用 service 删除部门
- 响应
- DeptService
- 调用 mapper 接口执行删除操作
- DeptMapper
delete from dept where id = ?;
- Database
- 思路
- 添加部门
- 思路
- Browser
- DeptController
- 注解:@PostMapping、@RequestBody
- 接收请求参数
- 调用 service 新增部门
- 响应
- DeptService
- 补充基础属性
- 调用 mapper 接口执行新增操作
- DeptMapper
insert into dept values (?, ?, ?);
- Database
- 思路
- 查询指定 ID 的部门
- 思路
- Browser
- DeptController
- 注解:@GetMapping
- 接收请求参数
- 调用 service 查询指定 ID 的部门
- 响应
- DeptService
- 调用 mapper 接口执行查询操作
- DeptMapper
select * from dept where id = ?
- Database
- 思路
- 修改部门
- 思路
- Browser
- DeptController
- 注解:@PutMapping、@RequestBody
- 接收请求参数
- 调用 service 修改部门
- 响应
- DeptService
- 修改 update_time
- 调用 mapper 接口执行修改操作
- DeptMapper
update dept set name = ?, update_time = ? where id = ?
- Database
- 思路
- 优化 Controller 层
-
抽取公共请求路径
一个完整的请求路径是类上的 @RequestMapping 的 value 属性 + 方法上的 @RequestMapping 的 value 属性。
-
综合案例-员工管理
- 分页查询
- 请求参数:页码、每页展示记录数
- 响应结果:总记录数、结果列表(PageBean)
- 思路
- Browser
- EmpController
- 注解:@GetMapping
- 接收分页参数 page、pageSize
- 调用 service 进行分页查询,获取 PageBean
- 响应
- EmpService
- 调用 mapper 接口查询总记录数 total
- 调用 mapper 接口获取数据列表 rows
- 封装 PageBean 对象,返回
- EmpMapper
select count(*) from emp;
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
- 接收分页参数 page、pageSize
- 调用 service 进行分页查询,获取 PageBean
- 响应
- EmpService
- 调用 mapper 接口查询总记录数 total
- 调用 mapper 接口获取数据列表 rows
- 封装 PageBean 对象,返回
- EmpMapper
select * from emp where name like concat('%', ?, '%') and gender = ? and entrydate between ? and ? order by update_time desc;
(动态 SQL 实现)
- Database
- 思路
- 删除员工
- 思路
- Browser
- EmpController
- 注解:@DeleteMapping、@PathVariable
- 接收路径参数 id 数组
- 调用 service 进行批量删除
- 响应
- EmpService
- 调用 mapper 接口进行批量删除操作
- EmpMapper
- 标签:
<foreach>
delete from emp where id in (?, ?, ?);
- 标签:
- Database
- 思路
- 新增员工
- 思路
- Browser
- EmpController
- 注解:@PostMapping、@RequestBody
- 接收并封装参数
- 调用 service 方法保存数据
- 响应
- EmpService
- 补充实体基础属性
- 调用 mapper 接口进行保存数据操作
- EmpMapper
insert into emp(…) values(?, ?, ?);
- Database
- 思路