基于SSM(Spring + Spring MVC + MyBatis)框架的咖啡馆管理系统

基于SSM(Spring + Spring MVC + MyBatis)框架的咖啡馆管理系统是一个综合性的Web应用程序,用于管理和优化咖啡馆的运营。下面我将提供一个详细的案例程序概述,包括主要的功能模块和技术栈介绍。

项目概述

功能需求
  1. 用户管理:管理员可以添加、删除、修改和查询用户信息。
  2. 员工管理:记录员工信息,如姓名、职位、工资等。
  3. 菜单管理:支持对菜单项的增删改查操作,包括菜品名称、价格、类别等。
  4. 订单管理:处理订单信息,记录订单详情,包括下单时间、顾客信息、订单状态等。
  5. 库存管理:记录原材料库存,当库存低于预设值时发出警告。
  6. 财务报表:生成各类报表,如收入报表、支出报表等。
  7. 权限管理:不同用户有不同的操作权限。
  8. 顾客反馈:记录顾客的反馈信息,用于改进服务质量。
技术栈
  • 前端:HTML, CSS, JavaScript, JSP(或Thymeleaf等模板引擎)
  • 后端
    • 框架:Spring, Spring MVC, MyBatis
    • 数据库:MySQL
    • 服务器:Tomcat
  • 工具:Maven(项目构建和依赖管理)

项目结构

复制代码
CoffeeShopManagementSystem
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com.example.coffeeshop
│   │   │       ├── controller
│   │   │       ├── service
│   │   │       ├── dao
│   │   │       └── entity
│   │   ├── resources
│   │   │   ├── mapper
│   │   │   ├── spring
│   │   │   └── mybatis-config.xml
│   │   └── webapp
│   │       ├── WEB-INF
│   │       │   └── web.xml
│   │       └── index.jsp
│   └── test
│       └── java
│           └── com.example.coffeeshop
└── pom.xml

关键技术点

  • Spring配置 :使用spring-contextspring-webmvc进行IoC容器和Web应用配置。
  • MyBatis配置:配置数据源、事务管理器以及映射文件路径。
  • 数据访问层:通过MyBatis的Mapper接口实现对数据库的操作。
  • 服务层:处理业务逻辑,调用DAO层完成数据操作。
  • 控制层:处理前端请求,调用服务层并返回响应结果给前端。
  • 页面展示:使用JSP或Thymeleaf等技术实现前后端交互。

示例代码片段

MyBatis Mapper XML
xml 复制代码
<!-- src/main/resources/mapper/MenuItemMapper.xml -->
<mapper namespace="com.example.coffeeshop.dao.MenuItemDao">
    <select id="getMenuItemById" resultType="com.example.coffeeshop.entity.MenuItem">
        SELECT * FROM menu_item WHERE id = #{id}
    </select>
</mapper>
Entity 类
java 复制代码
// src/main/java/com/example/coffeeshop/entity/MenuItem.java
public class MenuItem {
    private int id;
    private String name;
    private String category;
    private double price;
    // Getters and Setters
}
DAO 接口
java 复制代码
// src/main/java/com/example/coffeeshop/dao/MenuItemDao.java
public interface MenuItemDao {
    MenuItem getMenuItemById(int id);
    List<MenuItem> getAllMenuItems();
    void addMenuItem(MenuItem menuItem);
    void updateMenuItem(MenuItem menuItem);
    void deleteMenuItem(int id);
}
Service 层
java 复制代码
// src/main/java/com/example/coffeeshop/service/MenuItemService.java
@Service
public class MenuItemService {
    @Autowired
    private MenuItemDao menuItemDao;

    public MenuItem getMenuItemById(int id) {
        return menuItemDao.getMenuItemById(id);
    }

    public List<MenuItem> getAllMenuItems() {
        return menuItemDao.getAllMenuItems();
    }

    public void addMenuItem(MenuItem menuItem) {
        menuItemDao.addMenuItem(menuItem);
    }

    public void updateMenuItem(MenuItem menuItem) {
        menuItemDao.updateMenuItem(menuItem);
    }

    public void deleteMenuItem(int id) {
        menuItemDao.deleteMenuItem(id);
    }
}
Controller 层
java 复制代码
// src/main/java/com/example/coffeeshop/controller/MenuItemController.java
@Controller
@RequestMapping("/menu")
public class MenuItemController {
    @Autowired
    private MenuItemService menuItemService;

