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

server/2025/1/12 9:08:12/

一个基于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/server/157722.html

相关文章

vue入门项目

vue入门项目 1、创建前端脚手架2、安装依赖&#xff1a;3、启动项目4、整合技术5、画面布局&#xff1a;参照rco-design6、配置vue-router 路由跳转7、整合echarts渲染表8、完善细节问题 1、创建前端脚手架 node -v --查看node版本 npm create vite2、安装依赖&#xff1a; …

Ubuntu问题 - 服务器有两个网卡, 且都可以上互联网, 但是希望设置优先级,优先使用某个网卡, 没有网络再切换到另一个网卡 (已实操成功)

需求: 操作系统: Ubuntu22.04两个可以联网的网卡, 且都连接到互联网上了, 希望根据优先级实现自动切换网卡上网以 root登录 或者使用 sudo 命令 开始 步骤 1&#xff1a;查看当前的网络连接 nmcli connection showNAME 是连接的名称&#xff08;如 Wired connection 1 或 有…

计算机网络 笔记 数据链路层 2

1,信道划分&#xff1a; (1)时分复用TDM 将时间等分为“TDM帧”&#xff0c;每个TDM帧内部等分为m个时隙&#xff0c;m个用户对应m个时隙 缺点&#xff1a;每个节点只分到了总带宽的1/m,如果有部分的1节点不发出数据&#xff0c;那么就会在这个时间信道被闲置&#xff0c;利用…

校园资料分享微信小程序”的设计与实现springboot+论文源码调试讲解

第4章 系统设计 用户对着浏览器操作&#xff0c;肯定会出现某些不可预料的问题&#xff0c;但是不代表着系统对于用户在浏览器上的操作不进行处理&#xff0c;所以说&#xff0c;要提前考虑可能会出现的问题。 4.1 系统设计思想 系统设计&#xff0c;肯定要把设计的思想进行统…

Oracle 表分区简介

目录 一. 前置知识1.1 什么是表分区1.2 表分区的优势1.3 表分区的使用条件 二. 表分区的方法2.1 范围分区&#xff08;Range Partitioning&#xff09;2.2 列表分区&#xff08;List Partitioning&#xff09;2.3 哈希分区&#xff08;Hash Partitioning&#xff09;2.4 复合分…

Decord - 深度学习视频加载器

文章目录 一、关于 Decord初步基准 二、安装1、通过pip安装2、从源代码安装2.1 Linux2.2 macOS2.3 Windows 三、用法1、VideoReader2、VideoLoader3、AudioReader4、AVReader 四、深度学习框架的桥梁&#xff1a; 一、关于 Decord 一款高效的深度学习视频加载器&#xff0c;具…

科大讯飞前端面试题及参考答案 (下)

除了 echarts 还了解其它画图工具吗? 除了 Echarts,还有不少优秀的画图工具可供选择使用。 Highcharts:它是一款功能强大且应用广泛的图表绘制工具,支持多种常见的图表类型,像柱状图、折线图、饼图、散点图等,同时也能创建较为复杂的图表,比如仪表盘图表、极坐标图等。H…

Docker与GitHub的完美结合:6种实用方法

在现代软件开发中,Docker和GitHub已经成为不可或缺的工具。Docker提供了一致的环境封装和部署方案,而GitHub则是代码托管和版本控制的首选平台。将这两个强大的工具结合使用,可以大大提高开发效率,简化部署流程,并确保开发和生产环境的一致性。本文将介绍6种实用方法,帮助…