基于SSH框架的在线化妆品销售平台 - 源码深度解析

JavaJavaScriptSSH框架HTMLCSSMySQLJSP+Servlet
2026-02-1054 浏览

文章摘要

本项目是一款基于SSH(Struts2 + Spring + Hibernate)整合框架开发的在线化妆品销售平台,旨在为化妆品品牌或零售商提供一个功能完备、稳定可靠的B2C电子商务解决方案。其核心业务价值在于解决了传统线下或简单线上销售模式中商品信息更新滞后、订单处理效率低下以及客户数据管理混乱的...

企业级化妆品电商平台的技术架构与实现

在电子商务蓬勃发展的浪潮中,化妆品行业对线上销售平台的需求日益增长。本文深入剖析基于经典SSH(Struts2 + Spring + Hibernate)整合框架的企业级化妆品电商平台,为化妆品品牌和零售商提供一套完整的B2C电子商务解决方案。

系统架构与技术栈

该平台采用典型的三层架构设计,确保系统的高内聚低耦合:

前端技术栈

  • JSP动态页面技术,结合jQuery等JavaScript库实现丰富的用户交互体验
  • 响应式设计,适配不同终端设备
  • AJAX异步通信,提升用户体验

后端架构核心

  • Struts2:作为MVC框架负责请求分发和页面跳转控制,通过拦截器实现统一的安全验证
  • Spring框架:通过IoC容器管理业务组件,利用AOP实现事务管理和日志记录
  • Hibernate:作为ORM框架处理数据持久化,提供对象关系映射和数据缓存机制
// Spring配置文件示例
@Configuration
@EnableTransactionManagement
@ComponentScan("com.cosmetics.platform")
public class AppConfig {
    
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/cosmetics_db?useUnicode=true&characterEncoding=UTF-8");
        dataSource.setUsername("root");
        dataSource.setPassword("password");
        return dataSource;
    }
    
    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan("com.cosmetics.platform.entity");
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        hibernateProperties.setProperty("hibernate.show_sql", "true");
        sessionFactory.setHibernateProperties(hibernateProperties);
        return sessionFactory;
    }
    
    @Bean
    public HibernateTransactionManager transactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }
}

数据库设计亮点

订单模块的精细化设计

订单系统采用主从表结构设计,t_order表存储订单基本信息,t_orderitem表记录订单明细,这种设计支持一个订单包含多个商品的需求。

