2022-03-01 15:21:38 +08:00
|
|
|
|
# 电商基础秒杀项目
|
|
|
|
|
|
2022-03-01 15:40:59 +08:00
|
|
|
|
项目简介:基于SpringBoot,数据库使用MySQL
|
|
|
|
|
|
|
|
|
|
开发工具:IntelliJ IDEA 2021.3.2 (Ultimate Edition)
|
|
|
|
|
|
|
|
|
|
数据库版本:MySQL 5.7
|
|
|
|
|
|
2022-03-01 17:17:29 +08:00
|
|
|
|
项目数据库文件:[点击打开](./Others/sql/flashsale.sql)
|
|
|
|
|
|
|
|
|
|
### 默认用户
|
|
|
|
|
|
|
|
|
|
> 用户名密码均为 `admin`
|
|
|
|
|
|
|
|
|
|
### 层次结构(以User为例,自上向下)
|
|
|
|
|
|
2022-03-02 22:24:19 +08:00
|
|
|
|
| | 目录 | Java对象类型 | 说明 | 举例 |
|
|
|
|
|
| ----------------- | --------------------- | --------------------------------------- | ---------------------------------------- | ------------------------------- |
|
|
|
|
|
| **Controller层** | | | | |
|
|
|
|
|
| Controller | controller | 类 class | | controller.UserController |
|
2022-03-03 22:59:32 +08:00
|
|
|
|
| View Object (VO) | controller/viewobject | 类 class | 将用户Model转化为可供UI使用的View Object | controller.UserController |
|
2022-03-02 22:24:19 +08:00
|
|
|
|
| | | | | |
|
|
|
|
|
| **Service层** | 转换成业务模型 | | | |
|
|
|
|
|
| Service | service | 接口 interface | | service.UserService |
|
|
|
|
|
| ServiceImpl | service/impl | Service接口实现类 | 将DataObject组装成Model | service.impl.UserServiceImpl |
|
|
|
|
|
| Model | service/model | 类 class | 用户模型Model | service.model.UserModel |
|
|
|
|
|
| | | | | |
|
|
|
|
|
| **Dao层** | 对数据库的映射 | | | |
|
|
|
|
|
| Mapper | dao | 接口 interface | | dao.UserDOMapper |
|
|
|
|
|
| Mapping | resources/mapping | Mapper接口实现类 | xml格式;SQL语句 | mapping/UserDOMapper.xml |
|
2022-03-03 22:59:32 +08:00
|
|
|
|
| Data Object (DO) | dataobject | 类 class | | dataobject.UserDO |
|
2022-03-02 22:24:19 +08:00
|
|
|
|
| | | | | |
|
|
|
|
|
| **其他** | | | | |
|
|
|
|
|
| response | response | | 用于处理HTTP请求返回 | response.CommonReturnType |
|
|
|
|
|
| ValidationResult | validator | 类 class | Hibernate Validator 参数验证 | validator\ValidationResult.java |
|
|
|
|
|
| ValidatorImpl | validator | ValidationResult类实现类 | Hibernate Validator 参数验证 | validator\ValidatorImpl.java |
|
|
|
|
|
| | | | | |
|
|
|
|
|
| **异常处理** | 用于返回错误信息 | | | |
|
|
|
|
|
| CommonError | error | 接口 interface | | error.CommonError |
|
|
|
|
|
| EmBusinessError | error | 枚举 enum & CommonError接口实现类 | | error.EmBusinessError |
|
|
|
|
|
| BusinessException | error | CommonError接口实现类 & 继承自Exception | | error.BusinessException |
|
2022-03-01 17:17:59 +08:00
|
|
|
|
|
2022-03-01 17:57:36 +08:00
|
|
|
|
**Tips:** Model与Data Object并非完全一一对应,例如UserModel是由ServiceImpl将UserDO和UserPasswordDO组装而成的。
|
2022-03-03 22:59:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
代码构建顺序:
|
|
|
|
|
|
|
|
|
|
1. 创建Model模型(service/model)
|
|
|
|
|
|
|
|
|
|
2. 创建数据库中数据表
|
|
|
|
|
|
|
|
|
|
3. 使用mybatis-generator生成数据库映射文件(dao ; dataobject ; resources/mapping)
|
|
|
|
|
|
|
|
|
|
4. 创建Service接口(service)
|
|
|
|
|
|
|
|
|
|
5. 创建Service接口实现Impl(service/impl)(class打上 `Service` 标注;方法上打上 `@Transactional` 标签保证是在同一事务中)
|
|
|
|
|
|
|
|
|
|
6. 思考ServiceImpl中需要哪几步,并逐步实现
|
|
|
|
|
|
|
|
|
|
> - 如果需要修改自动生成的数据库语句,先改 resources/mapping 下的xml映射SQL语句,然后修改 dao 下的DOMapper接口
|
|
|
|
|
>
|
|
|
|
|
> - 可以用 `org.springframework.beans` 包中的 `BeanUtils.copyProperties` 进行DO转Model或Model转DO(要字段和类型完全一致的字段才能拷贝,其他不对应字段的需要手动设置)
|
|
|
|
|
|
2022-03-03 23:37:17 +08:00
|
|
|
|
7. Service返回Controller前完成将DO转换为Model,同时进行业务处理,最后返回Model
|
2022-03-03 22:59:32 +08:00
|
|
|
|
|
2022-03-03 23:37:17 +08:00
|
|
|
|
8. 创建Controller(继承BaseController),在class上添加注解
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
@Controller("/item")
|
|
|
|
|
@RequestMapping("/item")
|
|
|
|
|
@CrossOrigin(allowedHeaders = "*", allowCredentials = "true")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
9. 创建Controller方法(添加以下注释)
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
// POST
|
|
|
|
|
@RequestMapping(value = "/create", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
|
|
|
|
@ResponseBody
|
|
|
|
|
|
|
|
|
|
// GET
|
|
|
|
|
@RequestMapping(value = "/get", method = {RequestMethod.GET})
|
|
|
|
|
@ResponseBody
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
10.
|