当前位置: 首页 > article >正文

基于 SSM(Spring + Spring MVC + MyBatis)框架构建电器网上订购系统

基于 SSM(Spring + Spring MVC + MyBatis)框架构建电器网上订购系统可以为用户提供一个方便快捷的购物平台。以下将详细介绍该系统的开发流程,包括需求分析、技术选型、数据库设计、项目结构搭建、主要功能实现以及前端页面设计。
在这里插入图片描述

需求分析

电器网上订购系统应具备以下功能:

  • 用户注册与登录
  • 商品展示与搜索
  • 购物车管理
  • 订单管理
  • 支付接口集成
  • 后台管理系统(商品管理、订单管理、用户管理)

技术选型

  • 后端:Java、Spring、Spring MVC、MyBatis
  • 前端:HTML、CSS、JavaScript、JQuery
  • 数据库:MySQL
  • 开发工具:IntelliJ IDEA 或 Eclipse
  • 服务器:Tomcat

数据库设计

创建数据库表以存储用户信息、商品信息、购物车信息、订单信息等。

用户表(users)
  • id (INT, 主键, 自增)
  • username (VARCHAR)
  • password (VARCHAR)
  • email (VARCHAR)
  • phone (VARCHAR)
商品表(products)
  • id (INT, 主键, 自增)
  • name (VARCHAR)
  • description (TEXT)
  • price (DECIMAL)
  • stock (INT)
  • category (VARCHAR)
  • image_url (VARCHAR)
购物车表(cart_items)
  • id (INT, 主键, 自增)
  • user_id (INT, 外键)
  • product_id (INT, 外键)
  • quantity (INT)
订单表(orders)
  • id (INT, 主键, 自增)
  • user_id (INT, 外键)
  • order_date (DATETIME)
  • total_price (DECIMAL)
  • status (VARCHAR)
订单详情表(order_details)
  • id (INT, 主键, 自增)
  • order_id (INT, 外键)
  • product_id (INT, 外键)
  • quantity (INT)
  • price (DECIMAL)

