1. 项目背景与意义
随着人们生活水平的不断提高,宠物逐渐成为许多家庭的重要成员,宠物消费市场持续快速增长。传统的宠物商店多采用线下经营模式,存在商品信息更新不及时、库存管理混乱、客户购买体验不佳等问题。与此同时,互联网技术的普及使得线上购物成为主流消费方式,宠物主人们越来越倾向于通过网络平台浏览商品、下单购买、预约服务。
基于SpringBoot的宠物商店系统正是在这一背景下提出的。该系统旨在为宠物商店提供一个集商品展示、在线购买、订单管理、用户管理于一体的信息化管理平台,帮助商家降低运营成本、提升管理效率,同时为消费者提供便捷、直观的购物体验。
从实际意义来看,本系统的设计与实现具有以下价值:
- 提升管理效率:商品、库存、订单等核心数据实现数字化管理,减少人工记录和统计的工作量。
- 优化用户体验:用户可随时随地浏览商品、下单购买,系统支持订单状态跟踪,提升购物满意度。
- 促进业务增长:通过线上渠道扩大销售覆盖面,结合数据分析辅助经营决策,助力宠物商店数字化转型。
2. 系统技术栈
本系统采用前后端分离的开发模式,后端基于SpringBoot框架构建,前端使用Vue.js进行页面开发,数据库选用MySQL,整体技术栈成熟稳定、生态完善,适合中小型业务系统的快速开发与部署。
| 层次 | 技术选型 | 说明 |
|---|---|---|
| 后端框架 | SpringBoot | 简化Spring应用搭建与配置,内置Tomcat,支持快速开发RESTful接口 |
| 持久层框架 | MyBatis-Plus | 简化数据库操作,提供通用CRUD、分页查询等能力 |
| 数据库 | MySQL | 关系型数据库,存储用户、商品、订单等业务数据 |
| 前端框架 | Vue.js + Element UI | 构建管理后台和用户端页面,组件化开发,交互友好 |
| 权限认证 | Spring Security + JWT | 实现用户登录认证与接口访问控制 |
| 构建工具 | Maven | 管理项目依赖与构建流程 |
在系统架构上,后端按分层思想组织代码,分为Controller层、Service层、Mapper层和实体层,各层职责清晰、便于维护和扩展。前端通过Axios调用后端接口,实现前后端数据交互。
3. 系统功能模块设计
基于宠物商店的业务需求,系统主要划分为以下几个功能模块:
- 用户模块:用户注册、登录、个人信息管理,支持普通用户和管理员两种角色。
- 商品模块:宠物及宠物用品的分类展示、商品详情查看、库存管理。
- 购物车模块:用户可将心仪商品加入购物车,修改数量或删除商品。
- 订单模块:用户提交订单、在线支付(模拟)、查看订单状态,管理员进行订单发货处理。
- 公告模块:管理员发布店铺公告和促销信息,用户端进行展示。
系统整体采用角色权限控制,普通用户只能操作自己的购物车和订单,管理员则拥有商品管理、订单管理、用户管理等后台功能,保障了系统的安全性和数据隔离性。
4. 核心代码实现
下面选取系统开发中几个具有代表性的核心代码片段进行说明,包括实体类、Controller接口、Service业务逻辑以及JWT工具类。
4.1 商品实体类
商品实体类对应数据库中的商品表,使用MyBatis-Plus的注解简化字段映射。
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import java.math.BigDecimal; import java.time.LocalDateTime; @Data @TableName("product") public class Product { @TableId(type = IdType.AUTO) private Integer id; private String name; private String category; private BigDecimal price; private Integer stock; private String image; private String description; private LocalDateTime createTime; }4.2 商品查询Controller
Controller层负责接收前端请求并返回统一格式的响应结果,以下代码实现了商品分页查询接口。
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/product") public class ProductController { @Autowired private ProductService productService; @GetMapping("/page") public Result page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) String keyword) { Page<Product> page = new Page<>(pageNum, pageSize); QueryWrapper<Product> wrapper = new QueryWrapper<>(); if (keyword != null && !keyword.isEmpty()) { wrapper.like("name", keyword); } wrapper.orderByDesc("create_time"); return Result.success(productService.page(page, wrapper)); } }4.3 下单业务逻辑
下单是系统的核心业务,涉及库存校验、订单创建和购物车清理等多个步骤,以下代码展示了Service层的实现思路。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; @Service public class OrderServiceImpl implements OrderService { @Autowired private OrderMapper orderMapper; @Autowired private OrderItemMapper orderItemMapper; @Autowired private ProductMapper productMapper; @Autowired private CartMapper cartMapper; @Override @Transactional(rollbackFor = Exception.class) public Integer createOrder(Integer userId, List<CartItem> cartItems) { BigDecimal totalPrice = BigDecimal.ZERO; for (CartItem item : cartItems) { Product product = productMapper.selectById(item.getProductId()); if (product == null || product.getStock() < item.getQuantity()) { throw new RuntimeException("商品库存不足:" + item.getProductId()); } totalPrice = totalPrice.add(product.getPrice().multiply(BigDecimal.valueOf(item.getQuantity()))); } Order order = new Order(); order.setUserId(userId); order.setTotalPrice(totalPrice); order.setStatus(0); order.setCreateTime(LocalDateTime.now()); orderMapper.insert(order); for (CartItem item : cartItems) { OrderItem orderItem = new OrderItem(); orderItem.setOrderId(order.getId()); orderItem.setProductId(item.getProductId()); orderItem.setQuantity(item.getQuantity()); orderItemMapper.insert(orderItem); Product product = productMapper.selectById(item.getProductId()); product.setStock(product.getStock() - item.getQuantity()); productMapper.updateById(product); cartMapper.deleteById(item.getId()); } return order.getId(); } }4.4 JWT工具类
系统使用JWT生成和校验登录令牌,以下工具类封装了Token的创建与解析逻辑。
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import java.util.Date; public class JwtUtil { private static final String SECRET_KEY = "pet-shop-secret-key"; private static final long EXPIRE_TIME = 7 * 24 * 60 * 60 * 1000L; public static String generateToken(Integer userId, String username) { Date now = new Date(); Date expireDate = new Date(now.getTime() + EXPIRE_TIME); return Jwts.builder() .setSubject(username) .claim("userId", userId) .setIssuedAt(now) .setExpiration(expireDate) .signWith(SignatureAlgorithm.HS256, SECRET_KEY) .compact(); } public static Claims parseToken(String token) { return Jwts.parser() .setSigningKey(SECRET_KEY) .parseClaimsJws(token) .getBody(); } }5. 总结
本文围绕基于SpringBoot的宠物商店系统,从项目背景、技术栈、功能模块和核心代码四个方面进行了介绍。系统采用SpringBoot + MyBatis-Plus + MySQL + Vue.js的技术组合,实现了商品管理、购物车、订单处理等核心业务功能,具备良好的扩展性和可维护性。
通过本项目的设计与实现,不仅验证了SpringBoot在中小型业务系统开发中的高效性,也为宠物商店的数字化转型提供了一个切实可行的技术方案。后续可在此基础上进一步引入支付网关、消息队列、分布式缓存等组件,持续提升系统的性能与用户体验。