作者:笙囧同学
项目源码:基于黑马程序员课程扩展开发
适合人群:Java/Vue初学者到进阶开发者

📚 写在前面

大家好,我是笙囧同学!最近跟着黑马程序员的课程学习了小兔鲜电商项目,从一个对电商系统一无所知的小白,到现在能够独立开发和部署完整的电商平台,这个过程收获满满!

为什么写这篇文章?

  • 📝 记录自己的学习历程和踩坑经验
  • 🎯 帮助同样在学习这个项目的同学少走弯路
  • 💡 分享项目中的重要知识点和最佳实践
  • 🔧 提供完整的环境搭建和部署方案

这篇文章你能学到什么?

  • 🏗️ 完整的电商系统开发流程
  • 💻 Spring Boot + Vue 3 技术栈实战
  • 🗄️ 数据库设计和优化技巧
  • 🚀 项目部署和运维经验
  • 🐛 常见问题的解决方案

🎯 我的学习背景

在开始分享之前,先说说我的技术背景,这样大家可以对照自己的情况:

学习前的技术水平:

  • ✅ Java基础:熟悉基本语法,了解面向对象
  • ✅ Spring基础:学过Spring Boot入门,但没有实战经验
  • ✅ 前端基础:会HTML/CSS/JavaScript,Vue 2有一点了解
  • ❌ 数据库:只会基本的增删改查,不懂优化
  • ❌ 项目经验:没有完整的项目开发经验
  • ❌ 部署运维:完全不懂,只会在IDE里跑代码

学习目标:

  • 🎯 掌握完整的电商系统开发流程
  • 🎯 学会前后端分离项目的开发
  • 🎯 理解企业级项目的架构设计
  • 🎯 具备独立开发和部署项目的能力

📋 学习路线图

基于黑马程序员的课程,我制定了一个3个月的学习计划:

2024-04-07 2024-04-14 2024-04-21 2024-04-28 2024-05-05 2024-05-12 2024-05-19 2024-05-26 2024-06-02 2024-06-09 2024-06-16 2024-06-23 2024-06-30 环境搭建 Spring Boot复习 Vue 3基础学习 数据库设计 项目初始化 用户模块开发 商品模块开发 订单模块开发 Vue项目搭建 页面组件开发 状态管理 接口联调 性能优化 部署脚本 项目总结 第一阶段:基础准备 第二阶段:后端开发 第三阶段:前端开发 第四阶段:优化部署 小兔鲜商城学习时间线

🛠️ 第一阶段:环境搭建与基础准备

开发环境清单

跟着黑马的课程,首先要搭建开发环境。这里我踩了不少坑,给大家分享一下:

graph LR
    A[开发环境] --> B[必装软件]
    A --> C[开发工具]
    A --> D[数据库]
    A --> E[其他工具]

    B --> B1[JDK 21 ⭐]
    B --> B2[Node.js 22+]
    B --> B3[Maven 3.9+]
    B --> B4[Git]

    C --> C1[IntelliJ IDEA]
    C --> C2[VS Code]
    C --> C3[Navicat/DBeaver]
    C --> C4[Postman/Apifox]

    D --> D1[MySQL 8.0]
    D --> D2[Redis 6.0+]

    E --> E1[Chrome DevTools]
    E --> E2[Vue DevTools]
    E --> E3[Redis Desktop Manager]

环境搭建踩坑记录

坑1:JDK版本问题

问题:黑马课程用的是JDK 8,但我想用最新的JDK 21
解决

# 下载JDK 21
# 配置JAVA_HOME环境变量
export JAVA_HOME=/path/to/jdk-21
export PATH=$JAVA_HOME/bin:$PATH

# 验证安装
java -version
javac -version

注意事项

  • JDK 21的一些新特性需要Spring Boot 3.0+支持
  • 部分老的依赖可能不兼容,需要升级版本
坑2:MySQL连接问题

问题:MySQL 8.0的认证方式变了,连接报错
解决

-- 修改认证方式
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
FLUSH PRIVILEGES;

-- 或者在连接URL中指定
jdbc:mysql://localhost:3306/xiaotuxian_mall?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
坑3:Node.js版本兼容性

问题:Vue 3项目需要Node.js 16+,但我装的是14
解决

# 使用nvm管理Node.js版本
nvm install 22.16.0
nvm use 22.16.0
nvm alias default 22.16.0

# 验证版本
node --version
npm --version

项目结构理解

黑马的项目结构设计很有学习价值,我画个图帮大家理解:

小兔鲜商城
后端项目
前端项目
数据库脚本
部署文档
src/main/java
cn.edu.xcu.common 公共模块
cn.edu.xcu.lwh 商品模块
cn.edu.xcu.tkk 用户订单模块
config 配置类
security 安全配置
exception 异常处理
utils 工具类
controller 控制器
service 业务层
mapper 数据访问层
entity 实体类
src 源码目录
views 页面组件
components 公共组件
stores 状态管理
apis 接口调用
utils 工具函数

为什么这样设计?

  • 🎯 模块化:按功能模块划分,便于团队协作
  • 🎯 分层架构:Controller-Service-Mapper三层架构,职责清晰
  • 🎯 公共模块:避免代码重复,提高复用性

📚 核心知识点学习

知识点1:Spring Boot自动配置原理

这是我在学习过程中觉得最重要的知识点之一:

graph TD
    A[@SpringBootApplication] --> B[@EnableAutoConfiguration]
    B --> C[spring.factories]
    C --> D[自动配置类]
    D --> E[@ConditionalOnClass]
    D --> F[@ConditionalOnProperty]
    D --> G[@ConditionalOnMissingBean]

    E --> H[类路径检查]
    F --> I[配置属性检查]
    G --> J[Bean存在性检查]

    H --> K[创建Bean]
    I --> K
    J --> K

学习要点

  • Spring Boot如何实现"约定大于配置"
  • 自动配置的条件注解使用
  • 如何自定义自动配置类

实际应用

@Configuration
@ConditionalOnClass(RedisTemplate.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        // 配置序列化器
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}

知识点2:MyBatis Plus条件构造器

这个功能让我彻底告别了手写SQL的痛苦:

QueryWrapper
基础查询
条件查询
排序分页
分组统计
eq 等于
ne 不等于
gt 大于
lt 小于
like 模糊查询
in 范围查询
between 区间查询
isNull 空值查询
orderBy 排序
page 分页
groupBy 分组
having 条件

实战示例

// 商品查询 - 我最开始写的版本
public Page<Goods> searchGoods(GoodsSearchDTO searchDTO) {
    QueryWrapper<Goods> wrapper = new QueryWrapper<>();

    // 商品名称模糊查询
    if (StringUtils.hasText(searchDTO.getName())) {
        wrapper.like("name", searchDTO.getName());
    }

    // 分类筛选
    if (searchDTO.getCategoryId() != null) {
        wrapper.eq("category_id", searchDTO.getCategoryId());
    }

    // 价格区间
    if (searchDTO.getMinPrice() != null) {
        wrapper.ge("price", searchDTO.getMinPrice());
    }
    if (searchDTO.getMaxPrice() != null) {
        wrapper.le("price", searchDTO.getMaxPrice());
    }

    // 按创建时间倒序
    wrapper.orderByDesc("create_time");

    return goodsMapper.selectPage(
        new Page<>(searchDTO.getPageNum(), searchDTO.getPageSize()),
        wrapper
    );
}