项目结构搭建

  1. 创建 Maven 项目

    • 在 IDE 中创建一个新的 Maven 项目。
    • 修改 pom.xml 文件,添加必要的依赖项。
  2. 配置文件

    • application.properties
      spring.datasource.url=jdbc:mysql://localhost:3306/electronics_store?useSSL=false&serverTimezone=UTC
      spring.datasource.username=root
      spring.datasource.password=root
      spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
      
      mybatis.mapper-locations=classpath:mapper/*.xml
      
    • spring-mvc.xml
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context.xsd
             http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc.xsd">
      
          <context:component-scan base-package="com.electronics"/>
          <mvc:annotation-driven/>
      
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/views/"/>
              <property name="suffix" value=".jsp"/>
          </bean>
      
      </beans>
      
    • mybatis-config.xml
      <configuration>
          <mappers>
              <mapper resource="mapper/UserMapper.xml"/>
              <mapper resource="mapper/ProductMapper.xml"/>
              <mapper resource="mapper/CartItemMapper.xml"/>
              <mapper resource="mapper/OrderMapper.xml"/>
              <mapper resource="mapper/OrderDetailMapper.xml"/>
          </mappers>
      </configuration>
      

实体类

User.java
package com.electronics.entity;

public class User {
    private int id;
    private String username;
    private String password;
    private String email;
    private String phone;

    // Getters and Setters
}
Product.java
package com.electronics.entity;

public class Product {
    private int id;
    private String name;
    private String description;
    private double price;
    private int stock;
    private String category;
    private String imageUrl;

    // Getters and Setters
}
CartItem.java
package com.electronics.entity;

public class CartItem {
    private int id;
    private int userId;
    private int productId;
    private int quantity;

    // Getters and Setters
}
Order.java
package com.electronics.entity;

import java.util.Date;

public class Order {
    private int id;
    private int userId;
    private Date orderDate;
    private double totalPrice;
    private String status;

    // Getters and Setters
}
OrderDetail.java
package com.electronics.entity;

public class OrderDetail {
    private int id;
    private int orderId;
    private int productId;
    private int quantity;
    private double price;

    // Getters and Setters
}

DAO 层

UserMapper.java
package com.electronics.mapper;

import com.electronics.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM users WHERE username = #{username} AND password = #{password}")
    User login(@Param("username") String username, @Param("password") String password);

    @Insert("INSERT INTO users(username, password, email, phone) VALUES(#{username}, #{password}, #{email}, #{phone})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void register(User user);
}
ProductMapper.java
package com.electronics.mapper;

import com.electronics.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {
    @Select("SELECT * FROM products")
    List<Product> getAllProducts();

    @Select("SELECT * FROM products WHERE id = #{id}")
    Product getProductById(int id);

    @Insert("INSERT INTO products(name, description, price, stock, category, image_url) VALUES(#{name}, #{description}, #{price}, #{stock}, #{category}, #{imageUrl})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addProduct(Product product);

    @Update("UPDATE products SET name=#{name}, description=#{description}, price=#{price}, stock=#{stock}, category=#{category}, image_url=#{imageUrl} WHERE id=#{id}")
    void updateProduct(Product product);

    @Delete("DELETE FROM products WHERE id=#{id}")
    void deleteProduct(int id);
}
CartItemMapper.java
package com.electronics.mapper;

import com.electronics.entity.CartItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CartItemMapper {
    @Select("SELECT * FROM cart_items WHERE user_id = #{userId}")
    List<CartItem> getCartItemsByUserId(int userId);

    @Insert("INSERT INTO cart_items(user_id, product_id, quantity) VALUES(#{userId}, #{productId}, #{quantity})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addToCart(CartItem cartItem);

    @Update("UPDATE cart_items SET quantity=#{quantity} WHERE id=#{id}")
    void updateCartItem(CartItem cartItem);

    @Delete("DELETE FROM cart_items WHERE id=#{id}")
    void deleteCartItem(int id);
}
OrderMapper.java
package com.electronics.mapper;

import com.electronics.entity.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {
    @Select("SELECT * FROM orders WHERE user_id = #{userId}")
    List<Order> getOrdersByUserId(int userId);

    @Insert("INSERT INTO orders(user_id, order_date, total_price, status) VALUES(#{userId}, #{orderDate}, #{totalPrice}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addOrder(Order order);

    @Update("UPDATE orders SET order_date=#{orderDate}, total_price=#{totalPrice}, status=#{status} WHERE id=#{id}")
    void updateOrder(Order order);

    @Delete("DELETE FROM orders WHERE id=#{id}")
    void deleteOrder(int id);
}
OrderDetailMapper.java
package com.electronics.mapper;

import com.electronics.entity.OrderDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderDetailMapper {
    @Select("SELECT * FROM order_details WHERE order_id = #{orderId}")
    List<OrderDetail> getOrderDetailsByOrderId(int orderId);

    @Insert("INSERT INTO order_details(order_id, product_id, quantity, price) VALUES(#{orderId}, #{productId}, #{quantity}, #{price})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addOrderDetail(OrderDetail orderDetail);
}

Service 层

UserService.java
package com.electronics.service;

import com.electronics.entity.User;
import com.electronics.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

    public void register(User user) {
        userMapper.register(user);
    }
}
ProductService.java
package com.electronics.service;

import com.electronics.entity.Product;
import com.electronics.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    @Autowired
    private ProductMapper productMapper;

    public List<Product> getAllProducts() {
        return productMapper.getAllProducts();
    }

    public Product getProductById(int id) {
        return productMapper.getProductById(id);
    }

    public void addProduct(Product product) {
        productMapper.addProduct(product);
    }

    public void updateProduct(Product product) {
        productMapper.updateProduct(product);
    }

    public void deleteProduct(int id) {
        productMapper.deleteProduct(id);
    }
}
CartService.java
package com.electronics.service;

import com.electronics.entity.CartItem;
import com.electronics.mapper.CartItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartService {
    @Autowired
    private CartItemMapper cartItemMapper;

    public List<CartItem> getCartItemsByUserId(int userId) {
        return cartItemMapper.getCartItemsByUserId(userId);
    }

    public void addToCart(CartItem cartItem) {
        cartItemMapper.addToCart(cartItem);
    }

    public void updateCartItem(CartItem cartItem) {
        cartItemMapper.updateCartItem(cartItem);
    }

    public void deleteCartItem(int id) {
        cartItemMapper.deleteCartItem(id);
    }
}
OrderService.java
package com.electronics.service;

import com.electronics.entity.Order;
import com.electronics.entity.OrderDetail;
import com.electronics.mapper.OrderDetailMapper;
import com.electronics.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    public List<Order> getOrdersByUserId(int userId) {
        return orderMapper.getOrdersByUserId(userId);
    }

    public void addOrder(Order order) {
        orderMapper.addOrder(order);
        for (OrderDetail detail : order.getOrderDetails()) {
            orderDetailMapper.addOrderDetail(detail);
        }
    }

    public void updateOrder(Order order) {
        orderMapper.updateOrder(order);
    }

    public void deleteOrder(int id) {
        orderMapper.deleteOrder(id);
    }
}

Controller 层

UserController.java
package com.electronics.controller;

import com.electronics.entity.User;
import com.electronics.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String showLoginForm() {
        return "login";
    }

    @PostMapping("/login")
    public String handleLogin(@RequestParam("username") String username, @RequestParam("password") String password, Model model) {
        User user = userService.login(username, password);
        if (user != null) {
            model.addAttribute("user", user);
            return "redirect:/products";
        } else {
            model.addAttribute("error", "Invalid username or password");
            return "login";
        }
    }

    @GetMapping("/register")
    public String showRegisterForm() {
        return "register";
    }

    @PostMapping("/register")
    public String handleRegister(User user) {
        userService.register(user);
        return "redirect:/login";
    }
}
ProductController.java
package com.electronics.controller;

import com.electronics.entity.Product;
import com.electronics.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/products")
    public String showProducts(Model model) {
        List<Product> products = productService.getAllProducts();
        model.addAttribute("products", products);
        return "products";
    }

    @GetMapping("/product/{id}")
    public String showProductDetails(@RequestParam("id") int id, Model model) {
        Product product = productService.getProductById(id);
        model.addAttribute("product", product);
        return "productDetails";
    }

    @GetMapping("/addProduct")
    public String showAddProductForm() {
        return "addProduct";
    }

    @PostMapping("/addProduct")
    public String handleAddProduct(Product product) {
        productService.addProduct(product);
        return "redirect:/products";
    }

    @GetMapping("/editProduct/{id}")
    public String showEditProductForm(@RequestParam("id") int id, Model model) {
        Product product = productService.getProductById(id);
        model.addAttribute("product", product);
        return "editProduct";
    }

    @PostMapping("/editProduct")
    public String handleEditProduct(Product product) {
        productService.updateProduct(product);
        return "redirect:/products";
    }

    @GetMapping("/deleteProduct/{id}")
    public String handleDeleteProduct(@RequestParam("id") int id) {
        productService.deleteProduct(id);
        return "redirect:/products";
    }
}
CartController.java
package com.electronics.controller;

import com.electronics.entity.CartItem;
import com.electronics.entity.Product;
import com.electronics.service.CartService;
import com.electronics.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class CartController {
    @Autowired
    private CartService cartService;

    @Autowired
    private ProductService productService;

    @GetMapping("/cart")
    public String showCart(@RequestParam("userId") int userId, Model model) {
        List<CartItem> cartItems = cartService.getCartItemsByUserId(userId);
        model.addAttribute("cartItems", cartItems);
        return "cart";
    }

    @PostMapping("/addToCart")
    public String handleAddToCart(@RequestParam("userId") int userId, @RequestParam("productId") int productId, @RequestParam("quantity") int quantity) {
        Product product = productService.getProductById(productId);
        CartItem cartItem = new CartItem();
        cartItem.setUserId(userId);
        cartItem.setProductId(productId);
        cartItem.setQuantity(quantity);
        cartService.addToCart(cartItem);
        return "redirect:/cart?userId=" + userId;
    }

    @PostMapping("/updateCartItem")
    public String handleUpdateCartItem(@RequestParam("id") int id, @RequestParam("quantity") int quantity) {
        CartItem cartItem = new CartItem();
        cartItem.setId(id);
        cartItem.setQuantity(quantity);
        cartService.updateCartItem(cartItem);
        return "redirect:/cart?userId=" + cartItem.getUserId();
    }

    @GetMapping("/removeFromCart/{id}")
    public String handleRemoveFromCart(@RequestParam("id") int id, @RequestParam("userId") int userId) {
        cartService.deleteCartItem(id);
        return "redirect:/cart?userId=" + userId;
    }
}
OrderController.java
package com.electronics.controller;

import com.electronics.entity.Order;
import com.electronics.entity.OrderDetail;
import com.electronics.entity.Product;
import com.electronics.service.OrderService;
import com.electronics.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductService productService;

    @GetMapping("/orders")
    public String showOrders(@RequestParam("userId") int userId, Model model) {
        List<Order> orders = orderService.getOrdersByUserId(userId);
        model.addAttribute("orders", orders);
        return "orders";
    }

    @PostMapping("/placeOrder")
    public String handlePlaceOrder(@RequestParam("userId") int userId, @RequestParam("cartItemIds") String cartItemIds) {
        Order order = new Order();
        order.setUserId(userId);
        order.setOrderDate(new Date());
        order.setTotalPrice(0.0);
        order.setStatus("Pending");

        List<OrderDetail> orderDetails = new ArrayList<>();
        String[] ids = cartItemIds.split(",");
        for (String id : ids) {
            int cartItemId = Integer.parseInt(id);
            CartItem cartItem = cartService.getCartItemById(cartItemId);
            Product product = productService.getProductById(cartItem.getProductId());

            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setProductId(cartItem.getProductId());
            orderDetail.setQuantity(cartItem.getQuantity());
            orderDetail.setPrice(product.getPrice());

            orderDetails.add(orderDetail);
            order.setTotalPrice(order.getTotalPrice() + product.getPrice() * cartItem.getQuantity());
        }

        order.setOrderDetails(orderDetails);
        orderService.addOrder(order);

        // 清空购物车
        for (OrderDetail detail : orderDetails) {
            cartService.deleteCartItem(detail.getId());
        }

        return "redirect:/orders?userId=" + userId;
    }
}

前端页面

使用 JSP 创建前端页面。以下是简单的 JSP 示例:

login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="${pageContext.request.contextPath}/login" method="post">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br>
    <input type="submit" value="Login">
</form>
<c:if test="${not empty error}">
    <p style="color: red">${error}</p>
</c:if>
</body>
</html>
products.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Products</title>
</head>
<body>
<h2>Products</h2>
<table>
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>Price</th>
        <th>Stock</th>
        <th>Category</th>
        <th>Action</th>
    </tr>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.name}</td>
            <td>${product.description}</td>
            <td>${product.price}</td>
            <td>${product.stock}</td>
            <td>${product.category}</td>
            <td>
                <a href="${pageContext.request.contextPath}/product/${product.id}">View</a>
                <a href="${pageContext.request.contextPath}/addToCart?userId=${user.id}&productId=${product.id}&quantity=1">Add to Cart</a>
            </td>
        </tr>
    </c:forEach>
</table>
<a href="${pageContext.request.contextPath}/addProduct">Add New Product</a>
</body>
</html>
cart.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Cart</title>
</head>
<body>
<h2>Cart</h2>
<table>
    <tr>
        <th>Product Name</th>
        <th>Quantity</th>
        <th>Price</th>
        <th>Action</th>
    </tr>
    <c:forEach items="${cartItems}" var="cartItem">
        <tr>
            <td>${cartItem.product.name}</td>
            <td>${cartItem.quantity}</td>
            <td>${cartItem.product.price}</td>
            <td>
                <a href="${pageContext.request.contextPath}/removeFromCart?id=${cartItem.id}&userId=${user.id}">Remove</a>
            </td>
        </tr>
    </c:forEach>
</table>
<a href="${pageContext.request.contextPath}/placeOrder?userId=${user.id}&cartItemIds=${cartItemIds}">Place Order</a>
</body>
</html>

测试与调试

对每个功能进行详细测试,确保所有功能都能正常工作。

部署与发布

编译最终版本的应用程序,并准备好 WAR 文件供 Tomcat 或其他应用服务器部署。


http://www.kler.cn/a/386164.html

相关文章:

  • 使用 VS Code 远程连接时解决 OpenSSL 版本不匹配及权限问题
  • 容器技术在持续集成与持续交付中的应用
  • 检测敏感词功能
  • 【Rust 编程语言工具】rustup-init.exe 安装与使用指南
  • 自动驾驶系列—从数据采集到存储:解密自动驾驶传感器数据采集盒子的关键技术
  • 删库跑路,启动!
  • Unity性能优化-具体操作
  • 从技术创新到商业应用,智象未来(HiDream.ai)创新不止步
  • web 渗透学习指南——初学者防入狱篇
  • java:修复aspectj-maven-plugin插件在java9项目中执行报错:cannot be resolved to a module
  • QML项目实战:自定义Button
  • Spring面试题之事务的传播行为
  • electron 中 ipcRendererEvent 作用
  • 面试总结!
  • spark的学习-03
  • YOLOv8相较于YOLOv5有哪些改进?
  • 滑动窗口最大值
  • 《手写Spring渐进式源码实践》实践笔记(第十七章 数据类型转换)
  • 布朗运动
  • 如何用全局事件总线实现组件间的通信
  • STM32标准库-待机模式
  • 数据集市是什么?有什么优势?
  • OpenGL学习笔记(三) 绘制图形
  • RabbitMQ 篇-深入了解 RabbitMQ 安装以及 SpringAMQP 的基础使用(声明队列和交换机、发送接收消息、配置 JSON 消息转化器)
  • 【Linux系列】利用 CURL 发送 POST 请求
  • Android13 系统/用户证书安装相关分析总结(二) 如何增加一个安装系统证书的接口