项目:数据库应用系统开发:智能电商管理系统

发布于:2025-07-01 ⋅ 阅读:(25) ⋅ 点赞:(0)

目录

1. 开发工具及环境

2.需求分析  

2.1 业务需求  

2.2  数据需求  

3. 数据库设计  

3.1 逻辑模型(表结构设计)如:

4. SQL代码示例

5. 关键索引与约束优化

6. 系统功能实现  

7. 系统测试  

8. 总结


1. 开发工具及环境

数据库:MySQL 8.0+  

后端语言:Java (Spring Boot)

前端框架:Vue.js7

开发工具:IntelliJ IDEA, VS Code

2.需求分析  

2.1 业务需求  

系统目标:解决商品在线销售、用户购物流程管理、订单处理效率问题,提供个性化商品选项和3D模型预览功能。

核心功能:  

功能1:用户注册登录(users表)  

功能2:商品多规格管理(categories表 + products表 + product_options表+product_price_matrix表)  

功能3:订单全流程处理(orders + order_items表) 

功能4:购物车与收藏夹(cart_items表 + favorites表)

功能5:地区数据管理(region_data表)

功能6:商品评价(reviews表)

功能7:收货地址(addresses表)

2.2  数据需求  

核心数据实体:如:  

用户(users):存储用户基础信息。

地址(addresses):与用户关联(user_id外键)。

商品(products):支持多规格选项(JSON字段)。

价格矩阵(product_price_matrix):定义商品组合价格。

订单(orders)与订单项(order_items):一对多关系。

购物车(cart_items)、收藏夹(favorites):关联用户与商品。

3. 数据库设计  

1.概念模型(E-R图)

实体用户、商品、价格矩阵、订单、订单商品、分类、地址、购物车、收藏夹

关系

用户 1:N 地址、订单、订单商品、收藏夹

用户 1:1 购物车

商品 N:1 分类

商品 N:N 订单商品、收藏夹

价格矩阵 N:1 商品

订单 1:N 用户、订单商品

订单 N:1 地址

订单商品 N:1 订单、商品

分类 1:N 商品

地址 1:N 用户

地址 N:1 订单

购物车 1:1 用户

购物车 1:N 商品

收藏夹 1:N 用户

收藏夹 N:1 商品

3.1 逻辑模型(表结构设计)如:

表一:user(用户表)

字段名

数据类型

约束

说明

id

INT

PK,AUTO_INCREMENT

用户ID

Email

Varchar(100)

Unique,Not null

邮箱

Password

Varchar(255)

Not null

密码(BCrypt加密)

表二:products(商品表)

字段名

数据类型

说明

size_options

JSON

尺寸选项

Price_matrix

JSON

价格矩阵

Has_3d_model

TINYINT

是否支持3D模型预览

4. SQL代码示例

CREATE TABLE `addresses` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` int NOT NULL,
  `receiver` varchar(50) NOT NULL,
  `phone` varchar(20) NOT NULL,
  `province` varchar(50) NOT NULL,
  `city` varchar(50) NOT NULL,
  `district` varchar(50) NOT NULL,
  `detail` varchar(255) NOT NULL,
  `is_default` tinyint DEFAULT '0' COMMENT '1-默认地址, 0-非默认',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `addresses_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
;
CREATE TABLE `cart_items` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` int NOT NULL,
  `product_id` int NOT NULL,
  `quantity` int NOT NULL DEFAULT '1',
  `selected` tinyint DEFAULT '1' COMMENT '1-选中, 0-未选中',
  `options` text COMMENT '选项JSON字符串',
  `price` decimal(10,2) DEFAULT NULL COMMENT '选择的规格价格',
  `size` varchar(20) DEFAULT NULL,
  `color` varchar(20) DEFAULT NULL,
  `light_type` varchar(20) DEFAULT NULL,
  `power` varchar(20) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `product_id` (`product_id`),
  CONSTRAINT `cart_items_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `cart_items_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
;
CREATE TABLE `categories` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `icon` varchar(255) DEFAULT NULL,
  `parent_id` int DEFAULT NULL,
  `sort_order` int DEFAULT '0',
  `status` tinyint DEFAULT '1' COMMENT '1-显示, 0-隐藏',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `parent_id` (`parent_id`),
  CONSTRAINT `categories_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
