基于SpringBoot的在线水果商城系统 - 源码深度解析

JavaJavaScriptHTMLCSSSpringboot框架SSM框架MavenMySQL
2026-02-0812 浏览

文章摘要

本项目是一款基于SpringBoot框架构建的在线水果商城系统,核心目标是为水果零售商或农户提供一个功能完整、易于维护的线上销售平台。系统有效解决了传统线下水果销售渠道单一、营业时间受限、客户覆盖面窄等核心痛点,通过将商品展示与交易流程线上化,帮助商家突破地域和时间限制,拓宽销售渠道,并利用数字化的...

在传统的水果零售行业中,实体店铺面临着营业时间固定、客户覆盖范围有限、库存管理效率低下等挑战。随着电子商务的快速发展,水果销售行业也迎来了数字化转型的契机。本文介绍的"鲜果易购"平台正是基于SpringBoot框架构建的专业级在线水果销售解决方案,旨在为中小型水果零售商和个体果农提供功能完备、易于维护的线上销售渠道。

系统架构与技术栈

该平台采用经典的三层架构设计,后端基于SpringBoot 2.x框架,充分利用其自动配置和起步依赖特性,快速集成了Spring MVC、Spring Data JPA、Spring Security等核心模块。前端使用Thymeleaf模板引擎结合Bootstrap组件库构建响应式用户界面,确保在不同设备上都能提供良好的用户体验。

核心技术栈配置:

# 应用基础配置
spring:
  datasource:
    url: jdbc:mysql://www.csbishe.cn/boot_shuiguoshop?useSSL=false&serverTimezone=Asia/Shanghai
    username: boot_shuiguoshop
    password: boot_shuiguoshop
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: org.apache.commons.dbcp2.BasicDataSource
    dbcp2:
      max-wait-millis: 10000
      min-idle: 5
      initial-size: 5
  
  jpa:
    show-sql: true
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB

server:
  port: 40000
  servlet:
    session:
      timeout: -1

logging:
  level:
    com.soft.demo.dao: debug

数据库设计亮点分析

商品表设计优化

商品表(goods)的设计体现了电商系统的核心数据模型,采用了合理的字段类型和索引策略:

CREATE TABLE `goods` (
  `goods_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品ID',
  `goods_type_id` int(11) DEFAULT 0 COMMENT '商品类型ID',
  `goods_no` varchar(50) DEFAULT NULL COMMENT '商品编号',
  `goods_name` varchar(225) DEFAULT NULL COMMENT '商品名称',
  `goods_pic` varchar(225) DEFAULT NULL COMMENT '商品图片',
  `goods_publisher` varchar(225) DEFAULT NULL COMMENT '商品生产商',
  `goods_price` double DEFAULT 0 COMMENT '商品价格',
  `goods_discount` double DEFAULT NULL COMMENT '商品折扣',
  `goods_date` varchar(50) DEFAULT NULL COMMENT '商品上架时间',
  `goods_desc` text DEFAULT NULL COMMENT '商品描述',
  PRIMARY KEY (`goods_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='商品表'

设计亮点:

  1. 主键优化:使用自增INT类型作为主键,确保插入性能和数据连续性
  2. 字段长度规划:商品名称和描述字段长度合理,商品图片路径使用varchar(225)满足存储需求
  3. 价格字段设计:采用double类型存储价格,支持小数精度,满足水果按重量计价的需求
  4. 索引策略:在主键上建立BTREE索引,提升商品查询效率

购物车表业务逻辑设计

购物车表(gouwuche)的设计充分考虑了电商场景下的并发和业务需求:

CREATE TABLE `gouwuche` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '购物车ID',
  `goods_id` int(11) DEFAULT NULL COMMENT '商品ID',
  `user_id` int(11) DEFAULT NULL COMMENT '用户ID',
  `time` date DEFAULT NULL COMMENT '加入时间',
  `count` int(11) NOT NULL DEFAULT 1 COMMENT '数量',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=160 DEFAULT CHARSET=utf8 COMMENT='购物车表'

业务逻辑考量:

  1. 数量控制:count字段设置默认值1,避免空值问题
  2. 时间记录:记录加入购物车时间,便于清理过期商品
  3. 用户关联:通过user_id关联用户,支持多用户并发操作
  4. 商品关联:goods_id外键关联确保数据一致性

购物车功能

核心功能实现深度解析

1. 商品展示与搜索模块

商品展示模块采用分层架构设计,通过JPA实现数据持久化操作:

@Entity
@Table(name = "goods")
public class Goods extends BaseDomain {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "goods_id")
    private Integer goodsId;
    
    @Column(name = "goods_name")
    private String goodsName;
    
    @Column(name = "goods_price")
    private Double goodsPrice;
    
    @Column(name = "goods_discount")
    private Double goodsDiscount;
    
    @Column(name = "goods_desc", length = 1000)
    private String goodsDesc;
    
    // 省略getter/setter方法
    
    public Double getFinalPrice() {
        if (goodsDiscount != null && goodsDiscount > 0) {
            return goodsPrice * goodsDiscount;
        }
        return goodsPrice;
    }
}