// 优化后的Lambda版本 - 跟着黑马老师学的
public Page<Goods> searchGoodsOptimized(GoodsSearchDTO searchDTO) {
    LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
    wrapper.like(StringUtils.hasText(searchDTO.getName()),
                Goods::getName, searchDTO.getName())
           .eq(searchDTO.getCategoryId() != null,
               Goods::getCategoryId, searchDTO.getCategoryId())
           .between(searchDTO.getMinPrice() != null && searchDTO.getMaxPrice() != null,
                   Goods::getPrice, searchDTO.getMinPrice(), searchDTO.getMaxPrice())
           .orderByDesc(Goods::getCreateTime);

    return goodsMapper.selectPage(
        new Page<>(searchDTO.getPageNum(), searchDTO.getPageSize()),
        wrapper
    );
}

学习心得

  • Lambda表达式避免了字段名写错的问题
  • 条件构造器让代码更简洁易读
  • 分页插件的使用大大简化了分页逻辑

知识点3:Vue 3 Composition API

从Vue 2的Options API到Vue 3的Composition API,这个转变让我受益匪浅:

Vue 2 Options API
Vue 3 Composition API
data 数据
methods 方法
computed 计算属性
watch 监听器
生命周期钩子
ref/reactive 响应式
普通函数
computed 计算属性
watch/watchEffect
onMounted等钩子

学习对比

// Vue 2 写法 - 我最开始的代码
export default {
  data() {
    return {
      goodsList: [],
      loading: false,
      searchForm: {
        name: '',
        categoryId: null
      }
    }
  },
  methods: {
    async fetchGoods() {
      this.loading = true
      try {
        const res = await goodsAPI.getGoodsList(this.searchForm)
        this.goodsList = res.data.records
      } catch (error) {
        this.$message.error('获取商品列表失败')
      } finally {
        this.loading = false
      }
    },
    handleSearch() {
      this.fetchGoods()
    }
  },
  computed: {
    filteredGoods() {
      return this.goodsList.filter(item => item.status === 1)
    }
  },
  mounted() {
    this.fetchGoods()
  }
}

// Vue 3 Composition API - 跟着黑马学习后的写法
import { ref, reactive, computed, onMounted } from 'vue'
import { goodsAPI } from '@/apis/goods'
import { ElMessage } from 'element-plus'

export default {
  setup() {
    // 响应式数据
    const goodsList = ref([])
    const loading = ref(false)
    const searchForm = reactive({
      name: '',
      categoryId: null
    })

    // 计算属性
    const filteredGoods = computed(() => {
      return goodsList.value.filter(item => item.status === 1)
    })

    // 方法
    const fetchGoods = async () => {
      loading.value = true
      try {
        const res = await goodsAPI.getGoodsList(searchForm)
        goodsList.value = res.data.records
      } catch (error) {
        ElMessage.error('获取商品列表失败')
      } finally {
        loading.value = false
      }
    }

    const handleSearch = () => {
      fetchGoods()
    }

    // 生命周期
    onMounted(() => {
      fetchGoods()
    })

    return {
      goodsList,
      loading,
      searchForm,
      filteredGoods,
      fetchGoods,
      handleSearch
    }
  }
}

Composition API的优势

  • 🎯 逻辑复用:可以抽取成自定义Hook
  • 🎯 类型推导:TypeScript支持更好
  • 🎯 代码组织:相关逻辑可以组织在一起

🔧 第二阶段:后端开发实战

项目初始化

跟着黑马的课程,我学会了如何正确初始化一个Spring Boot项目:

项目初始化
创建Maven项目
配置pom.xml
创建目录结构
配置application.yml
编写启动类
Spring Boot Starter
MyBatis Plus
MySQL Driver
Redis
JWT
controller 控制器
service 业务层
mapper 数据访问
entity 实体类
config 配置类

pom.xml关键依赖

<dependencies>
    <!-- Spring Boot Web Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Boot Security -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!-- MyBatis Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.12</version>
    </dependency>

    <!-- MySQL Driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.33</version>
    </dependency>

    <!-- Redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <!-- JWT -->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-api</artifactId>
        <version>0.12.6</version>
    </dependency>
</dependencies>

数据库设计学习

这是我觉得最有价值的学习内容之一,黑马老师的数据库设计思路很值得学习:

USER ORDER CART_ITEM ADDRESS ORDER_ITEM GOODS CATEGORY BRAND 用户下单 购物车 收货地址 订单明细 商品订单 购物车商品 商品分类 商品品牌 父子分类

数据库设计要点

  • 🎯 主键设计:使用bigint自增主键,性能更好
  • 🎯 索引设计:为常用查询字段添加索引
  • 🎯 字段类型:合理选择字段类型,节省存储空间
  • 🎯 JSON字段:MySQL 8.0的JSON类型存储复杂数据

用户认证模块开发

这是我学习过程中觉得最复杂的部分,涉及Spring Security + JWT:

客户端 Spring Security JWT工具类 UserService Redis 数据库 用户登录流程 提交登录表单 调用认证服务 验证用户名密码 返回用户信息 生成JWT Token 返回Token 缓存用户会话 返回认证结果 返回Token和用户信息 后续请求验证 请求API(携带Token) 验证Token 检查会话状态 返回会话信息 验证通过 执行业务逻辑 返回业务数据 客户端 Spring Security JWT工具类 UserService Redis 数据库

JWT工具类实现

@Component
@Slf4j
public class JwtUtils {

    @Value("${jwt.secret}")
    private String secret;

    @Value("${jwt.expiration}")
    private Long expiration;

    // 生成Token
    public String generateToken(UserDetails userDetails) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("username", userDetails.getUsername());
        claims.put("authorities", userDetails.getAuthorities());
        return createToken(claims, userDetails.getUsername());
    }

    // 创建Token
    private String createToken(Map<String, Object> claims, String subject) {
        Date now = new Date();
        Date expiryDate = new Date(now.getTime() + expiration * 1000);

        return Jwts.builder()
                .setClaims(claims)
                .setSubject(subject)
                .setIssuedAt(now)
                .setExpiration(expiryDate)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }

    // 验证Token
    public Boolean validateToken(String token, UserDetails userDetails) {
        final String username = getUsernameFromToken(token);
        return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));
    }

    // 从Token中获取用户名
    public String getUsernameFromToken(String token) {
        return getClaimFromToken(token, Claims::getSubject);
    }

    // 检查Token是否过期
    private Boolean isTokenExpired(String token) {
        final Date expiration = getExpirationDateFromToken(token);
        return expiration.before(new Date());
    }
}

学习心得

  • JWT的无状态特性很适合分布式系统
  • 需要合理设置Token过期时间
  • 敏感操作建议使用短期Token

商品管理模块开发

商品管理是电商系统的核心,我按照黑马老师的思路,采用了分层架构:

GoodsController
GoodsService
GoodsMapper
MySQL数据库
分页查询商品
获取商品详情
创建商品
更新商品
删除商品
业务逻辑处理
数据校验
缓存管理
事务控制
基础CRUD
复杂查询
批量操作

Controller层实现

@RestController
@RequestMapping("/api/lwh/goods")
@Slf4j
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    // 分页查询商品
    @GetMapping("/page")
    public R<Page<GoodsVO>> getGoodsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long categoryId) {

        GoodsSearchDTO searchDTO = new GoodsSearchDTO();
        searchDTO.setPageNum(pageNum);
        searchDTO.setPageSize(pageSize);
        searchDTO.setName(name);
        searchDTO.setCategoryId(categoryId);

        Page<GoodsVO> result = goodsService.getGoodsPage(searchDTO);
        return R.success(result);
    }

    // 获取商品详情
    @GetMapping("/{id}")
    public R<GoodsDetailVO> getGoodsDetail(@PathVariable Long id) {
        GoodsDetailVO goods = goodsService.getGoodsDetail(id);
        return R.success(goods);
    }

    // 创建商品
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> createGoods(@Valid @RequestBody GoodsCreateDTO createDTO) {
        goodsService.createGoods(createDTO);
        return R.success();
    }
}

