Files
java-beta/README.md
2025-06-18 18:29:40 +08:00

163 lines
4.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 菜品管理系统后端
这是一个基于Spring Boot + MyBatis-Plus的菜品管理系统后端API。
## 技术栈
- **Spring Boot 3.4.6** - Web框架
- **MyBatis-Plus 3.5.3.1** - 数据持久层框架
- **MySQL** - 数据库
- **Lombok** - 简化Java代码
- **Maven** - 项目管理工具
## 项目结构
```
src/
├── main/
│ ├── java/com/example/javatest/
│ │ ├── JavaTestApplication.java # 主启动类
│ │ ├── config/
│ │ │ └── MybatisPlusConfig.java # MyBatis-Plus配置
│ │ ├── controller/
│ │ │ └── DishController.java # 菜品控制器
│ │ ├── entity/
│ │ │ └── Dish.java # 菜品实体类
│ │ ├── mapper/
│ │ │ └── DishMapper.java # 菜品Mapper接口
│ │ └── service/
│ │ ├── DishService.java # 菜品服务接口
│ │ └── impl/
│ │ └── DishServiceImpl.java # 菜品服务实现
│ └── resources/
│ ├── application.properties # 应用配置
│ ├── mapper/
│ │ └── DishMapper.xml # MyBatis映射文件
│ └── sql/
│ └── dishes.sql # 数据库建表脚本
```
## 快速开始
### 1. 环境要求
- JDK 17+
- MySQL 8.0+
- Maven 3.6+
### 2. 数据库配置
1. 创建MySQL数据库
2. 执行 `src/main/resources/sql/dishes.sql` 脚本创建表和测试数据
3. 修改 `application.properties` 中的数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/restaurant_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username=your_username
spring.datasource.password=your_password
```
### 3. 运行项目
```bash
# 克隆项目
git clone <your-repo-url>
# 进入项目目录
cd java-test
# 安装依赖并运行
mvn spring-boot:run
```
项目启动后访问http://localhost:8080
## API接口
### 菜品管理接口
| 方法 | 路径 | 描述 |
|------|------|------|
| GET | `/api/dishes` | 分页查询菜品列表 |
| GET | `/api/dishes/{id}` | 根据ID查询菜品详情 |
| POST | `/api/dishes` | 添加菜品 |
| PUT | `/api/dishes/{id}` | 更新菜品 |
| DELETE | `/api/dishes/{id}` | 删除菜品 |
| PATCH | `/api/dishes/{id}/status` | 更新菜品状态 |
### 分页查询参数
- `page`: 当前页默认1
- `size`: 每页大小默认10
- `dishName`: 菜品名称(模糊查询)
- `category`: 分类筛选
- `status`: 状态筛选
### 示例请求
```bash
# 查询菜品列表
curl "http://localhost:8080/api/dishes?page=1&size=10"
# 根据名称搜索
curl "http://localhost:8080/api/dishes?dishName=宫保鸡丁"
# 根据分类筛选
curl "http://localhost:8080/api/dishes?category=主食"
# 更新菜品状态
curl -X PATCH "http://localhost:8080/api/dishes/1/status" \
-H "Content-Type: application/json" \
-d '{"status": "停售"}'
```
## 数据模型
### Dish (菜品)
| 字段 | 类型 | 说明 |
|------|------|------|
| dishId | Long | 菜品ID (主键) |
| dishName | String | 菜品名称 |
| category | String | 分类 |
| price | BigDecimal | 价格 |
| stock | Integer | 库存 |
| status | String | 状态 (在售/停售) |
| description | String | 描述 |
| imageUrl | String | 图片URL |
| sales | Integer | 销量 |
| tag | String | 标签 |
| isDeleted | Integer | 逻辑删除标记 |
| deletedAt | LocalDateTime | 删除时间 |
| createdAt | LocalDateTime | 创建时间 |
| updatedAt | LocalDateTime | 更新时间 |
## 功能特性
- ✅ CRUD操作
- ✅ 分页查询
- ✅ 条件筛选(名称、分类、状态)
- ✅ 逻辑删除
- ✅ 自动时间戳填充
- ✅ 跨域支持
- ✅ 统一响应格式
## 前端集成
此后端API与Vue3前端菜品管理页面完全兼容支持
- 菜品列表展示
- 分页显示
- 搜索筛选
- 菜品详情查看
- 菜品编辑
- 状态切换
- 菜品删除
## 开发说明
- 使用MyBatis-Plus简化数据操作
- 支持逻辑删除,数据安全
- 自动填充创建时间和更新时间
- ResponseEntity统一响应格式
- 完整的异常处理机制