基于SSM框架的在线商品进销存管理系统 - 源码深度解析
在当今数字化商业浪潮中,中小型商贸企业对高效库存管理的需求变得尤为迫切。传统的手工记账和Excel表格管理方式存在数据易错、信息孤岛、统计滞后等系统性痛点,严重制约企业的运营效率和决策质量。针对这一市场需求,我们开发了一套企业级智能库存管理平台,通过全流程数字化管理,实现商品从采购入库、在库管理到销售出库的精准追踪与智能分析。
系统架构与技术栈选型
该平台采用业界经典的SSM(Spring + Spring MVC + MyBatis)框架组合,构建了清晰的三层架构体系:
架构层次解析:
- 控制层(Web层):基于Spring MVC框架,以DispatcherServlet为核心控制器,实现请求路由、参数绑定和视图解析
- 业务逻辑层(Service层):Spring框架管理业务组件,通过AOP实现事务管理、日志记录等横切关注点
- 数据访问层(DAO层):MyBatis负责数据持久化,提供灵活的SQL映射和对象关系映射
核心技术栈配置:
<dependencies>
<!-- Spring MVC Web框架 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
<!-- MyBatis持久层框架 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- MySQL数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- 数据源连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
数据库设计架构深度解析
出入库流水明细表设计策略
系统采用业界标准的主从表结构设计,确保数据的一致性和完整性。以下以入库明细表为例展示核心设计:
CREATE TABLE `instockdetails` (
`i_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '入库详单编号',
`inStock_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '入库单编号',
`p_id` int(11) DEFAULT NULL COMMENT '产品编号',
`i_num` int(11) DEFAULT NULL COMMENT '入库数量',
`i_price` decimal(10,2) DEFAULT NULL COMMENT '入库单价',
`i_total` decimal(12,2) DEFAULT NULL COMMENT '入库金额',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`i_id`),
KEY `idx_instock_id` (`inStock_id`),
KEY `idx_product_id` (`p_id`),
KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COMMENT='入库详单表'
设计亮点深度分析:
- 主键优化策略:采用AUTO_INCREMENT自增主键,确保主键的唯一性和连续性,提升索引效率
- 字符集智能配置:业务字段使用utf8mb4字符集,全面支持Emoji和生僻字,满足多元化业务需求
- 索引优化设计:针对查询频率高的字段建立复合索引,显著提升查询性能
- 审计字段完备:包含创建时间和更新时间字段,便于数据追踪和审计
- 事务安全保障:采用InnoDB存储引擎,支持ACID事务特性,确保高并发下的数据一致性
销售与退货业务闭环设计
销售与退货模块采用关联表设计,实现完整的业务数据闭环:
-- 销售主表设计
CREATE TABLE `sell` (
`s_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '销售单编号',
`shop_id` int(11) DEFAULT NULL COMMENT '网店编号',
`u_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '管理员编号',
`customer_id` int(11) DEFAULT NULL COMMENT '客户编号',
`s_date` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '销售时间',
`s_total` decimal(12,2) DEFAULT '0.00' COMMENT '销售总额',
`s_status` tinyint(1) DEFAULT '1' COMMENT '订单状态:1-正常 2-已退货',
`s_bz` varchar(150) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`s_id`),
KEY `idx_shop_date` (`shop_id`,`s_date`),
KEY `idx_customer` (`customer_id`),
KEY `idx_status` (`s_status`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='销售单表'
-- 退货明细表设计
CREATE TABLE `goodsbackdetails` (
`g_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '退货详单编号',
`goodsBack_id` int(11) DEFAULT NULL COMMENT '退货单编号',
`sell_id` int(11) DEFAULT NULL COMMENT '原销售单编号',
`p_id` int(11) DEFAULT NULL COMMENT '产品编号',
`g_num` int(11) DEFAULT NULL COMMENT '退货数量',
`g_reason` varchar(200) DEFAULT NULL COMMENT '退货原因',
`g_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '退货时间',
PRIMARY KEY (`g_id`),
KEY `idx_sell_id` (`sell_id`),
KEY `idx_product_id` (`p_id`),
CONSTRAINT `fk_sell_back` FOREIGN KEY (`sell_id`) REFERENCES `sell` (`s_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COMMENT='退货详单表'
核心业务功能实现详解
客户管理模块架构设计
客户信息管理作为企业运营的基础核心模块,系统提供了完整的CRUD操作和业务验证机制。
控制器层实现代码:
package com.friday.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.friday.model.Customer;
import com.friday.service.CustomerService;
import com.friday.service.impl.CustomerServiceImpl;
import com.friday.validation.CustomerValidator;
/**
* 客户信息添加控制器
* 负责处理客户信息的添加请求和参数验证
*/
public class AddCustomerController implements Controller {
private CustomerValidator customerValidator;
private CustomerService customerService;
public AddCustomerController() {
this.customerValidator = new CustomerValidator();
this.customerService = new CustomerServiceImpl();
}
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
try {
// 参数获取与基础验证
String name = request.getParameter("name");
String phone = request.getParameter("phone");
String address = request.getParameter("address");
String bz = request.getParameter("remark");
// 创建客户对象
Customer customer = new Customer();
customer.setcName(name);
customer.setcPhone(phone);
customer.setcAddress(address);
customer.setcNote(bz);
// 业务规则验证
if (!customerValidator.validate(customer)) {
model.put("error", customerValidator.getErrorMessage());
return new ModelAndView("customer_add", model);
}
// 执行添加操作
customerService.addCustomer(customer);
// 返回客户列表
List<Customer> customerList = customerService.queryCustomer();
model.put("result", customerList);
model.put("success", "客户添加成功");
return new ModelAndView("customer_management", model);
} catch (Exception e) {
model.put("error", "操作失败:" + e.getMessage());
return new ModelAndView("error", model);
}
}
}
服务层业务逻辑实现:
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper customerMapper;
@Override
@Transactional(readOnly = false, rollbackFor = Exception.class)
public void addCustomer(Customer customer) {
// 数据完整性验证
if (customer.getcName() == null || customer.getcName().trim().isEmpty()) {
throw new IllegalArgumentException("客户名称不能为空");
}
// 手机号格式验证
if (!isValidPhone(customer.getcPhone())) {
throw new IllegalArgumentException("手机号格式不正确");
}
// 客户名称重复性检查
if (customerMapper.existsByName(customer.getcName())) {
throw new BusinessException("客户名称已存在");
}
// 数据持久化操作
int result = customerMapper.insert(customer);
if (result <= 0) {
throw new DataAccessException("客户信息保存失败");
}
// 记录操作日志
logService.recordOperation("添加客户", "客户名称:" + customer.getcName());
}
/**
* 手机号格式验证
*/
private boolean isValidPhone(String phone) {
if (phone == null) return false;
return phone.matches("^1[3-9]\\d{9}$");
}
}

技术实现亮点:
- 分层架构清晰:严格遵循MVC模式,各层职责明确
- 事务管理完善:使用Spring声明式事务,确保数据一致性
- 参数验证全面:前后端双重验证,提升系统健壮性
- 异常处理规范:统一的异常处理机制,提高系统可维护性
- 日志记录完整:关键操作留痕,便于审计和问题追踪
该系统通过精心的架构设计和严谨的编码实现,为中小型企业提供了稳定可靠的库存管理解决方案,有效提升了企业的数字化管理水平。