1. 项目概述:全栈厨艺交流平台的技术架构
这套"Java Web厨艺交流平台"采用前后端分离架构,后端基于SpringBoot2框架构建RESTful API服务,前端使用Vue3实现响应式界面,数据持久层选用MyBatis-Plus操作MySQL8.0数据库。作为全栈项目典型范例,其技术栈组合体现了当前企业级开发的黄金标准——SpringBoot提供快速开发能力,Vue3保障前端交互体验,MyBatis-Plus简化数据库操作,MySQL8.0则提供稳定可靠的数据存储。
提示:项目源码包通常包含完整的Maven依赖配置、Vue脚手架工程、SQL初始化脚本以及API文档,建议先通读文档了解整体架构设计
2. 核心技术组件解析
2.1 SpringBoot2后端框架选型
采用SpringBoot 2.7.x版本(对应Spring Framework 5.3.x),相较于旧版具有以下优势:
- 内嵌Tomcat 9.x支持HTTP/2协议
- 改进的Actuator端点安全配置
- 更精细化的自动配置条件评估
- 对JDK 17的兼容性支持
关键配置示例(application.yml):
spring: datasource: url: jdbc:mysql://localhost:3306/cooking_db?useSSL=false&serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+82.2 Vue3前端工程化实践
前端项目采用Vue3 + Vite构建方案,主要技术特性包括:
- Composition API替代Options API
<script setup>语法糖- Pinia状态管理替代Vuex
- TypeScript类型支持
- Element Plus组件库
典型页面组件结构:
/src /views RecipeDetail.vue # 菜谱详情页 UserCenter.vue # 用户中心 /components UploadImage.vue # 图片上传组件 CommentList.vue # 评论列表组件2.3 MyBatis-Plus高效数据操作
MyBatis-Plus 3.5.3版本核心功能:
- 通用Mapper自动CRUD
- Lambda表达式条件构造器
- 分页插件自动拦截
- 乐观锁@Version注解
- 逻辑删除@TableLogic
实体类注解示例:
@Data @TableName("t_recipe") public class Recipe { @TableId(type = IdType.AUTO) private Long id; private String title; @TableField("cover_img") private String coverImg; @TableLogic private Integer deleted; }2.4 MySQL8.0数据库优化
项目使用MySQL8.0.28+版本,关键优化点:
- 采用utf8mb4字符集存储emoji
- 使用窗口函数实现复杂统计
- JSON字段存储菜谱步骤
- 全文索引加速搜索
- 事务隔离级别设为READ-COMMITTED
建表示例:
CREATE TABLE `t_recipe` ( `id` bigint NOT NULL AUTO_INCREMENT, `title` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `content` json DEFAULT NULL, `user_id` bigint NOT NULL, `view_count` int DEFAULT '0', `create_time` datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), FULLTEXT KEY `ft_title` (`title`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;3. 核心功能模块实现
3.1 用户认证与授权
采用JWT + Spring Security方案:
- 登录接口颁发Token
- 自定义UserDetailsService
- 密码BCrypt加密存储
- 注解式权限控制
- 接口访问日志记录
安全配置核心代码:
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() .antMatchers("/api/auth/**").permitAll() .antMatchers("/api/admin/**").hasRole("ADMIN") .anyRequest().authenticated() .and() .addFilter(new JwtAuthenticationFilter(authenticationManager())) .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS); } }3.2 菜谱发布与管理
核心业务流程:
- 富文本编辑器(Quill)内容处理
- 图片上传阿里云OSS
- 标签多对多关联存储
- 草稿自动保存功能
- 版本控制与历史记录
事务处理示例:
@Transactional public Long publishRecipe(RecipeDTO dto) { // 1. 保存基础信息 Recipe recipe = convertToEntity(dto); recipeMapper.insert(recipe); // 2. 处理标签关联 tagService.batchRelateTags(recipe.getId(), dto.getTagIds()); // 3. 上传封面图 String url = ossService.upload(dto.getCoverFile()); recipe.setCoverImg(url); recipeMapper.updateById(recipe); return recipe.getId(); }3.3 互动交流功能
实现要点:
- 评论树形结构存储(parent_id自关联)
- @提及用户通知
- 敏感词过滤(DFA算法)
- 点赞防刷(Redis计数器)
- 收藏夹分组管理
评论表设计:
CREATE TABLE `t_comment` ( `id` bigint NOT NULL AUTO_INCREMENT, `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `user_id` bigint NOT NULL, `recipe_id` bigint NOT NULL, `parent_id` bigint DEFAULT NULL, `create_time` datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `idx_recipe` (`recipe_id`), KEY `idx_parent` (`parent_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;4. 部署与运维实践
4.1 开发环境搭建
- JDK17安装配置环境变量
- IDEA安装Lombok插件
- Node.js 16.x + pnpm包管理
- MySQL8.0配置my.cnf
- Redis6.x缓存服务
关键开发依赖:
<!-- 后端POM片段 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 前端package.json片段 --> "dependencies": { "vue": "^3.2.47", "pinia": "^2.0.33", "element-plus": "^2.3.3", "axios": "^1.3.4" }4.2 生产环境部署
Docker Compose方案:
version: '3' services: mysql: image: mysql:8.0.32 environment: MYSQL_ROOT_PASSWORD: yourpassword volumes: - ./mysql/data:/var/lib/mysql - ./mysql/conf:/etc/mysql/conf.d ports: - "3306:3306" redis: image: redis:6.2-alpine ports: - "6379:6379" backend: build: ./backend ports: - "8080:8080" depends_on: - mysql - redis frontend: build: ./frontend ports: - "80:80"4.3 性能优化策略
- Nginx静态资源缓存
- API响应Gzip压缩
- MySQL查询优化(EXPLAIN分析)
- Redis缓存热点数据
- 前端路由懒加载
慢查询监控配置:
-- 开启慢查询日志 SET GLOBAL slow_query_log = 'ON'; SET GLOBAL long_query_time = 1; SET GLOBAL slow_query_log_file = '/var/log/mysql/mysql-slow.log';5. 典型问题排查指南
5.1 跨域问题解决方案
开发环境配置:
@Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE") .allowedHeaders("*") .maxAge(3600); } }生产环境推荐Nginx配置:
location /api/ { add_header 'Access-Control-Allow-Origin' $http_origin; add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS'; add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization'; if ($request_method = 'OPTIONS') { return 204; } proxy_pass http://backend:8080; }5.2 MyBatis-Plus常见异常
- 分页失效:检查是否添加分页插件
@Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return interceptor; } }字段映射错误:确认@TableField注解值
乐观锁冲突:确保@Version字段存在且版本号递增
5.3 Vue3开发调试技巧
- Chrome安装Vue Devtools 6.5
- 组件props类型校验:
interface Props { recipeId: number editable?: boolean } const props = defineProps<Props>()- 全局错误处理:
app.config.errorHandler = (err) => { console.error('[Vue Error]', err) showErrorDialog(err.message) }6. 项目扩展方向建议
- 移动端适配:开发uniapp版本
- 智能推荐:基于用户行为的协同过滤
- 视频教程:集成七牛云点播服务
- 直播功能:使用WebRTC技术
- 数据分析:ELK日志收集分析
推荐系统伪代码实现:
# 基于物品的协同过滤 def recommend_recipes(user_id): user_history = get_user_behavior(user_id) similar_items = calculate_similarity(user_history) return sort_by_score(similar_items)