一个基于Spring Boot的简单网吧管理系统

embedded/2025/1/11 18:43:36/

一个基于Spring Boot的简单网吧管理系统的案例代码。这个系统包括用户管理、电脑管理、上机记录管理等功能。代码结构清晰,适合初学者学习和参考。
在这里插入图片描述

1. 项目结构

src/main/java/com/example/netbarmanagement├── controller│   ├── ComputerController.java│   ├── UserController.java│   └── RecordController.java├── model│   ├── Computer.java│   ├── User.java│   └── Record.java├── repository│   ├── ComputerRepository.java│   ├── UserRepository.java│   └── RecordRepository.java├── service│   ├── ComputerService.java│   ├── UserService.java│   └── RecordService.java└── NetbarManagementApplication.java

2. 依赖配置 (pom.xml)

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>

3. 实体类 (model 包)

Computer.java
package com.example.netbarmanagement.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class Computer {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private boolean isOccupied;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public boolean isOccupied() {return isOccupied;}public void setOccupied(boolean occupied) {isOccupied = occupied;}
}
User.java
package com.example.netbarmanagement.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String username;private String password;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
Record.java
package com.example.netbarmanagement.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;@Entity
public class Record {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private Long userId;private Long computerId;private LocalDateTime startTime;private LocalDateTime endTime;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public Long getUserId() {return userId;}public void setUserId(Long userId) {this.userId = userId;}public Long getComputerId() {return computerId;}public void setComputerId(Long computerId) {this.computerId = computerId;}public LocalDateTime getStartTime() {return startTime;}public void setStartTime(LocalDateTime startTime) {this.startTime = startTime;}public LocalDateTime getEndTime() {return endTime;}public void setEndTime(LocalDateTime endTime) {this.endTime = endTime;}
}

4. 仓库接口 (repository 包)

ComputerRepository.java
package com.example.netbarmanagement.repository;import com.example.netbarmanagement.model.Computer;
import org.springframework.data.jpa.repository.JpaRepository;public interface ComputerRepository extends JpaRepository<Computer, Long> {
}
UserRepository.java
package com.example.netbarmanagement.repository;import com.example.netbarmanagement.model.User;
import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<User, Long> {
}
RecordRepository.java
package com.example.netbarmanagement.repository;import com.example.netbarmanagement.model.Record;
import org.springframework.data.jpa.repository.JpaRepository;public interface RecordRepository extends JpaRepository<Record, Long> {
}

5. 服务层 (service 包)

ComputerService.java
package com.example.netbarmanagement.service;import com.example.netbarmanagement.model.Computer;
import com.example.netbarmanagement.repository.ComputerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class ComputerService {@Autowiredprivate ComputerRepository computerRepository;public List<Computer> getAllComputers() {return computerRepository.findAll();}public Computer getComputerById(Long id) {return computerRepository.findById(id).orElse(null);}public Computer saveComputer(Computer computer) {return computerRepository.save(computer);}public void deleteComputer(Long id) {computerRepository.deleteById(id);}
}
UserService.java
package com.example.netbarmanagement.service;import com.example.netbarmanagement.model.User;
import com.example.netbarmanagement.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserRepository userRepository;public List<User> getAllUsers() {return userRepository.findAll();}public User getUserById(Long id) {return userRepository.findById(id).orElse(null);}public User saveUser(User user) {return userRepository.save(user);}public void deleteUser(Long id) {userRepository.deleteById(id);}
}
RecordService.java
package com.example.netbarmanagement.service;import com.example.netbarmanagement.model.Record;
import com.example.netbarmanagement.repository.RecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class RecordService {@Autowiredprivate RecordRepository recordRepository;public List<Record> getAllRecords() {return recordRepository.findAll();}public Record getRecordById(Long id) {return recordRepository.findById(id).orElse(null);}public Record saveRecord(Record record) {return recordRepository.save(record);}public void deleteRecord(Long id) {recordRepository.deleteById(id);}
}

6. 控制器层 (controller 包)

ComputerController.java
package com.example.netbarmanagement.controller;import com.example.netbarmanagement.model.Computer;
import com.example.netbarmanagement.service.ComputerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/computers")
public class ComputerController {@Autowiredprivate ComputerService computerService;@GetMappingpublic List<Computer> getAllComputers() {return computerService.getAllComputers();}@GetMapping("/{id}")public Computer getComputerById(@PathVariable Long id) {return computerService.getComputerById(id);}@PostMappingpublic Computer createComputer(@RequestBody Computer computer) {return computerService.saveComputer(computer);}@PutMapping("/{id}")public Computer updateComputer(@PathVariable Long id, @RequestBody Computer computer) {computer.setId(id);return computerService.saveComputer(computer);}@DeleteMapping("/{id}")public void deleteComputer(@PathVariable Long id) {computerService.deleteComputer(id);}
}
UserController.java
package com.example.netbarmanagement.controller;import com.example.netbarmanagement.model.User;
import com.example.netbarmanagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/users")
public class UserController {@Autowiredprivate UserService userService;@GetMappingpublic List<User> getAllUsers() {return userService.getAllUsers();}@GetMapping("/{id}")public User getUserById(@PathVariable Long id) {return userService.getUserById(id);}@PostMappingpublic User createUser(@RequestBody User user) {return userService.saveUser(user);}@PutMapping("/{id}")public User updateUser(@PathVariable Long id, @RequestBody User user) {user.setId(id);return userService.saveUser(user);}@DeleteMapping("/{id}")public void deleteUser(@PathVariable Long id) {userService.deleteUser(id);}
}
RecordController.java
package com.example.netbarmanagement.controller;import com.example.netbarmanagement.model.Record;
import com.example.netbarmanagement.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/records")
public class RecordController {@Autowiredprivate RecordService recordService;@GetMappingpublic List<Record> getAllRecords() {return recordService.getAllRecords();}@GetMapping("/{id}")public Record getRecordById(@PathVariable Long id) {return recordService.getRecordById(id);}@PostMappingpublic Record createRecord(@RequestBody Record record) {return recordService.saveRecord(record);}@PutMapping("/{id}")public Record updateRecord(@PathVariable Long id, @RequestBody Record record) {record.setId(id);return recordService.saveRecord(record);}@DeleteMapping("/{id}")public void deleteRecord(@PathVariable Long id) {recordService.deleteRecord(id);}
}

