首页 > 编程语言 >基于 JAVASSM 框架沙县小吃点餐系统

基于 JAVASSM 框架沙县小吃点餐系统

时间:2024-11-03 12:48:59浏览次数:4  
标签:String id JAVASSM 沙县 import dish 点餐 com public

基于 JAVASSM 框架(即 Java + Spring + Spring MVC + MyBatis)开发一个沙县小吃点餐系统。
在这里插入图片描述

步骤一:需求分析

明确系统需要实现的功能,比如:

  • 用户注册和登录
  • 浏览菜单
  • 添加菜品到购物车
  • 下单并支付
  • 订单管理
  • 后台管理(菜品管理、订单管理等)

步骤二:设计数据库

使用 MySQL 数据库存储系统数据。设计数据库表结构如下:

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

步骤三:选择开发工具

使用 IntelliJ IDEA 或 Eclipse 作为开发环境。

步骤四:搭建项目结构

  1. 创建 Maven 项目。
  2. 添加必要的依赖项(Spring、Spring MVC、MyBatis、MySQL 驱动等)。

步骤五:配置文件

application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/sandwich_shop?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.sandwichshop"/>
    <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/DishMapper.xml"/>
        <!-- 其他 Mapper 文件 -->
    </mappers>
</configuration>

步骤六:编写实体类

User.java
package com.sandwichshop.entity;

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

    // Getters and Setters
}
Dish.java
package com.sandwichshop.entity;

public class Dish {
    private int id;
    private String name;
    private double price;
    private String description;
    private String imageUrl;

    // Getters and Setters
}

步骤七:编写 DAO 层

UserMapper.java
package com.sandwichshop.mapper;

import com.sandwichshop.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);
}
DishMapper.java
package com.sandwichshop.mapper;

import com.sandwichshop.entity.Dish;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishMapper {
    @Select("SELECT * FROM dishes")
    List<Dish> getAllDishes();

    @Select("SELECT * FROM dishes WHERE id = #{id}")
    Dish getDishById(int id);

    @Insert("INSERT INTO dishes(name, price, description, image_url) VALUES(#{name}, #{price}, #{description}, #{imageUrl})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addDish(Dish dish);

    @Update("UPDATE dishes SET name=#{name}, price=#{price}, description=#{description}, image_url=#{imageUrl} WHERE id=#{id}")
    void updateDish(Dish dish);

    @Delete("DELETE FROM dishes WHERE id=#{id}")
    void deleteDish(int id);
}

步骤八:编写 Service 层

UserService.java
package com.sandwichshop.service;

import com.sandwichshop.entity.User;
import com.sandwichshop.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);
    }
}
DishService.java
package com.sandwichshop.service;

import com.sandwichshop.entity.Dish;
import com.sandwichshop.mapper.DishMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishService {
    @Autowired
    private DishMapper dishMapper;

    public List<Dish> getAllDishes() {
        return dishMapper.getAllDishes();
    }

    public Dish getDishById(int id) {
        return dishMapper.getDishById(id);
    }

    public void addDish(Dish dish) {
        dishMapper.addDish(dish);
    }

    public void updateDish(Dish dish) {
        dishMapper.updateDish(dish);
    }

    public void deleteDish(int id) {
        dishMapper.deleteDish(id);
    }
}

步骤九:编写 Controller 层

UserController.java
package com.sandwichshop.controller;

import com.sandwichshop.entity.User;
import com.sandwichshop.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 "home";
        } 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";
    }
}
DishController.java
package com.sandwichshop.controller;

import com.sandwichshop.entity.Dish;
import com.sandwichshop.service.DishService;
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 DishController {
    @Autowired
    private DishService dishService;

    @GetMapping("/menu")
    public String showMenu(Model model) {
        List<Dish> dishes = dishService.getAllDishes();
        model.addAttribute("dishes", dishes);
        return "menu";
    }

    @GetMapping("/dish/{id}")
    public String showDishDetails(@RequestParam("id") int id, Model model) {
        Dish dish = dishService.getDishById(id);
        model.addAttribute("dish", dish);
        return "dishDetails";
    }

    @GetMapping("/addDish")
    public String showAddDishForm() {
        return "addDish";
    }

    @PostMapping("/addDish")
    public String handleAddDish(Dish dish) {
        dishService.addDish(dish);
        return "redirect:/menu";
    }

    @GetMapping("/editDish/{id}")
    public String showEditDishForm(@RequestParam("id") int id, Model model) {
        Dish dish = dishService.getDishById(id);
        model.addAttribute("dish", dish);
        return "editDish";
    }

    @PostMapping("/editDish")
    public String handleEditDish(Dish dish) {
        dishService.updateDish(dish);
        return "redirect:/menu";
    }

    @GetMapping("/deleteDish/{id}")
    public String handleDeleteDish(@RequestParam("id") int id) {
        dishService.deleteDish(id);
        return "redirect:/menu";
    }
}

步骤十:前端页面

使用 JSP 或 Thymeleaf 创建前端页面。以下是简单的 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>
menu.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Menu</title>
</head>
<body>
<h2>Menu</h2>
<table>
    <tr>
        <th>Name</th>
        <th>Price</th>
        <th>Description</th>
        <th>Image</th>
    </tr>
    <c:forEach items="${dishes}" var="dish">
        <tr>
            <td>${dish.name}</td>
            <td>${dish.price}</td>
            <td>${dish.description}</td>
            <td><img src="${dish.imageUrl}" alt="${dish.name}" width="100"></td>
        </tr>
    </c:forEach>
</table>
<a href="${pageContext.request.contextPath}/addDish">Add New Dish</a>
</body>
</html>

步骤十一:测试与调试

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

步骤十二:部署与发布

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

标签:String,id,JAVASSM,沙县,import,dish,点餐,com,public
From: https://blog.csdn.net/m0_52011717/article/details/143464118

相关文章