Service层实现

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Page<GoodsVO> getGoodsPage(GoodsSearchDTO searchDTO) {
        // 构建查询条件
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(searchDTO.getName()),
                    Goods::getName, searchDTO.getName())
               .eq(searchDTO.getCategoryId() != null,
                   Goods::getCategoryId, searchDTO.getCategoryId())
               .eq(Goods::getStatus, 1) // 只查询上架商品
               .orderByDesc(Goods::getCreateTime);

        // 分页查询
        Page<Goods> page = new Page<>(searchDTO.getPageNum(), searchDTO.getPageSize());
        Page<Goods> result = goodsMapper.selectPage(page, wrapper);

        // 转换为VO
        return result.convert(this::convertToVO);
    }

    @Override
    @Cacheable(value = "goods:detail", key = "#id", unless = "#result == null")
    public GoodsDetailVO getGoodsDetail(Long id) {
        Goods goods = goodsMapper.selectById(id);
        if (goods == null) {
            throw new BusinessException("商品不存在");
        }

        return convertToDetailVO(goods);
    }

    private GoodsVO convertToVO(Goods goods) {
        GoodsVO vo = new GoodsVO();
        BeanUtils.copyProperties(goods, vo);
        return vo;
    }
}

学习要点

  • 🎯 分层架构:Controller只负责参数接收,Service处理业务逻辑
  • 🎯 参数校验:使用@Valid注解进行参数校验
  • 🎯 权限控制:使用@PreAuthorize进行方法级权限控制
  • 🎯 缓存应用:使用@Cacheable提高查询性能

订单管理模块开发

订单模块是我觉得最复杂的部分,涉及状态流转和事务处理:

创建订单
支付成功
取消订单
超时自动取消
商家发货
确认收货
申请退款
退款成功
拒绝退款
待付款
待发货
已取消
待收货
已完成
退款中
已退款

订单创建流程

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Override
    public OrderCreateVO createOrder(OrderCreateDTO createDTO) {
        // 1. 验证商品库存
        List<OrderItemDTO> items = createDTO.getItems();
        for (OrderItemDTO item : items) {
            Goods goods = goodsMapper.selectById(item.getGoodsId());
            if (goods == null || goods.getStock() < item.getCount()) {
                throw new BusinessException("商品库存不足");
            }
        }

        // 2. 创建订单
        Order order = new Order();
        order.setOrderNo(generateOrderNo());
        order.setUserId(createDTO.getUserId());
        order.setStatus(OrderStatus.WAIT_PAY.getCode());
        order.setTotalAmount(calculateTotalAmount(items));
        order.setCreateTime(LocalDateTime.now());

        orderMapper.insert(order);

        // 3. 创建订单明细
        for (OrderItemDTO item : items) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(order.getId());
            orderItem.setGoodsId(item.getGoodsId());
            orderItem.setCount(item.getCount());
            orderItem.setPrice(item.getPrice());

            orderItemMapper.insert(orderItem);
        }

        // 4. 锁定库存
        for (OrderItemDTO item : items) {
            goodsMapper.lockStock(item.getGoodsId(), item.getCount());
        }

        // 5. 返回结果
        OrderCreateVO result = new OrderCreateVO();
        result.setOrderId(order.getId());
        result.setOrderNo(order.getOrderNo());

        return result;
    }

    private String generateOrderNo() {
        // 订单号生成规则:时间戳 + 随机数
        return System.currentTimeMillis() + RandomUtil.randomNumbers(6);
    }
}

学习心得

  • 🎯 事务管理:使用@Transactional确保数据一致性
  • 🎯 库存锁定:先锁定库存,支付成功后扣减
  • 🎯 订单号生成:保证唯一性和可读性
  • 🎯 状态机设计:清晰的状态流转逻辑

🎨 第三阶段:前端开发实战

Vue 3项目搭建

跟着黑马的课程,我学会了如何搭建现代化的Vue 3项目:

创建Vue项目
选择构建工具
Vite
Vue CLI
配置项目
安装依赖
配置路由
配置状态管理
配置UI组件库
Element Plus
Axios
Vue Router
Pinia
开发页面组件

项目初始化命令

# 使用Vite创建Vue 3项目
npm create vue@latest xiaotuxian-frontend

# 选择配置
✔ Add TypeScript? … No
✔ Add JSX Support? … No
✔ Add Vue Router for Single Page Application development? … Yes
✔ Add Pinia for state management? … Yes
✔ Add Vitest for Unit Testing? … No
✔ Add an End-to-End Testing Solution? … No
✔ Add ESLint for code quality? … Yes

# 安装依赖
cd xiaotuxian-frontend
npm install

# 安装Element Plus
npm install element-plus
npm install @element-plus/icons-vue

# 安装Axios
npm install axios

# 启动开发服务器
npm run dev

状态管理设计

使用Pinia进行状态管理,这是我觉得比Vuex简单很多的地方:

Pinia Store
User Store
Cart Store
Category Store
用户信息
登录状态
Token管理
购物车商品
商品数量
总价计算
分类数据
分类树结构

用户状态管理

// stores/userStore.js
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { userAPI } from '@/apis/user'

export const useUserStore = defineStore('user', () => {
  // 状态定义
  const userInfo = ref({})
  const token = ref('')

  // 计算属性
  const isLogin = computed(() => !!token.value)
  const userName = computed(() => userInfo.value.username || '游客')

  // 登录方法
  const login = async (loginForm) => {
    try {
      const res = await userAPI.login(loginForm)

      // 保存用户信息和Token
      userInfo.value = res.data.userInfo
      token.value = res.data.token

      // 设置请求头
      setAuthHeader(token.value)

      ElMessage.success('登录成功')
      return res
    } catch (error) {
      ElMessage.error(error.message || '登录失败')
      throw error
    }
  }

  // 退出登录
  const logout = () => {
    userInfo.value = {}
    token.value = ''
    removeAuthHeader()
    ElMessage.success('退出成功')
  }

  // 获取用户信息
  const getUserInfo = async () => {
    if (!token.value) return

    try {
      const res = await userAPI.getUserInfo()
      userInfo.value = res.data
    } catch (error) {
      console.error('获取用户信息失败:', error)
      logout()
    }
  }

  return {
    userInfo,
    token,
    isLogin,
    userName,
    login,
    logout,
    getUserInfo
  }
}, {
  // 持久化配置
  persist: {
    paths: ['token', 'userInfo']
  }
})

// 设置请求头
function setAuthHeader(token) {
  if (token) {
    http.defaults.headers.common['Authorization'] = `Bearer ${token}`
  }
}

// 移除请求头
function removeAuthHeader() {
  delete http.defaults.headers.common['Authorization']
}

购物车状态管理

// stores/cartStore.js
export const useCartStore = defineStore('cart', () => {
  const cartList = ref([])

  // 计算属性
  const cartCount = computed(() => {
    return cartList.value.reduce((sum, item) => sum + item.count, 0)
  })

  const cartTotal = computed(() => {
    return cartList.value.reduce((sum, item) => sum + item.price * item.count, 0)
  })

  // 添加到购物车
  const addToCart = (goods) => {
    const existItem = cartList.value.find(item => item.id === goods.id)

    if (existItem) {
      existItem.count += goods.count
    } else {
      cartList.value.push(goods)
    }

    ElMessage.success('添加到购物车成功')
  }

  // 删除购物车商品
  const removeFromCart = (goodsId) => {
    const index = cartList.value.findIndex(item => item.id === goodsId)
    if (index > -1) {
      cartList.value.splice(index, 1)
      ElMessage.success('删除成功')
    }
  }

  // 清空购物车
  const clearCart = () => {
    cartList.value = []
    ElMessage.success('购物车已清空')
  }

  return {
    cartList,
    cartCount,
    cartTotal,
    addToCart,
    removeFromCart,
    clearCart
  }
}, {
  persist: true
})

