1. 为什么选择SSM框架开发鲜花销售系统
在Java企业级应用开发领域,SSM(Spring+SpringMVC+MyBatis)框架组合长期占据主流地位。这套技术栈特别适合中小型电商系统的快速开发,鲜花销售管理系统正是其典型应用场景。相比传统的Servlet/JSP原生架构,SSM提供了更完善的解耦方案:
-
Spring:通过IoC容器管理对象生命周期,用AOP实现事务控制。鲜花系统的订单处理、库存变更等操作都需要严格的事务保证,Spring的声明式事务配置只需简单注解:
java复制@Transactional public void updateStock(Long flowerId, int quantity) { flowerMapper.reduceStock(flowerId, quantity); inventoryLogMapper.insert(new InventoryLog(flowerId, quantity)); } -
SpringMVC:采用前端控制器模式,将HTTP请求路由到对应Controller。对于鲜花系统的商品展示页面,一个典型的控制器如下:
java复制@Controller @RequestMapping("/product") public class ProductController { @Autowired private FlowerService flowerService; @GetMapping("/detail/{id}") public String detail(@PathVariable Long id, Model model) { model.addAttribute("flower", flowerService.getById(id)); return "product_detail"; } } -
MyBatis:通过XML或注解将Java方法映射到SQL语句。处理鲜花分类查询时,动态SQL可以优雅地构建查询条件:
xml复制<select id="selectByCondition" resultType="Flower"> SELECT * FROM t_flower <where> <if test="categoryId != null"> AND category_id = #{categoryId} </if> <if test="minPrice != null"> AND price >= #{minPrice} </if> </where> </select>
这套组合的另一个优势是丰富的社区支持。当需要实现微信支付对接时,Spring的RestTemplate可以快速集成第三方API;MyBatis的PageHelper插件能轻松实现商品分页查询。相比Spring Boot的自动配置,SSM需要更多手动配置,但这也让开发者对底层机制有更清晰的控制。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 系统核心模块设计与实现
2.1 商品管理模块
鲜花商品区别于普通电商商品,需要特殊字段设计。数据库表t_flower包含以下关键字段:
sql复制CREATE TABLE `t_flower` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL COMMENT '鲜花名称',
`category_id` int(11) NOT NULL COMMENT '分类ID',
`price` decimal(10,2) NOT NULL COMMENT '销售价',
`original_price` decimal(10,2) DEFAULT NULL COMMENT '原价',
`stock` int(11) NOT NULL DEFAULT '0' COMMENT '库存',
`image_url` varchar(255) DEFAULT NULL COMMENT '主图URL',
`bloom_date` date DEFAULT NULL COMMENT '开花日期',
`shelf_life` int(11) DEFAULT NULL COMMENT '保质期(天)',
`is_recommend` tinyint(1) DEFAULT '0' COMMENT '是否推荐',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
商品展示页需要处理高并发访问,我们采用二级缓存策略:
- MyBatis一级缓存(SqlSession级别)
- Redis分布式缓存(商品详情JSON存储)
java复制public Flower getById(Long id) {
String cacheKey = "flower:" + id;
String json = redisTemplate.opsForValue().get(cacheKey);
if (StringUtils.isNotBlank(json)) {
return JSON.parseObject(json, Flower.class);
}
Flower flower = flowerMapper.selectByPrimaryKey(id);
if (flower != null) {
redisTemplate.opsForValue().set(cacheKey,
JSON.toJSONString(flower), 30, TimeUnit.MINUTES);
}
return flower;
}
2.2 订单处理流程
鲜花订单有较强的时效性,状态机设计如下:
code复制待支付 --(30分钟超时)--> 已取消
待支付 --(支付成功)--> 待发货
待发货 --(发货)--> 待收货
待收货 --(确认/7天自动)--> 已完成
订单创建时需要处理库存预占:
java复制public Order createOrder(OrderDTO dto) {
// 校验库存
Flower flower = flowerService.getById(dto.getFlowerId());
if (flower.getStock() < dto.getQuantity()) {
throw new BusinessException("库存不足");
}
// 扣减库存(乐观锁)
int rows = flowerMapper.reduceStockWithVersion(
dto.getFlowerId(),
dto.getQuantity(),
