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

devtools/2025/1/11 19:48:00/

一个基于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/devtools/149687.html

相关文章

香港云服务器的ip可以更换的吗?

香港云服务器的IP是否可以更换&#xff0c;通常取决于你所使用的云服务商。大多数云服务商都提供一定的灵活性&#xff0c;允许你更换IP地址。 如果你使用的是动态IP(一般用于家庭或小型企业的网络)&#xff0c;IP地址可能会在一定时间后自动变动。对于云服务器&#xff0c;通常…

Microsoft 已经弃用了 <experimental/filesystem> 头文件

#define _CRT_SECURE_NO_WARNINGS 1 #define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING 1 //Microsoft 已经弃用了 <experimental / filesystem> 头文件&#xff0c;并计划在将来移除它。取而代之的是 C17 标准引入的 //<filesystem> 头文件&#xf…

音频-扬声器和麦克风

首先&#xff0c;我们先介绍一下音频芯片&#xff1a;开发板上带有一个麦克风&#xff0c;一个扬声器&#xff0c;音频编解码芯片使用ES8311。麦克风直接连接到了ES8311芯片上&#xff0c;ES8311和扬声器之间&#xff0c;还有一个音频驱动放大器。ES8311通过I2S接口与ESP32-C3连…

[读书日志]从零开始学习Chisel 第十一篇:Scala的类型参数化(敏捷硬件开发语言Chisel与数字系统设计)

8.Scala的类型参数化 8.1 var类型的字段 对于可重新赋值的字段&#xff0c;可以执行两个基本操作&#xff0c;获取字段值或设置为一个新值。如果在类中定义了一个var类型的字段&#xff0c;那么编译器会把这个变量限制为private[this]&#xff0c;同时隐式地定义一个名为变量…

npm run 运行项目报错:Cannot resolve the ‘pnmp‘ package manager

尝试使用 npm 运行一个项目&#xff0c;但是在解析 pnmp 包管理器时遇到了问题。这通常意味着项目可能配置错误&#xff0c;或者可能误输入了命令。 解决方法&#xff1a; 确认是否有拼写错误。通常情况下&#xff0c;应该是 npm 而不是 pnmp。 检查项目的 package.json 文件&…

安装rocketmq dashboard

1、访问如下地址&#xff1a; GitHub - apache/rocketmq-dashboard: The state-of-the-art Dashboard of Apache RoccketMQ provides excellent monitoring capability. Various graphs and statistics of events, performance and system information of clients and applica…

从excel提取和过滤数据到echarts中绘制图

主页面 介绍 echarts的事例页面,导入数据比较麻烦,此项目从excel中提取数据(含过滤数据),以注入页面. 代码说明 所有的需要从excel中读取的参数,从代码中替换.需以{{data}} 包含在内使用绘制参数的解析代码参数可以解析出来所有参数数据配置上传文件后,可以选择列数据过滤条…

Win10微调大语言模型ChatGLM2-6B

在《Win10本地部署大语言模型ChatGLM2-6B-CSDN博客》基础上进行&#xff0c;官方文档在这里&#xff0c;参考了这篇文章 首先确保ChatGLM2-6B下的有ptuning AdvertiseGen下载地址1&#xff0c;地址2&#xff0c;文件中数据留几行 模型文件下载地址 &#xff08;注意&#xff1…