7. 主应用类 (NetbarManagementApplication.java)

package com.example.netbarmanagement;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class NetbarManagementApplication {public static void main(String[] args) {SpringApplication.run(NetbarManagementApplication.class, args);}
}

8. 配置文件 (application.properties)

spring.datasource.url=jdbc:h2:mem:netbar
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true

9. 运行项目

  1. 使用 mvn spring-boot:run 命令运行项目。
  2. 访问 http://localhost:8080/h2-console 查看H2数据库。
  3. 使用API工具(如Postman)测试各个接口。

10. 扩展功能

  • 添加用户登录和权限管理(使用Spring Security)。
  • 添加计费功能,根据上机时间计算费用。
  • 添加前端页面(使用Thymeleaf或Vue.js等前端框架)。

这个案例代码是一个基础的网吧管理系统,适合初学者学习和扩展。你可以根据实际需求进一步开发和优化。


http://www.ppmy.cn/embedded/153091.html

相关文章

Data/Code/Algorithm

前段时间很久没更在忙考研的事情&#xff0c;现在初试过了也算是一阶段的学习尘埃落定 有时间和心力和探索更多未知的世界和新奇的领域 我在小红书上刷到帖子推荐了几部计算机专业学生必看的纪录片 个人认为这是一个了解计算机新领域的好时机&#xff0c;所以最近陆陆续续也…

awr报告无法生成:常见案例与解决办法

awr报告无法生成:常见案例与解决办法 STATISTICS_LEVEL设置过低数据库打开状态不对主库隐含参数设置错误MMON子进程被SuspendSYS模式统计信息过期WRH$_SQL_PLAN表数据量太大AWR绑定变量信息收集超时撞上数据库Bug 9040676STATISTICS_LEVEL设置过低 STATISTICS_LEVEL设置为BAS…

React函数组件中与生命周期相关Hooks详解

React 函数组件及其钩子渲染流程是 React 框架中的核心概念之一。以下是对该流程的详细解析&#xff1a; 一、React 函数组件基础 定义&#xff1a; React 函数组件是一个接收 props 作为参数并返回 React 元素的函数。它们通常用于表示 UI 的一部分&#xff0c;并且不保留内部…

从零用java实现 小红书 springboot vue uniapp (8)个人资料修改 消息页优化

前言 移动端演示 http://8.146.211.120:8081/#/ 前面的文章我们主要完成了点赞关注 im 聊天功能 下面我们将完善个人资料修改 和消息页优化 向产品迈进 个人资料修改 自定义头像背景图 以及网名等等修改之后个人资料卡片也会随之改变 这样看起来就美观多了 修改文字资料的话很…

uni-app持久化登录简单实现

想要实现持久化登录&#xff0c;原理就是在每次进入应用的时候获取上一次用户登录的信息。 那么就好办了&#xff0c;我们在每次登录成功后把用户的账号密码存储到本地&#xff0c;然后在进入应用的时候读取本地文件获取账号密码重新执行登录流程&#xff0c;在退出登录的时候删…

【LeetCode】每日一题 2024_1_10 统计重新排列后包含另一个字符串的子字符串数目 II(滑动窗口)

前言 每天和你一起刷 LeetCode 每日一题~ 拼尽全力无法战胜期末考试 寒假 . . . 堂堂复活&#xff01; 每日一题重出江湖&#xff01; 就用我最擅长的滑动窗口类型的每日一题作为我寒假回归的第一题&#xff01; LeetCode 启动&#xff01; 题目&#xff1a;统计重新排列…

【ArcGIS微课1000例】0137:色彩映射表转为RGB全彩模式

本文讲述ArcGIS中,将tif格式的影像数据从色彩映射表转为RGB全彩模式。 参考阅读:【GlobalMapper精品教程】093:将tif影像色彩映射表(调色板)转为RGB全彩模式 文章目录 一、色彩映射表预览二、色彩映射表转为RGB全彩模式一、色彩映射表预览 加载配套数据包中的0137.rar中的…

Spring-Cloud-Gateway-Samples,nacos为注册中心,负载均衡

背景&#xff1a;本想找个简单例子看下&#xff0c;无奈版本依赖太过复杂&#xff0c;花了点时间。记录下吧 使用Spring Cloud Gateway作为网关服务&#xff0c;Nacos作为注册中心&#xff0c;实现对子服务的负载均衡访问。简单例子。 一、gateway-main-nacos服务端&#xff…