    @GetMapping("/{id}")
    public String getMenuItemById(@PathVariable int id, Model model) {
        MenuItem menuItem = menuItemService.getMenuItemById(id);
        model.addAttribute("menuItem", menuItem);
        return "menuItemDetail";
    }

    @GetMapping("/")
    public String getAllMenuItems(Model model) {
        List<MenuItem> menuItems = menuItemService.getAllMenuItems();
        model.addAttribute("menuItems", menuItems);
        return "menuItemList";
    }

    @PostMapping("/")
    public String addMenuItem(@ModelAttribute MenuItem menuItem) {
        menuItemService.addMenuItem(menuItem);
        return "redirect:/menu/";
    }

    @PutMapping("/{id}")
    public String updateMenuItem(@PathVariable int id, @ModelAttribute MenuItem menuItem) {
        menuItem.setId(id);
        menuItemService.updateMenuItem(menuItem);
        return "redirect:/menu/";
    }

    @DeleteMapping("/{id}")
    public String deleteMenuItem(@PathVariable int id) {
        menuItemService.deleteMenuItem(id);
        return "redirect:/menu/";
    }
}

数据库表设计

sql 复制代码
CREATE TABLE user (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(50) NOT NULL,
    role VARCHAR(20) NOT NULL
);

CREATE TABLE employee (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    position VARCHAR(50) NOT NULL,
    salary DOUBLE NOT NULL
);

CREATE TABLE menu_item (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    category VARCHAR(50) NOT NULL,
    price DOUBLE NOT NULL
);

CREATE TABLE order (
    id INT AUTO_INCREMENT PRIMARY KEY,
    customer_name VARCHAR(50) NOT NULL,
    order_time DATETIME NOT NULL,
    total_price DOUBLE NOT NULL,
    status VARCHAR(20) NOT NULL
);

CREATE TABLE order_item (
    id INT AUTO_INCREMENT PRIMARY KEY,
    order_id INT NOT NULL,
    menu_item_id INT NOT NULL,
    quantity INT NOT NULL,
    FOREIGN KEY (order_id) REFERENCES order(id),
    FOREIGN KEY (menu_item_id) REFERENCES menu_item(id)
);

CREATE TABLE inventory (
    id INT AUTO_INCREMENT PRIMARY KEY,
    item_name VARCHAR(50) NOT NULL,
    quantity INT NOT NULL,
    threshold INT NOT NULL
);

CREATE TABLE feedback (
    id INT AUTO_INCREMENT PRIMARY KEY,
    customer_name VARCHAR(50) NOT NULL,
    content TEXT NOT NULL,
    feedback_time DATETIME NOT NULL
);

运行项目

  1. 数据库初始化:运行上述SQL脚本创建数据库表。
  2. 配置文件 :在src/main/resources目录下配置applicationContext.xmlspring-mvc.xmlmybatis-config.xml
  3. 启动服务器:使用Tomcat服务器启动项目。

示例配置文件

applicationContext.xml
xml 复制代码
<!-- src/main/resources/spring/applicationContext.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:tx="http://www.springframework.org/schema/tx"
       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/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.example.coffeeshop" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/coffeeshop?useSSL=false&serverTimezone=UTC" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.example.coffeeshop.dao" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>
spring-mvc.xml
xml 复制代码
<!-- src/main/resources/spring/spring-mvc.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.example.coffeeshop" />

    <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>
相关推荐
阿湯哥2 小时前
Spring AI Alibaba 实现 Workflow 全指南
java·人工智能·spring
萧曵 丶4 小时前
Spring Cloud Alibaba 详解
spring·spring cloud
szm02255 小时前
Spring
java·后端·spring
萧曵 丶6 小时前
Spring 全套高频面试题(由浅到深 完整版)
java·后端·spring
stillaliveQEJ6 小时前
【MyBatis】resultType踩坑实录
mybatis
雨中飘荡的记忆8 小时前
Spring Security入门:构建安全应用
spring
BD_Marathon8 小时前
MyBatis处理动态设置表名
mybatis
老毛肚9 小时前
Spring源码探究2.0
java·后端·spring
涵涵(互关)9 小时前
添加了 @TableId(type = IdType.AUTO) 但仍生成超大 ID
数据库·spring·mybatis
运维@小兵9 小时前
Spring AI入门
java·人工智能·spring