学习心得

  • Pinia的语法比Vuex简洁很多
  • Composition API风格的Store更容易理解
  • 持久化插件让数据持久化变得简单

组件化开发实践

在开发过程中,我学会了如何设计可复用的组件:

页面组件
业务组件
基础组件
商品卡片 GoodsCard
商品列表 GoodsList
购物车项 CartItem
订单项 OrderItem
按钮 XtxButton
输入框 XtxInput
图片查看器 ImageView
分页器 XtxPagination

商品卡片组件

<!-- components/GoodsCard/index.vue -->
<template>
  <div class="goods-card" @click="goToDetail">
    <div class="image-container">
      <el-image
        :src="goods.mainPicture"
        fit="cover"
        :preview-src-list="[goods.mainPicture]"
        class="goods-image"
      />
      <div class="price-tag">¥{{ goods.price }}</div>
    </div>

    <div class="goods-info">
      <h3 class="goods-title">{{ goods.name }}</h3>
      <p class="goods-desc">{{ goods.description }}</p>

      <div class="goods-footer">
        <span class="sales-count">已售{{ goods.salesCount }}件</span>
        <el-button
          type="primary"
          size="small"
          @click.stop="addToCart"
          :loading="loading"
        >
          加入购物车
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useCartStore } from '@/stores/cartStore'
import { ElMessage } from 'element-plus'

// 定义props
const props = defineProps({
  goods: {
    type: Object,
    required: true
  }
})

// 定义emits
const emit = defineEmits(['add-to-cart'])

const router = useRouter()
const cartStore = useCartStore()
const loading = ref(false)

// 跳转到商品详情
const goToDetail = () => {
  router.push(`/detail/${props.goods.id}`)
}