-- 订单主表结构
CREATE TABLE `t_order` (
  `order_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单ID',
  `order_bianhao` varchar(88) DEFAULT NULL COMMENT '订单编号',
  `order_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '订单日期',
  `order_zhuangtai` varchar(255) DEFAULT '待付款' COMMENT '订单状态',
  `order_songhuodizhi` varchar(255) DEFAULT NULL COMMENT '送货地址',
  `order_fukuangfangshi` varchar(255) DEFAULT NULL COMMENT '付款方式',
  `order_jine` decimal(10,2) DEFAULT NULL COMMENT '订单金额',
  `order_user_id` int(11) DEFAULT NULL COMMENT '用户ID',
  PRIMARY KEY (`order_id`),
  UNIQUE KEY `idx_order_bianhao` (`order_bianhao`),
  KEY `idx_user_id` (`order_user_id`),
  KEY `idx_order_date` (`order_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单表';

-- 订单明细表结构
CREATE TABLE `t_orderitem` (
  `orderItem_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单项ID',
  `order_id` int(11) DEFAULT NULL COMMENT '订单ID',
  `goods_id` int(11) DEFAULT NULL COMMENT '商品ID',
  `goods_quantity` int(11) DEFAULT NULL COMMENT '商品数量',
  `unit_price` decimal(10,2) DEFAULT NULL COMMENT '商品单价',
  PRIMARY KEY (`orderItem_id`),
  KEY `idx_order_id` (`order_id`),
  KEY `idx_goods_id` (`goods_id`),
  CONSTRAINT `fk_orderitem_order` FOREIGN KEY (`order_id`) REFERENCES `t_order` (`order_id`) ON DELETE CASCADE,
  CONSTRAINT `fk_orderitem_goods` FOREIGN KEY (`goods_id`) REFERENCES `t_goods` (`goods_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单项表';

设计优势分析

  • 数据一致性:通过外键约束确保订单项与主订单的严格对应关系
  • 事务完整性:采用数据库事务保证订单创建的原子性操作
  • 查询性能优化
    • 为订单编号字段建立唯一索引,提升查询效率
    • 为用户ID和订单日期建立复合索引,优化历史订单查询
  • 扩展性设计:支持订单状态追踪、支付方式多样化等业务需求

商品信息管理的灵活架构

商品表t_goods设计了完善的价格体系和分类管理机制:

CREATE TABLE `t_goods` (
  `goods_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品ID',
  `goods_name` varchar(200) NOT NULL COMMENT '商品名称',
  `goods_miaoshu` text COMMENT '商品描述',
  `goods_pic` varchar(500) DEFAULT NULL COMMENT '商品图片',
  `goods_shichangjia` decimal(10,2) DEFAULT NULL COMMENT '市场价',
  `goods_tejia` decimal(10,2) DEFAULT NULL COMMENT '特价',
  `goods_isnottejia` tinyint(1) DEFAULT '0' COMMENT '是否特价:0-否,1-是',
  `goods_isnottuijian` tinyint(1) DEFAULT '0' COMMENT '是否推荐:0-否,1-是',
  `goods_catelog_id` int(11) DEFAULT NULL COMMENT '分类ID',
  `goods_stock` int(11) DEFAULT '0' COMMENT '商品库存',
  `goods_sales` int(11) DEFAULT '0' COMMENT '商品销量',
  `goods_Del` tinyint(1) DEFAULT '0' COMMENT '是否删除:0-否,1-是',
  `create_time` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`goods_id`),
  KEY `idx_catelog_id` (`goods_catelog_id`),
  KEY `idx_is_tuijian` (`goods_isnottuijian`),
  KEY `idx_is_tejia` (`goods_isnottejia`),
  FULLTEXT KEY `idx_goods_name` (`goods_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商品表';

商品表设计亮点

  • 多维度定价策略:支持市场价、特价的多价格体系,便于促销活动管理
  • 商品状态精细控制:通过布尔标志位控制商品推荐状态和软删除机制
  • 分类管理优化:通过分类ID实现商品分类的灵活管理和快速检索
  • 全文搜索支持:为商品名称建立全文索引,提升搜索体验
  • 库存销量监控:实时跟踪商品库存和销售数据,为运营决策提供支持

商品信息管理界面

核心功能实现

用户认证与权限管理

系统采用基于角色的访问控制(RBAC)模型,支持管理员和普通用户两种角色权限管理。

// 用户实体类 - 增强版设计
@Entity
@Table(name = "t_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer user_id;
    
    @Column(nullable = false, unique = true, length = 50)
    private String user_name;
    
    @Column(nullable = false, length = 64)
    private String user_pw; // 存储加密后的密码
    
    @Column(name = "user_type", nullable = false)
    private Integer user_type; // 用户类型:0-管理员,1-普通用户
    
    private String user_realname;
    private String user_sex;
    private Integer user_age;
    private String user_address;
    private String user_tel;
    
    @Column(unique = true)
    private String user_email;
    
    @Column(name = "user_del", nullable = false)
    private Boolean user_del = false; // 软删除标志
    
    @CreationTimestamp
    private Timestamp create_time;
    
    @UpdateTimestamp
    private Timestamp update_time;
    
    // 关联关系定义
    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private Set<Order> orders = new HashSet<>();
    
    // 密码加密方法
    public void encryptPassword() {
        this.user_pw = BCrypt.hashpw(this.user_pw, BCrypt.gensalt());
    }
    
    // 密码验证方法
    public boolean checkPassword(String plainPassword) {
        return BCrypt.checkpw(plainPassword, this.user_pw);
    }
    
    // getter和setter方法省略
}

// 用户登录验证Struts2 Action - 增强安全设计
public class UserLoginAction extends ActionSupport {
    private String username;
    private String password;
    private UserService userService;
    
    // 登录尝试次数限制
    private static final int MAX_LOGIN_ATTEMPTS = 5;
    
    public String execute() {
        try {
            // 输入验证
            if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
                addActionError("用户名和密码不能为空");
                return ERROR;
            }
            
            User user = userService.validateUser(username, password);
            if (user != null) {
                if (user.getUser_del()) {
                    addActionError("该账户已被禁用");
                    return ERROR;
                }
                
                // 将用户信息存入session
                Map<String, Object> session = ActionContext.getContext().getSession();
                session.put("currentUser", user);
                session.put("loginTime", new Date());
                
                // 根据用户类型跳转不同页面
                if (user.getUser_type() == 0) {
                    return "admin";
                } else {
                    return "user";
                }
            } else {
                addActionError("用户名或密码错误");
                return ERROR;
            }
        } catch (Exception e) {
            addActionError("登录系统异常,请稍后重试");
            return ERROR;
        }
    }
    
    // 省略其他方法
}

用户登录界面设计

购物车与订单处理

购物车功能采用Session与数据库结合的方式,确保数据的临时存储和持久化需求。

// 购物车项实体 - 完整实现
public class CartItem {
    private Goods goods;
    private Integer quantity;
    private Double subtotal;
    
    public CartItem(Goods goods, Integer quantity) {
        this.goods = goods;
        this.quantity = quantity;
        this.subtotal = calculateSubtotal();
    }
    
    public Double getSubtotal() {
        return subtotal;
    }
    
    private Double calculateSubtotal() {
        // 优先使用特价,如果没有特价则使用正常价格
        Double price = goods.getGoods_tejia() != null ? 
                      goods.getGoods_tejia() : goods.getGoods_shichangjia();
        return price * quantity;
    }
    
    public void updateQuantity(Integer newQuantity) {
        this.quantity = newQuantity;
        this.subtotal = calculateSubtotal();
    }
    
    // getter和setter方法
    public Goods getGoods() { return goods; }
    public void setGoods(Goods goods) { 
        this.goods = goods; 
        this.subtotal = calculateSubtotal();
    }
    
    public Integer getQuantity() { return quantity; }
    public void setQuantity(Integer quantity) { 
        this.quantity = quantity; 
        this.subtotal = calculateSubtotal();
    }
}

// 购物车管理服务
@Service
@Transactional
public class CartService {
    
    @Autowired
    private GoodsService goodsService;
    
    /**
     * 添加商品到购物车
     */
    public void addToCart(Map<Integer, CartItem> cart, Integer goodsId, Integer quantity) {
        Goods goods = goodsService.getGoodsById(goodsId);
        if (goods == null) {
            throw new RuntimeException("商品不存在");
        }
        
        if (cart.containsKey(goodsId)) {
            CartItem existingItem = cart.get(goodsId);
            existingItem.setQuantity(existingItem.getQuantity() + quantity);
        } else {
            CartItem newItem = new CartItem(goods, quantity);
            cart.put(goodsId, newItem);
        }
    }
    
    /**
     * 计算购物车总金额
     */
    public Double calculateTotal(Map<Integer, CartItem> cart) {
        return cart.values().stream()
                .mapToDouble(CartItem::getSubtotal)
                .sum();
    }
    
    /**
     * 从购物车移除商品
     */
    public void removeFromCart(Map<Integer, CartItem> cart, Integer goodsId) {
        cart.remove(goodsId);
    }
    
    /**
     * 清空购物车
     */
    public void clearCart(Map<Integer, CartItem> cart) {
        cart.clear();
    }
}

订单处理业务流程

  1. 购物车验证:检查商品库存和价格一致性
  2. 订单生成:创建订单主表和明细记录
  3. 库存扣减:原子性操作确保库存准确性
  4. 支付处理:集成第三方支付接口
  5. 状态更新:实时跟踪订单状态变化

该化妆品电商平台通过SSH框架的有机整合,实现了高可用、易扩展的企业级电商解决方案,为化妆品行业的数字化转型提供了强有力的技术支撑。

本文关键词
SSH框架在线化妆品销售电商平台源码解析技术架构

上下篇

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