佛山門戶網(wǎng)站建設(shè)廣告優(yōu)化師怎么學(xué)
基于SSM(Spring + Spring MVC + MyBatis)框架的咖啡館管理系統(tǒng)是一個(gè)綜合性的Web應(yīng)用程序,用于管理和優(yōu)化咖啡館的運(yùn)營(yíng)。下面我將提供一個(gè)詳細(xì)的案例程序概述,包括主要的功能模塊和技術(shù)棧介紹。
項(xiàng)目概述
功能需求
- 用戶管理:管理員可以添加、刪除、修改和查詢用戶信息。
- 員工管理:記錄員工信息,如姓名、職位、工資等。
- 菜單管理:支持對(duì)菜單項(xiàng)的增刪改查操作,包括菜品名稱、價(jià)格、類別等。
- 訂單管理:處理訂單信息,記錄訂單詳情,包括下單時(shí)間、顧客信息、訂單狀態(tài)等。
- 庫(kù)存管理:記錄原材料庫(kù)存,當(dāng)庫(kù)存低于預(yù)設(shè)值時(shí)發(fā)出警告。
- 財(cái)務(wù)報(bào)表:生成各類報(bào)表,如收入報(bào)表、支出報(bào)表等。
- 權(quán)限管理:不同用戶有不同的操作權(quán)限。
- 顧客反饋:記錄顧客的反饋信息,用于改進(jìn)服務(wù)質(zhì)量。
技術(shù)棧
- 前端:HTML, CSS, JavaScript, JSP(或Thymeleaf等模板引擎)
- 后端:
- 框架:Spring, Spring MVC, MyBatis
- 數(shù)據(jù)庫(kù):MySQL
- 服務(wù)器:Tomcat
- 工具:Maven(項(xiàng)目構(gòu)建和依賴管理)
項(xiàng)目結(jié)構(gòu)
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
關(guān)鍵技術(shù)點(diǎn)
- Spring配置:使用
spring-context
和spring-webmvc
進(jìn)行IoC容器和Web應(yīng)用配置。 - MyBatis配置:配置數(shù)據(jù)源、事務(wù)管理器以及映射文件路徑。
- 數(shù)據(jù)訪問層:通過MyBatis的Mapper接口實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的操作。
- 服務(wù)層:處理業(yè)務(wù)邏輯,調(diào)用DAO層完成數(shù)據(jù)操作。
- 控制層:處理前端請(qǐng)求,調(diào)用服務(wù)層并返回響應(yīng)結(jié)果給前端。
- 頁(yè)面展示:使用JSP或Thymeleaf等技術(shù)實(shí)現(xiàn)前后端交互。
示例代碼片段
MyBatis Mapper 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 類
// 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 接口
// 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 層
// src/main/java/com/example/coffeeshop/service/MenuItemService.java
@Service
public class MenuItemService {@Autowiredprivate 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 層
// src/main/java/com/example/coffeeshop/controller/MenuItemController.java
@Controller
@RequestMapping("/menu")
public class MenuItemController {@Autowiredprivate 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/";}
}
數(shù)據(jù)庫(kù)表設(shè)計(jì)
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
);
運(yùn)行項(xiàng)目
- 數(shù)據(jù)庫(kù)初始化:運(yùn)行上述SQL腳本創(chuàng)建數(shù)據(jù)庫(kù)表。
- 配置文件:在
src/main/resources
目錄下配置applicationContext.xml
、spring-mvc.xml
和mybatis-config.xml
。 - 啟動(dòng)服務(wù)器:使用Tomcat服務(wù)器啟動(dòng)項(xiàng)目。
示例配置文件
applicationContext.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://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
<!-- 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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://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>