// 添加到购物车
const addToCart = async () => {
  loading.value = true

  try {
    await cartStore.addToCart({
      id: props.goods.id,
      name: props.goods.name,
      price: props.goods.price,
      mainPicture: props.goods.mainPicture,
      count: 1
    })

    emit('add-to-cart', props.goods)
  } catch (error) {
    ElMessage.error('添加失败,请重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.goods-card {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  background: white;
}

.goods-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.image-container {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
}

.goods-image {
  width: 100%;
  height: 100%;
  transition: transform 0.3s ease;
}

.goods-card:hover .goods-image {
  transform: scale(1.05);
}

.price-tag {
  position: absolute;
  top: 8px;
  right: 8px;
  background: linear-gradient(135deg, #ff6b6b, #ee5a24);
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

.goods-info {
  padding: 16px;
}

.goods-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 8px;

  /* 文本截断 */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.goods-desc {
  font-size: 14px;
  color: #909399;
  margin-bottom: 16px;

  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.goods-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sales-count {
  font-size: 12px;
  color: #c0c4cc;
}
</style>

学习要点

  • 🎯 组件设计:单一职责,高内聚低耦合
  • 🎯 Props验证:使用TypeScript或PropTypes进行类型检查
  • 🎯 事件通信:父子组件通过props和emits通信
  • 🎯 样式设计:使用CSS3实现现代化的交互效果

路由设计与页面结构

跟着黑马的课程,我学会了如何设计清晰的路由结构:

graph TD
    A[根路由 /] --> B[布局组件 Layout]
    B --> C[首页 Home]
    B --> D[分类页 Category]
    B --> E[商品详情 Detail]
    B --> F[购物车 Cart]

    A --> G[登录页 Login]
    A --> H[用户中心 Member]

    H --> H1[个人信息 UserInfo]
    H --> H2[我的订单 UserOrder]
    H --> H3[收货地址 UserAddress]

    A --> I[管理后台 Admin]
    I --> I1[商品管理 GoodsManage]
    I --> I2[订单管理 OrderManage]
    I --> I3[用户管理 UserManage]

路由配置

// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/userStore'

const routes = [
  {
    path: '/',
    component: () => import('@/views/Layout/index.vue'),
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/views/Home/index.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'category/:id',
        name: 'Category',
        component: () => import('@/views/Category/index.vue'),
        meta: { title: '商品分类' }
      },
      {
        path: 'detail/:id',
        name: 'Detail',
        component: () => import('@/views/Detail/index.vue'),
        meta: { title: '商品详情' }
      },
      {
        path: 'cart',
        name: 'Cart',
        component: () => import('@/views/CartList/index.vue'),
        meta: {
          title: '购物车',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/index.vue'),
    meta: { title: '用户登录' }
  },
  {
    path: '/member',
    component: () => import('@/views/Member/index.vue'),
    meta: {
      requiresAuth: true,
      title: '用户中心'
    },
    children: [
      {
        path: 'user',
        name: 'MemberUser',
        component: () => import('@/views/Member/UserInfo/index.vue'),
        meta: { title: '个人信息' }
      },
      {
        path: 'order',
        name: 'MemberOrder',
        component: () => import('@/views/Member/UserOrder/index.vue'),
        meta: { title: '我的订单' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior() {
    return { top: 0 }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 小兔鲜` : '小兔鲜'

  // 权限检查
  if (to.meta.requiresAuth && !userStore.isLogin) {
    ElMessage.warning('请先登录')
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
    return
  }

  next()
})

export default router

学习心得

  • 🎯 嵌套路由:合理使用嵌套路由组织页面结构
  • 🎯 路由守卫:实现权限控制和页面拦截
  • 🎯 动态路由:使用参数传递实现动态页面
  • 🎯 路由元信息:使用meta字段存储页面配置

🚀 第四阶段:项目优化与部署

性能优化实践

在学习过程中,我发现性能优化是一个很重要的环节:

性能优化
前端优化
后端优化
数据库优化
缓存优化
代码分割
图片懒加载
组件懒加载
打包优化
接口优化
异步处理
连接池优化
JVM调优
索引优化
查询优化
分页优化
读写分离
Redis缓存
本地缓存
CDN加速
浏览器缓存
前端性能优化

路由懒加载

// 优化前 - 全部导入
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
import Detail from '@/views/Detail/index.vue'

// 优化后 - 懒加载
const routes = [
  {
    path: '/',
    component: () => import('@/views/Home/index.vue')
  },
  {
    path: '/category/:id',
    component: () => import('@/views/Category/index.vue')
  },
  {
    path: '/detail/:id',
    component: () => import('@/views/Detail/index.vue')
  }
]

图片懒加载

<template>
  <!-- 使用Element Plus的图片懒加载 -->
  <el-image
    :src="imageSrc"
    lazy
    :loading="loadingImage"
    :error="errorImage"
    fit="cover"
  />
</template>

<script setup>
import loadingImage from '@/assets/images/loading.gif'
import errorImage from '@/assets/images/error.png'
</script>

Vite打包优化

// vite.config.js
export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        // 分包策略
        manualChunks: {
          'element-plus': ['element-plus'],
          'vue-vendor': ['vue', 'vue-router', 'pinia'],
          'utils': ['axios', 'dayjs']
        }
      }
    },
    // 压缩配置
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  }
})
后端性能优化

数据库连接池优化

# application.yml
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 10          # 初始连接数
      min-idle: 10              # 最小空闲连接
      max-active: 200           # 最大活跃连接
      max-wait: 60000           # 获取连接超时时间
      validation-query: SELECT 1
      test-while-idle: true
      time-between-eviction-runs-millis: 60000

Redis缓存策略

@Service
public class GoodsServiceImpl implements GoodsService {

    // 缓存热点数据
    @Cacheable(value = "goods:hot", key = "'list'", unless = "#result.isEmpty()")
    public List<GoodsVO> getHotGoods() {
        return goodsMapper.selectHotGoods();
    }

    // 缓存商品详情
    @Cacheable(value = "goods:detail", key = "#id", unless = "#result == null")
    public GoodsDetailVO getGoodsDetail(Long id) {
        return buildGoodsDetail(id);
    }

    // 更新时清除缓存
    @CacheEvict(value = {"goods:hot", "goods:detail"}, allEntries = true)
    public void updateGoods(GoodsUpdateDTO updateDTO) {
        // 更新逻辑
    }
}

部署方案设计

这是我觉得最实用的部分,黑马老师教的部署方案很完整:

部署方案
开发环境
测试环境
生产环境
本地开发
热重载
调试工具
Docker部署
自动化测试
性能测试
负载均衡
数据库集群
Redis集群
监控告警
一键部署脚本

我按照黑马的思路,写了一个自动化部署脚本:

# 启动脚本.ps1
Write-Host "🚀 开始启动小兔鲜商城项目..." -ForegroundColor Green

# 检查环境
function Check-Environment {
    Write-Host "🔍 检查运行环境..." -ForegroundColor Cyan

    # 检查JDK
    try {
        $javaVersion = java -version 2>&1 | Select-String "21\."
        if ($javaVersion) {
            Write-Host "✅ JDK 21 检查通过" -ForegroundColor Green
        } else {
            Write-Host "❌ 需要JDK 21,请先安装" -ForegroundColor Red
            exit 1
        }
    } catch {
        Write-Host "❌ 未找到Java,请先安装JDK 21" -ForegroundColor Red
        exit 1
    }

    # 检查Node.js
    try {
        $nodeVersion = node --version
        if ($nodeVersion -match "v2[0-9]\.") {
            Write-Host "✅ Node.js 检查通过: $nodeVersion" -ForegroundColor Green
        } else {
            Write-Host "❌ 需要Node.js 20+,当前版本: $nodeVersion" -ForegroundColor Red
            exit 1
        }
    } catch {
        Write-Host "❌ 未找到Node.js,请先安装" -ForegroundColor Red
        exit 1
    }

    # 检查MySQL
    try {
        $mysqlStatus = Get-Service -Name "MySQL*" -ErrorAction SilentlyContinue
        if ($mysqlStatus -and $mysqlStatus.Status -eq "Running") {
            Write-Host "✅ MySQL 服务运行中" -ForegroundColor Green
        } else {
            Write-Host "⚠️  MySQL 服务未运行,尝试启动..." -ForegroundColor Yellow
            Start-Service -Name "MySQL*"
        }
    } catch {
        Write-Host "❌ MySQL 服务检查失败" -ForegroundColor Red
    }
}

# 启动后端
function Start-Backend {
    Write-Host "🔧 启动后端服务..." -ForegroundColor Cyan

    # 编译项目
    Write-Host "📦 编译后端项目..." -ForegroundColor Yellow
    mvn clean compile -q

    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ 后端编译成功" -ForegroundColor Green

        # 启动Spring Boot应用
        Write-Host "🚀 启动Spring Boot应用..." -ForegroundColor Yellow
        Start-Process -FilePath "mvn" -ArgumentList "spring-boot:run" -WindowStyle Minimized

        # 等待启动
        Write-Host "⏳ 等待后端启动..." -ForegroundColor Yellow
        Start-Sleep -Seconds 30

        # 健康检查
        try {
            $response = Invoke-RestMethod -Uri "http://localhost:8080/api/test/health" -TimeoutSec 10
            Write-Host "✅ 后端启动成功: $($response.message)" -ForegroundColor Green
        } catch {
            Write-Host "❌ 后端启动失败,请检查日志" -ForegroundColor Red
        }
    } else {
        Write-Host "❌ 后端编译失败" -ForegroundColor Red
        exit 1
    }
}

# 启动前端
function Start-Frontend {
    Write-Host "🎨 启动前端服务..." -ForegroundColor Cyan

    Set-Location "黑马程序员小兔鲜"

    # 安装依赖
    if (!(Test-Path "node_modules")) {
        Write-Host "📦 安装前端依赖..." -ForegroundColor Yellow
        npm install
    }

    # 启动开发服务器
    Write-Host "🚀 启动前端开发服务器..." -ForegroundColor Yellow
    Start-Process -FilePath "npm" -ArgumentList "run", "dev" -WindowStyle Minimized

    # 等待启动
    Start-Sleep -Seconds 15

    # 检查前端服务
    try {
        $response = Invoke-WebRequest -Uri "http://localhost:3000" -TimeoutSec 10
        if ($response.StatusCode -eq 200) {
            Write-Host "✅ 前端启动成功" -ForegroundColor Green
        }
    } catch {
        Write-Host "❌ 前端启动失败" -ForegroundColor Red
    }

    Set-Location ".."
}

# 主流程
Check-Environment
Start-Backend
Start-Frontend

Write-Host ""
Write-Host "🎉 项目启动完成!" -ForegroundColor Green
Write-Host "📱 前端地址: http://localhost:3000" -ForegroundColor Cyan
Write-Host "🔧 后端地址: http://localhost:8080" -ForegroundColor Cyan
Write-Host "📚 API文档: http://localhost:8080/doc.html" -ForegroundColor Cyan
Write-Host ""
Write-Host "按任意键打开浏览器..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

# 打开浏览器
Start-Process "http://localhost:3000"

学习心得

  • 🎯 自动化部署:减少人工操作,提高部署效率
  • 🎯 环境检查:确保运行环境正确配置
  • 🎯 健康检查:验证服务启动状态
  • 🎯 错误处理:完善的错误提示和处理机制

🐛 踩坑经验分享

常见问题及解决方案

在学习过程中,我遇到了很多问题,这里分享一些典型的踩坑经验:

常见问题
环境问题
代码问题
部署问题
性能问题
JDK版本不兼容
MySQL连接失败
Redis连接超时
端口被占用
跨域问题
JWT Token失效
数据校验失败
事务回滚问题
打包失败
静态资源404
数据库连接池
内存溢出
查询慢
缓存穿透
前端卡顿
接口超时
问题1:跨域问题

现象:前端调用后端接口时报CORS错误

Access to XMLHttpRequest at 'http://localhost:8080/api/user/login'
from origin 'http://localhost:3000' has been blocked by CORS policy

原因:Spring Security默认禁用跨域请求

解决方案

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            // 配置CORS
            .cors(cors -> cors.configurationSource(corsConfigurationSource()))
            // 其他配置...
            ;
        return http.build();
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOriginPatterns(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
        configuration.setAllowedHeaders(Arrays.asList("*"));
        configuration.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}
问题2:JWT Token在请求头中丢失

现象:登录成功后,后续请求仍然返回401未授权

原因:Axios请求拦截器没有正确设置Authorization头

解决方案

// utils/http.js
import axios from 'axios'
import { useUserStore } from '@/stores/userStore'
import { ElMessage } from 'element-plus'

const http = axios.create({
  baseURL: 'http://localhost:8080',
  timeout: 10000
})

// 请求拦截器
http.interceptors.request.use(
  config => {
    // 从store中获取token
    const userStore = useUserStore()
    if (userStore.token) {
      config.headers.Authorization = `Bearer ${userStore.token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
http.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    if (error.response?.status === 401) {
      ElMessage.error('登录已过期,请重新登录')
      const userStore = useUserStore()
      userStore.logout()
      // 跳转到登录页
      router.push('/login')
    }
    return Promise.reject(error)
  }
)

export default http
问题3:MyBatis Plus分页插件不生效

现象:使用Page对象查询,但返回的是全部数据

原因:没有配置分页插件

解决方案

@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 分页插件
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
        paginationInterceptor.setDbType(DbType.MYSQL);
        paginationInterceptor.setOverflow(false); // 超过最大页数后不查询
        paginationInterceptor.setMaxLimit(500L); // 单页最大数量

        interceptor.addInnerInterceptor(paginationInterceptor);

        return interceptor;
    }
}
问题4:Vue组件状态不更新

现象:修改了响应式数据,但页面没有更新

原因:直接修改了响应式对象的属性,破坏了响应式

错误写法

// ❌ 错误:直接替换响应式对象
const userInfo = ref({ name: '张三', age: 20 })
userInfo.value = { name: '李四', age: 25 } // 这样会丢失响应式

// ❌ 错误:修改数组索引
const list = ref([1, 2, 3])
list.value[0] = 999 // 在某些情况下可能不会触发更新

正确写法

// ✅ 正确:使用Object.assign或展开运算符
const userInfo = ref({ name: '张三', age: 20 })
Object.assign(userInfo.value, { name: '李四', age: 25 })
// 或者
userInfo.value = { ...userInfo.value, name: '李四', age: 25 }

// ✅ 正确:使用数组方法
const list = ref([1, 2, 3])
list.value.splice(0, 1, 999) // 使用splice方法

性能优化踩坑

问题5:N+1查询问题

现象:查询商品列表时,每个商品都会单独查询分类信息,导致大量SQL查询

原因:没有使用关联查询或缓存

解决方案

// 方案1:使用MyBatis Plus的关联查询
@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {

    @Select("SELECT g.*, c.name as category_name " +
            "FROM goods g LEFT JOIN category c ON g.category_id = c.id " +
            "WHERE g.status = 1 " +
            "ORDER BY g.create_time DESC " +
            "LIMIT #{offset}, #{size}")
    List<GoodsVO> selectGoodsWithCategory(@Param("offset") long offset, @Param("size") long size);
}

// 方案2:使用缓存预加载分类数据
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private CategoryService categoryService;

    public Page<GoodsVO> getGoodsPage(GoodsSearchDTO searchDTO) {
        // 预加载所有分类数据到缓存
        Map<Long, String> categoryMap = categoryService.getAllCategoryMap();

        // 查询商品数据
        Page<Goods> goodsPage = goodsMapper.selectPage(page, wrapper);

        // 转换为VO,从缓存中获取分类名称
        return goodsPage.convert(goods -> {
            GoodsVO vo = new GoodsVO();
            BeanUtils.copyProperties(goods, vo);
            vo.setCategoryName(categoryMap.get(goods.getCategoryId()));
            return vo;
        });
    }
}
问题6:前端首屏加载慢

现象:首次访问页面需要等待很长时间

原因:打包后的JS文件过大,没有做代码分割

解决方案

// vite.config.js
export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          // 将第三方库单独打包
          'vue-vendor': ['vue', 'vue-router', 'pinia'],
          'element-plus': ['element-plus', '@element-plus/icons-vue'],
          'utils': ['axios', 'dayjs', 'lodash-es']
        }
      }
    }
  },
  // 开启gzip压缩
  plugins: [
    vue(),
    viteCompression({
      algorithm: 'gzip'
    })
  ]
})

调试技巧分享

后端调试

使用IDEA的Debug功能

@RestController
public class GoodsController {

    @GetMapping("/goods/{id}")
    public R<GoodsVO> getGoods(@PathVariable Long id) {
        // 在这里打断点,可以查看参数值
        log.info("查询商品详情,ID: {}", id);

        GoodsVO goods = goodsService.getGoodsDetail(id);

        // 在这里打断点,可以查看返回值
        log.info("查询结果: {}", goods);

        return R.success(goods);
    }
}

使用日志排查问题

# application-dev.yml
logging:
  level:
    cn.edu.xcu: DEBUG  # 设置项目包的日志级别
    org.springframework.security: DEBUG  # 调试Security问题
    org.springframework.web: DEBUG  # 调试Web请求问题
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
前端调试

使用Vue DevTools

// 在组件中添加调试信息
export default {
  setup() {
    const userStore = useUserStore()

    // 在开发环境下暴露到window对象,方便调试
    if (process.env.NODE_ENV === 'development') {
      window.userStore = userStore
    }

    return {
      userStore
    }
  }
}

使用console.log进行调试

// 封装调试函数
const debug = {
  log: (...args) => {
    if (process.env.NODE_ENV === 'development') {
      console.log('[DEBUG]', ...args)
    }
  },
  error: (...args) => {
    if (process.env.NODE_ENV === 'development') {
      console.error('[ERROR]', ...args)
    }
  }
}

// 使用
debug.log('用户登录', { username, password })

📊 学习成果总结

技能提升对比

通过3个月的学习,我的技能水平有了显著提升:

graph LR
    A[学习前] --> B[学习后]

    A --> A1[Java基础 ⭐⭐⭐]
    A --> A2[Spring Boot ⭐⭐]
    A --> A3[Vue.js ⭐⭐]
    A --> A4[数据库 ⭐⭐]
    A --> A5[项目经验 ⭐]

    B --> B1[Java进阶 ⭐⭐⭐⭐⭐]
    B --> B2[Spring Boot ⭐⭐⭐⭐⭐]
    B --> B3[Vue 3 ⭐⭐⭐⭐⭐]
    B --> B4[数据库优化 ⭐⭐⭐⭐]
    B --> B5[项目实战 ⭐⭐⭐⭐]

项目完成度统计

13% 13% 13% 13% 12% 12% 11% 12% 项目功能完成度 用户管理 商品管理 订单管理 购物车 支付系统 后台管理 性能优化 部署运维

代码量统计

模块 代码行数 文件数量 主要技术
后端代码 8,000+ 120+ Spring Boot, MyBatis Plus
前端代码 6,000+ 80+ Vue 3, Element Plus
配置文件 500+ 15+ YAML, XML, JSON
数据库脚本 300+ 5+ MySQL DDL/DML
部署脚本 200+ 8+ PowerShell, Batch
总计 15,000+ 228+ 全栈开发

学习时间分配

40% 30% 15% 10% 5% 学习时间分配 后端开发 前端开发 数据库设计 部署运维 文档整理

🎯 给学习者的建议

学习路径建议

基于我的学习经验,给大家推荐一个学习路径:

准备阶段
基础学习
项目实战
深入优化
总结提升
环境搭建
工具准备
基础复习
Spring Boot学习
Vue 3学习
数据库设计
跟着视频敲代码
理解业务逻辑
调试解决问题
性能优化
代码重构
功能扩展
技术总结
经验分享
简历项目

学习方法建议

1. 理论与实践结合

不要只看不练

  • ❌ 只看视频不动手
  • ✅ 边看边敲代码,理解每一行的作用

多思考为什么

  • ❌ 只知道怎么用,不知道为什么这样用
  • ✅ 理解技术选型的原因和适用场景
2. 循序渐进学习

第一遍:跟着敲代码

  • 目标:能够运行起来
  • 重点:熟悉开发流程
  • 时间:1个月

第二遍:理解业务逻辑

  • 目标:明白每个功能的实现原理
  • 重点:掌握核心技术点
  • 时间:1个月

第三遍:优化和扩展

  • 目标:能够独立开发新功能
  • 重点:性能优化和代码质量
  • 时间:1个月
3. 建立知识体系

技术栈学习顺序

Java基础
Spring Boot
MyBatis Plus
Spring Security
Redis缓存
HTML/CSS/JS
Vue 3基础
Vue Router
Pinia状态管理
Element Plus
前后端联调
项目部署

常用学习资源

官方文档
学习视频
  • 黑马程序员Spring Boot教程
  • 黑马程序员Vue 3教程
  • 尚硅谷微服务教程
实用工具
  • 开发工具:IntelliJ IDEA, VS Code
  • 数据库工具:Navicat, DBeaver
  • 接口测试:Postman, Apifox
  • 版本控制:Git, GitHub

面试准备建议

项目介绍模板

项目背景
“这是我跟着黑马程序员学习的一个电商项目,采用前后端分离架构,我负责了整个项目的开发,包括…”

技术栈
“后端使用Spring Boot 3.4 + MyBatis Plus + MySQL + Redis,前端使用Vue 3 + Element Plus + Pinia…”

核心功能
“实现了用户管理、商品管理、订单管理、购物车等核心电商功能…”

技术亮点
“项目中使用了JWT认证、Redis缓存、分页查询优化、前端组件化设计等技术…”

遇到的问题
“在开发过程中遇到了跨域问题、N+1查询问题等,通过…方式解决了…”

常见面试问题

Spring Boot相关

  • Spring Boot的自动配置原理是什么?
  • 如何自定义Starter?
  • Spring Boot的启动流程是怎样的?

Vue 3相关

  • Vue 3相比Vue 2有哪些改进?
  • Composition API的优势是什么?
  • Vue 3的响应式原理是什么?

项目相关

  • 如何保证接口的安全性?
  • 如何处理高并发场景?
  • 如何进行性能优化?

🚀 未来学习计划

短期目标(3-6个月)

当前水平
短期目标
单体应用开发
基础功能实现
简单部署
微服务架构
分布式事务
容器化部署
监控告警

具体计划

  • 学习Spring Cloud微服务框架
  • 掌握Docker容器化技术
  • 学习Kubernetes集群管理
  • 了解分布式事务解决方案

长期目标(6-12个月)

技术深度

  • 深入学习JVM调优
  • 掌握分布式系统设计
  • 学习大数据处理技术
  • 了解云原生技术栈

项目经验

  • 参与开源项目贡献
  • 独立设计和开发项目
  • 技术博客和分享
  • 面试和求职准备

💭 学习心得体会

最大的收获

技术能力提升
从一个只会写简单CRUD的初学者,到现在能够独立开发完整的电商系统,这个过程让我对全栈开发有了深入的理解。

解决问题的能力
在学习过程中遇到了很多问题,通过查阅文档、搜索资料、请教他人等方式解决问题,这个过程锻炼了我的问题解决能力。

项目思维
不再是单纯的学习技术,而是从项目的角度思考问题,考虑用户体验、性能优化、可维护性等方面。

学习感悟

坚持的重要性
学习编程是一个长期的过程,需要持续的投入和坚持。遇到困难时不要放弃,多尝试不同的解决方案。

实践的重要性
光看视频和文档是不够的,一定要动手实践。在实践中遇到的问题和解决过程,才是真正的学习。

总结的重要性
定期总结学习内容,整理知识点,写技术博客,这样可以加深理解,也能帮助其他学习者。

给初学者的话

如果你也在学习这个项目,或者准备开始学习,我想对你说:

不要害怕困难
编程学习确实有一定的难度,但只要坚持下去,一定能够掌握。每个程序员都是从初学者开始的。

多动手实践
不要只是看视频,一定要跟着敲代码。遇到错误不要慌,这是学习的一部分。

善于求助
遇到问题时,可以通过搜索引擎、技术论坛、QQ群等方式寻求帮助。程序员社区是很友善的。

保持学习
技术更新很快,要保持持续学习的心态。关注新技术,但也要把基础打牢。

📝 项目复现指南

为了帮助大家能够顺利复现这个项目,我整理了一份详细的操作指南:

环境准备清单

环境准备
软件安装
配置设置
项目下载
JDK 21
Node.js 22+
MySQL 8.0
Redis 6.0+
Git
环境变量
数据库配置
IDE设置
克隆代码
导入项目
依赖安装

第一步:软件安装

1.1 安装JDK 21
# Windows用户
# 1. 下载Oracle JDK 21或OpenJDK 21
# 2. 安装到 C:\Program Files\Java\jdk-21
# 3. 配置环境变量
JAVA_HOME=C:\Program Files\Java\jdk-21
PATH=%JAVA_HOME%\bin;%PATH%

# 验证安装
java -version
javac -version
1.2 安装Node.js
# 下载Node.js 22.x版本
# 安装到默认路径
# 验证安装
node --version
npm --version

# 配置npm镜像(可选)
npm config set registry https://registry.npmmirror.com
1.3 安装MySQL 8.0
-- 安装MySQL 8.0
-- 设置root密码为:root
-- 创建数据库
CREATE DATABASE xiaotuxian_mall CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 创建用户(可选)
CREATE USER 'xiaotuxian'@'localhost' IDENTIFIED BY 'xiaotuxian123';
GRANT ALL PRIVILEGES ON xiaotuxian_mall.* TO 'xiaotuxian'@'localhost';
FLUSH PRIVILEGES;
1.4 安装Redis(可选)
# Windows用户可以下载Redis for Windows
# 或者使用Docker运行Redis
docker run -d --name redis -p 6379:6379 redis:latest

# 验证Redis连接
redis-cli ping

第二步:项目下载和配置

2.1 克隆项目代码
# 如果有Git仓库地址
git clone [项目地址]
cd xiaotuxian-mall

# 或者直接下载压缩包解压
2.2 后端项目配置
# src/main/resources/application.yml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xiaotuxian_mall?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

  redis:
    host: localhost
    port: 6379
    password: # 如果Redis设置了密码
    database: 0

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

# JWT配置
jwt:
  secret: xiaotuxianmallsecretkeymustbelongerthan512bits
  expiration: 86400 # 24小时
2.3 前端项目配置
// 黑马程序员小兔鲜/src/utils/http.js
const http = axios.create({
  baseURL: 'http://localhost:8080', // 后端服务地址
  timeout: 10000
})

第三步:启动项目

3.1 启动后端
# 方式1:使用Maven命令
mvn clean compile
mvn spring-boot:run

# 方式2:使用IDE
# 在IDEA中打开项目,运行XiaotuxianMallApplication.java

# 方式3:使用提供的脚本
# Windows用户
.\启动后端-JDK21.bat

# 验证后端启动
curl http://localhost:8080/api/test/health
3.2 启动前端
# 进入前端目录
cd 黑马程序员小兔鲜

# 安装依赖
npm install

# 启动开发服务器
npm run dev

# 或者使用提供的脚本
.\启动前端.bat
3.3 验证项目运行
# 检查后端服务
curl http://localhost:8080/api/test/health

# 检查前端服务
# 浏览器访问 http://localhost:3000

# 检查数据库连接
# 查看后端控制台日志,确认数据库连接成功

第四步:功能测试

4.1 基础功能测试
功能测试
用户功能
商品功能
订单功能
用户注册
用户登录
个人信息
商品列表
商品详情
商品搜索
添加购物车
创建订单
订单查询
4.2 测试用例
// 可以使用提供的测试页面
// 打开 测试接口.html 进行接口测试

// 或者使用Postman/Apifox测试
// 导入API文档:https://www.apifox.cn/apidoc/shared-c05cb8d7-e591-4d9c-aff8-11065a0ec1de/api-67132167

第五步:常见问题解决

5.1 端口冲突
# 检查端口占用
netstat -ano | findstr :8080
netstat -ano | findstr :3000

# 杀死占用端口的进程
taskkill /PID [进程ID] /F

# 或者修改配置文件中的端口
5.2 数据库连接失败
# 检查MySQL服务是否启动
net start mysql

# 检查数据库配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xiaotuxian_mall?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
    username: root
    password: root # 确认密码正确
5.3 前端启动失败
# 清除npm缓存
npm cache clean --force

# 删除node_modules重新安装
rm -rf node_modules
npm install

# 检查Node.js版本
node --version # 确保是22.x版本

第六步:开发环境优化

6.1 IDEA配置优化
<!-- 在IDEA中配置Maven -->
<!-- Settings -> Build -> Build Tools -> Maven -->
<!-- Maven home directory: [Maven安装路径] -->
<!-- User settings file: [Maven配置文件路径] -->
<!-- Local repository: [本地仓库路径] -->
6.2 VS Code插件推荐
{
  "recommendations": [
    "Vue.volar",
    "Vue.vscode-typescript-vue-plugin",
    "bradlc.vscode-tailwindcss",
    "esbenp.prettier-vscode",
    "dbaeumer.vscode-eslint"
  ]
}

学习建议

循序渐进的学习方法
  1. 第一遍:跟着教程敲代码,重点是让项目跑起来
  2. 第二遍:理解每个功能的实现原理和业务逻辑
  3. 第三遍:尝试修改和扩展功能,加深理解
重点关注的知识点
  • Spring Boot自动配置原理
  • MyBatis Plus条件构造器使用
  • Vue 3 Composition API
  • JWT认证机制
  • Redis缓存策略
    -前后端接口设计
扩展学习方向
  • 添加新的业务功能(如优惠券、积分系统)
  • 性能优化(如数据库索引、缓存策略)
  • 安全加固(如接口限流、数据加密)
  • 部署优化(如Docker容器化、CI/CD)

通过这个项目的学习,你将掌握现代化Web开发的核心技能,为后续的职业发展打下坚实的基础!

🎉 结语

写在最后

通过这3个月的学习历程,我从一个对电商系统一无所知的初学者,成长为能够独立开发完整项目的开发者。这个过程虽然充满挑战,但收获满满。

感谢黑马程序员
感谢黑马程序员提供了如此优质的课程内容,让我能够系统地学习现代化Web开发技术。老师们的讲解深入浅出,项目设计也很贴近实际开发场景。

感谢开源社区
感谢Spring、Vue、MyBatis Plus等优秀的开源框架,让我们能够站在巨人的肩膀上快速开发应用。

感谢每一位读者
如果你读到了这里,说明你对技术学习有着和我一样的热情。希望我的学习经历能够对你有所帮助。

项目价值总结

这个项目不仅仅是一个学习案例,更是一个完整的技术实践:

mindmap
  root((项目价值))
    技术价值
      现代化技术栈
      完整开发流程
      最佳实践应用
      性能优化经验
    学习价值
      系统性学习
      问题解决能力
      项目思维培养
      团队协作模拟
    职业价值
      简历项目经验
      面试技术储备
      实际开发能力
      持续学习基础

技术成长轨迹

初学者
入门开发者
熟练开发者
高级开发者
基础语法
简单CRUD
框架使用
项目开发
架构设计
性能优化
技术选型
团队协作

持续学习计划

学习永无止境,这个项目只是我技术成长路上的一个里程碑:

近期计划

  • 深入学习Spring Cloud微服务
  • 掌握Docker和Kubernetes
  • 学习分布式系统设计
  • 参与开源项目贡献

长期目标

  • 成为全栈架构师
  • 具备大型项目设计能力
  • 拥有技术团队管理经验
  • 在技术社区有一定影响力

给同路人的话

如果你也在学习这个项目,或者正在考虑开始学习,我想对你说:

坚持下去
编程学习确实有一定难度,但只要坚持下去,一定能够掌握。每一个优秀的程序员都是从初学者开始的。

享受过程
不要只关注结果,要享受学习和解决问题的过程。每一次调试成功、每一个功能实现,都是成长的见证。

保持好奇
技术在不断发展,要保持对新技术的好奇心和学习热情。但也要把基础打牢,基础扎实了,学习新技术会更容易。

乐于分享
在学习过程中,不要忘记分享和帮助他人。教学相长,在帮助别人的过程中,自己也会有新的收获。

版权声明

本文基于黑马程序员的小兔鲜商城课程进行学习和总结,代码主要来源于课程内容,在此基础上进行了一些扩展和优化。

📊 性能优化实践

1. 数据库优化

索引设计
-- 商品表索引优化
CREATE INDEX idx_goods_category_price ON goods(category_id, price);
CREATE INDEX idx_goods_name_fulltext ON goods(name) USING FULLTEXT;
CREATE INDEX idx_goods_create_time ON goods(create_time DESC);

-- 订单表索引优化  
CREATE INDEX idx_order_user_status ON orders(user_id, status);
CREATE INDEX idx_order_create_time ON orders(create_time DESC);
查询优化
原始查询
添加索引
查询重写
分页优化
缓存策略
500ms
200ms
100ms
50ms
10ms

2. 前端性能优化

路由懒加载
const routes = [
  {
    path: '/home',
    component: () => import('@/views/Home/index.vue')
  },
  {
    path: '/category',
    component: () => import('@/views/Category/index.vue')
  }
]
图片懒加载
<template>
  <el-image 
    :src="imageSrc" 
    lazy
    :loading="loadingImage"
    :error="errorImage"
  />
</template>

🚀 部署与运维

1. 自动化部署

部署流程图
代码提交
环境检查
依赖安装
项目编译
数据库迁移
启动服务
健康检查
部署完成
检查JDK 21
检查Node.js
检查MySQL
一键部署脚本

项目提供了完整的自动化部署方案:

# 启动脚本.ps1
Write-Host "🚀 开始启动小兔鲜商城项目..." -ForegroundColor Green

# 1. 环境检查
.\环境检查.ps1

# 2. 启动后端
Start-Process -FilePath "cmd" -ArgumentList "/c", "启动后端-JDK21.bat"

# 3. 启动前端  
Start-Process -FilePath "cmd" -ArgumentList "/c", "启动前端.bat"

# 4. 健康检查
Start-Sleep -Seconds 30
Invoke-RestMethod -Uri "http://localhost:8080/api/test/health"

2. 监控告警

系统监控指标
监控系统
应用监控
数据库监控
缓存监控
服务器监控
响应时间
错误率
吞吐量
连接数
慢查询
锁等待
命中率
内存使用
连接数
CPU使用率
内存使用率
磁盘IO

📈 项目成果展示

性能测试结果

指标 优化前 优化后 提升幅度
首页加载时间 2.5s 0.8s 68% ⬆️
商品查询响应 500ms 50ms 90% ⬆️
并发处理能力 100 QPS 1000 QPS 900% ⬆️
内存使用 512MB 256MB 50% ⬇️

🙏 致谢

感谢所有为这个项目提供帮助和支持的朋友们:

  • 黑马程序员:提供了优秀的学习资源和项目思路
  • 开源社区:Spring、Vue等优秀的开源框架
  • 技术博主:分享的宝贵经验和最佳实践
  • 测试用户:提供的反馈和建议

特别感谢我的开发伙伴们,正是大家的共同努力,才有了这个项目的成功!


最后,如果这篇文章对你有帮助,请点赞👍、收藏⭐、分享🔄支持一下!你的支持是我继续创作的动力!

版权声明:本文为笙囧同学原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。


**如果这篇文章对你有帮助,请点赞👍、收藏⭐、分享🔄支持一下!**

Logo

电商企业物流数字化转型必备!快递鸟 API 接口,72 小时快速完成物流系统集成。全流程实战1V1指导,营造开放的API技术生态圈。

更多推荐