业务层实现商品查询逻辑:

@Service
@Transactional
public class GoodsService {
    
    @Autowired
    private GoodsRepository goodsRepository;
    
    public Page<Goods> searchGoods(String keyword, Integer typeId, Pageable pageable) {
        Specification<Goods> spec = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            
            if (StringUtils.hasText(keyword)) {
                Predicate namePredicate = cb.like(root.get("goodsName"), "%" + keyword + "%");
                Predicate descPredicate = cb.like(root.get("goodsDesc"), "%" + keyword + "%");
                predicates.add(cb.or(namePredicate, descPredicate));
            }
            
            if (typeId != null && typeId > 0) {
                predicates.add(cb.equal(root.get("goodsTypeId"), typeId));
            }
            
            return cb.and(predicates.toArray(new Predicate[0]));
        };
        
        return goodsRepository.findAll(spec, pageable);
    }
}

商品展示页面

2. 购物车管理功能

购物车功能采用Session与数据库结合的方式,确保用户数据的持久化:

@RestController
@RequestMapping("/cart")
public class CartController {
    
    @PostMapping("/add")
    public ResponseEntity<?> addToCart(@RequestParam Integer goodsId, 
                                      @RequestParam Integer quantity,
                                      HttpSession session) {
        try {
            User user = (User) session.getAttribute("currentUser");
            if (user == null) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
            
            CartItem existingItem = cartService.findByUserIdAndGoodsId(user.getUserId(), goodsId);
            if (existingItem != null) {
                existingItem.setCount(existingItem.getCount() + quantity);
                cartService.updateCartItem(existingItem);
            } else {
                CartItem newItem = new CartItem();
                newItem.setGoodsId(goodsId);
                newItem.setUserId(user.getUserId());
                newItem.setCount(quantity);
                newItem.setTime(new Date());
                cartService.addToCart(newItem);
            }
            
            return ResponseEntity.ok("添加成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    
    @GetMapping("/items")
    public List<CartDTO> getCartItems(HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        return cartService.getCartDetailsByUserId(user.getUserId());
    }
}

购物车数据传输对象设计:

public class CartDTO {
    private Integer cartId;
    private Integer goodsId;
    private String goodsName;
    private String goodsPic;
    private Double goodsPrice;
    private Double goodsDiscount;
    private Integer quantity;
    private Double subtotal;
    
    public Double getSubtotal() {
        Double price = goodsDiscount != null ? goodsPrice * goodsDiscount : goodsPrice;
        return price * quantity;
    }
    
    // 省略getter/setter方法
}

3. 订单处理流程

订单生成模块实现了完整的业务流程,包括库存校验、价格计算和状态管理:

@Service
public class OrderService {
    
    @Autowired
    private OrderRepository orderRepository;
    
    @Autowired
    private GoodsRepository goodsRepository;
    
    @Autowired
    private CartService cartService;
    
    @Transactional
    public Order createOrder(Integer userId, List<CartItem> cartItems, String shippingAddress) {
        Order order = new Order();
        order.setUserId(userId);
        order.setOrderTime(new Date());
        order.setShippingAddress(shippingAddress);
        order.setStatus(OrderStatus.PENDING_PAYMENT);
        
        Double totalAmount = 0.0;
        List<OrderItem> orderItems = new ArrayList<>();
        
        for (CartItem cartItem : cartItems) {
            Goods goods = goodsRepository.findById(cartItem.getGoodsId())
                    .orElseThrow(() -> new RuntimeException("商品不存在"));
            
            if (goods.getStock() < cartItem.getCount()) {
                throw new RuntimeException("商品库存不足");
            }
            
            OrderItem orderItem = new OrderItem();
            orderItem.setGoodsId(goods.getGoodsId());
            orderItem.setGoodsName(goods.getGoodsName());
            orderItem.setQuantity(cartItem.getCount());
            orderItem.setUnitPrice(goods.getFinalPrice());
            orderItem.setSubtotal(orderItem.getUnitPrice() * orderItem.getQuantity());
            
            orderItems.add(orderItem);
            totalAmount += orderItem.getSubtotal();
            
            // 更新库存
            goods.setStock(goods.getStock() - cartItem.getCount());
            goodsRepository.save(goods);
        }
        
        order.setOrderItems(orderItems);
        order.setTotalAmount(totalAmount);
        order.setOrderNumber(generateOrderNumber());
        
        return orderRepository.save(order);
    }
    
    private String generateOrderNumber() {
        return "ORD" + System.currentTimeMillis() + 
               String.format("%04d", new Random().nextInt(10000));
    }
}

订单管理界面

4. 管理员商品管理

后台管理系统提供了完整的商品CRUD操作,支持图片上传和批量处理:

@Controller
@RequestMapping("/admin/goods")
public class AdminGoodsController {
    
    @PostMapping("/upload")
    public String uploadGoods(@ModelAttribute Goods goods, 
                             @RequestParam("imageFile") MultipartFile imageFile) {
        try {
            if (!imageFile.isEmpty()) {
                String fileName = storeImage(imageFile);
                goods.setGoodsPic(fileName);
            }
            
            goods.setGoodsDate(new Date());
            goodsService.saveGoods(goods);
            
            return "redirect:/admin/goods/list";
        } catch (Exception e) {
            return "error";
        }
    }
    
    @GetMapping("/list")
    public String goodsList(@RequestParam(defaultValue = "0") Integer page,
                           Model model) {
        Pageable pageable = PageRequest.of(page, 10, Sort.by("goodsDate").descending());
        Page<Goods> goodsPage = goodsService.findAllGoods(pageable);
        
        model.addAttribute("goodsPage", goodsPage);
        model.addAttribute("currentPage", page);
        
        return "admin/goods-list";
    }
}

商品管理后台

实体模型设计

系统采用面向对象的设计思想,通过基类封装通用属性和行为:

public abstract class BaseDomain implements Serializable {
    private static final long serialVersionUID = -3308831596689250063L;
    
    private int start;
    private int limit = 20;
    private int end;
    private String sort;
    private String order;
    private String dir;
    
    public int getStart() {
        return start;
    }
    
    public void setStart(int start) {
        this.start = start;
    }
    
    public int getEnd() {
        return end;
    }
    
    public void setEnd(int end) {
        this.end = end;
    }
    
    // 其他getter/setter方法
}

商品实体类扩展实现:

@Entity
@Table(name = "goods")
public class Goods extends BaseDomain {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer goodsId;
    
    @Column(name = "goods_name")
    @NotBlank(message = "商品名称不能为空")
    private String goodsName;
    
    @Column(name = "goods_price")
    @Min(value = 0, message = "价格不能为负数")
    private Double goodsPrice;
    
    @Column(name = "goods_discount")
    @DecimalMin(value = "0.0", message = "折扣不能为负数")
    @DecimalMax(value = "1.0", message = "折扣不能大于1")
    private Double goodsDiscount;
    
    @Transient
    public Double getFinalPrice() {
        return goodsPrice * (goodsDiscount != null ? goodsDiscount : 1.0);
    }
    
    @Transient
    public boolean isOnDiscount() {
        return goodsDiscount != null && goodsDiscount < 1.0;
    }
}

功能展望与优化方向

1. 引入Redis缓存提升性能

实现思路: 使用Redis缓存热点商品数据、用户会话信息和页面静态化内容

@Configuration
@EnableCaching
public class RedisConfig {
    
    @Bean
    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. 微服务架构改造

架构规划: 将单体应用拆分为用户服务、商品服务、订单服务、支付服务等独立微服务,通过Spring Cloud实现服务治理。

3. 移动端适配优化

技术方案: 开发React Native或Flutter移动应用,通过RESTful API与后端服务交互,提供原生移动体验。

4. 智能推荐系统

算法实现: 基于用户行为数据构建协同过滤推荐模型,个性化推荐水果商品:

@Service
public class RecommendationService {
    
    public List<Goods> recommendGoods(Integer userId, int limit) {
        // 基于用户历史订单和浏览行为实现推荐逻辑
        return collaborativeFiltering(userId, limit);
    }
    
    private List<Goods> collaborativeFiltering(Integer userId, int limit) {
        // 实现协同过滤算法
        return goodsRepository.findRecommendedGoods(userId, limit);
    }
}

5. 物流跟踪集成

第三方对接: 集成主流物流公司API,实现订单物流状态的实时跟踪和推送通知。

总结

鲜果易购平台通过SpringBoot框架实现了完整的水果电商解决方案,在数据库设计上注重业务逻辑的合理性和性能优化,在功能实现上采用分层架构确保代码的可维护性和扩展性。系统不仅提供了基础的商品展示、购物车、订单管理等核心功能,还通过合理的技术选型和架构设计为后续的功能扩展奠定了坚实基础。

未来的优化方向主要集中在性能提升、架构演进和用户体验改善三个方面,通过引入缓存、微服务改造、移动端适配等技术手段,可以进一步提升系统的竞争力和用户满意度。该项目的技术实现为同类电商平台的开发提供了有价值的参考和实践经验。

本文关键词
SpringBoot在线水果商城源码解析系统架构数据库设计

上下篇

上一篇
没有更多文章
下一篇
没有更多文章