;
CREATE TABLE `favorites` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` int NOT NULL,
  `product_id` int NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`product_id`),
  KEY `product_id` (`product_id`),
  CONSTRAINT `favorites_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `favorites_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
;
CREATE TABLE `orders` (
  `id` int NOT NULL AUTO_INCREMENT,
  `order_no` varchar(50) NOT NULL,
  `user_id` int NOT NULL,
  `total_amount` decimal(10,2) NOT NULL,
  `status` tinyint DEFAULT '0' COMMENT '0-待付款, 1-待发货, 2-待收货, 3-已完成, 4-已取消',
  `payment_method` tinyint DEFAULT NULL COMMENT '1-支付宝, 2-微信, 3-银行卡',
  `payment_time` timestamp NULL DEFAULT NULL,
  `shipping_address_id` int DEFAULT NULL,
  `shipping_name` varchar(50) DEFAULT NULL,
  `shipping_phone` varchar(20) DEFAULT NULL,
  `shipping_address` text,
  `express_company` varchar(50) DEFAULT NULL,
  `express_no` varchar(50) DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `order_no` (`order_no`),
  KEY `user_id` (`user_id`),
  KEY `shipping_address_id` (`shipping_address_id`),
  CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
  CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`shipping_address_id`) REFERENCES `addresses` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
;
CREATE TABLE `order_items` (
  `id` int NOT NULL AUTO_INCREMENT,
  `order_id` int NOT NULL,
  `product_id` int NOT NULL,
  `product_name` varchar(255) NOT NULL,
  `product_image` varchar(255) DEFAULT NULL,
  `price` decimal(10,2) NOT NULL,
  `quantity` int NOT NULL DEFAULT '1',
  `options` text,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_order_id` (`order_id`),
  KEY `idx_product_id` (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
;
CREATE TABLE `products` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `description` text,
  `price` decimal(10,2) NOT NULL,
  `original_price` decimal(10,2) DEFAULT NULL,
  `stock` int DEFAULT '0',
  `sales` int DEFAULT '0',
  `category_id` int DEFAULT NULL,
  `image` varchar(255) DEFAULT NULL,
  `status` tinyint DEFAULT '1' COMMENT '1-上架, 0-下架',
  `material` varchar(50) DEFAULT NULL,
  `style` varchar(50) DEFAULT NULL,
  `applicable_space` varchar(100) DEFAULT NULL,
  `light_source` varchar(50) DEFAULT NULL,
  `power` varchar(30) DEFAULT NULL,
  `color_temperature` varchar(50) DEFAULT NULL,
  `light_type` varchar(50) DEFAULT NULL,
  `tag` varchar(20) DEFAULT NULL,
  `is_featured` tinyint DEFAULT '0',
  `size_options` json DEFAULT NULL COMMENT '尺寸选项,如[{"value":"s","label":"小号(30cm)"}]',
  `color_options` json DEFAULT NULL COMMENT '颜色选项,如[{"value":"white","label":"典雅白","code":"#FFFFFF"}]',
  `light_type_options` json DEFAULT NULL COMMENT '光源类型选项,如[{"value":"led","label":"LED"}]',
  `power_options` json DEFAULT NULL COMMENT '功率选项,如[{"value":"5w","label":"5W"}]',
  `price_matrix` json DEFAULT NULL COMMENT '价格矩阵,如[{"size":"s","color":"white","light_type":"led","power":"5w","price":199,"stock":100}]',
  `features` json DEFAULT NULL COMMENT '产品特性,如["节能环保","智能调光"]',
  `images` json DEFAULT NULL COMMENT '产品图片,如["image1.jpg"]',
  `has_3d_model` tinyint DEFAULT '0' COMMENT '是否有3D模型',
  `model_url` varchar(255) DEFAULT NULL COMMENT '3D模型URL',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `category_id` (`category_id`),
  CONSTRAINT `products_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
;
CREATE TABLE `product_options` (
  `id` int NOT NULL AUTO_INCREMENT,
  `product_id` int NOT NULL,
  `option_type` enum('size','color','light_type','power') NOT NULL,
  `option_value` varchar(50) NOT NULL,
  `option_label` varchar(100) NOT NULL,
  `option_extra` json DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `product_id` (`product_id`,`option_type`,`option_value`),
  CONSTRAINT `product_options_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
;
CREATE TABLE `product_price_matrix` (
  `id` int NOT NULL AUTO_INCREMENT,
  `product_id` int NOT NULL,
  `size` varchar(20) DEFAULT NULL,
  `color` varchar(20) DEFAULT NULL,
  `light_type` varchar(20) DEFAULT NULL,
  `power` varchar(20) DEFAULT NULL,
  `price` decimal(10,2) NOT NULL,
  `stock` int NOT NULL DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `product_id` (`product_id`,`size`,`color`,`light_type`,`power`),
  CONSTRAINT `product_price_matrix_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
;
CREATE TABLE `region_data` (
  `id` varchar(20) NOT NULL COMMENT '地区ID',
  `pid` varchar(20) NOT NULL COMMENT '父地区ID',
  `deep` tinyint(1) NOT NULL COMMENT '深度级别:0省,1市,2区县',
  `name` varchar(50) NOT NULL COMMENT '地区名称',
  `pinyin_prefix` varchar(10) DEFAULT NULL COMMENT '拼音首字母',
  `pinyin` varchar(100) DEFAULT NULL COMMENT '完整拼音',
  `ext_id` varchar(20) DEFAULT NULL COMMENT '扩展ID',
  `ext_name` varchar(100) DEFAULT NULL COMMENT '扩展名称',
  PRIMARY KEY (`id`),
  KEY `idx_pid` (`pid`),
  KEY `idx_deep` (`deep`),
  KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='地区数据表'
;
CREATE TABLE `reviews` (
  `id` int NOT NULL AUTO_INCREMENT,
  `product_id` int NOT NULL,
  `user_id` int NOT NULL,
  `order_id` int DEFAULT NULL,
  `rating` tinyint NOT NULL COMMENT '1-5星评分',
  `content` text,
  `images` text,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `product_id` (`product_id`),
  KEY `user_id` (`user_id`),
  KEY `order_id` (`order_id`),
  CONSTRAINT `reviews_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE,
  CONSTRAINT `reviews_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `reviews_ibfk_3` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
;
CREATE TABLE `users` (
  `id` int NOT NULL AUTO_INCREMENT,
  `nickname` varchar(50) NOT NULL,
  `email` varchar(100) NOT NULL,
  `phone` varchar(20) DEFAULT NULL,
  `password` varchar(255) NOT NULL,
  `avatar` varchar(255) DEFAULT NULL,
  `balance` decimal(10,2) DEFAULT '0.00',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `last_login` timestamp NULL DEFAULT NULL,
  `status` tinyint DEFAULT '1' COMMENT '1-正常, 0-禁用',
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `phone` (`phone`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

​​​​​​​5. 关键索引与约束优化

为高频查询字段添加索引,使用ON DELETE CASCADE保证数据一致性:

ALTER TABLE `addresses`
ADD CONSTRAINT `fk_user_address`
  FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
  ON DELETE CASCADE;

6. 系统功能实现  

1. 核心功能代码,用户注册(Java + Spring Boot):

@PostMapping("/register")
public ResponseEntity<User> registerUser(@RequestBody UserDTO userDTO) {
    // 密码加密
    String hashedPassword = BCrypt.hashpw(userDTO.getPassword(), BCrypt.gensalt());
    
    User newUser = new User();
    newUser.setEmail(userDTO.getEmail());
    newUser.setPassword(hashedPassword);
    
    userRepository.save(newUser); // 保存到数据库
    return ResponseEntity.ok(newUser);
}
(2)购物车商品添加(关联价格矩阵)
public void addToCart(int userId, int productId, String size, String color) {
    // 查询商品价格矩阵
    ProductPriceMatrix matrix = priceMatrixRepo.findByProductAndOptions(
        productId, size, color, null, null);
    
    CartItem item = new CartItem();
    item.setUserId(userId);
    item.setProductId(productId);
    item.setPrice(matrix.getPrice()); // 根据规格动态定价
    cartItemRepository.save(item);
}
(3)3D模型预览(前端Vue.js)
<template>
  <div v-if="product.has_3d_model">
    <iframe :src="product.model_url" class="model-viewer"></iframe>
  </div>
</template>

7. 系统测试  

1. 测试用例(部分)  

功能

输入

预期输出

结果

商品多规格选择

尺寸=“中号”,颜色=“黑色”

价格=299.00,库存=50

订单状态流转

支付订单(状态0->1)

状态更新为“待发货”

删除用户

删除用户ID=8

同步删除其地址/购物车数据

性能测试结果:

商品列表页响应时间:<500ms(添加category_id索引后优化60%)。

订单创建并发能力:支持1000+TPS(事务隔离级别优化)。

8. 总结

完成功能

用户体系(注册/登录)、商品多规格管理、3D预览。

订单全流程(创建→支付→发货→完成)。

1. 优化点:

索引优化:为orders.user_idproducts.category_id添加索引。

JSON字段查询:利用MySQL 8.0的JSON_EXTRACT()加速商品选项检索。  

2. 问题与解决:

问题1:商品多规格组合查询性能低下。

解决方案:拆解JSON字段为关系表product_price_matrix,查询效率提升10倍。 

问题2:订单状态并发修改冲突。  

解决方案:采用乐观锁(updated_at时间戳校验)。


网站公告

今日签到

点亮在社区的每一天
去签到