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

news/2025/2/11 0:49:02/

一个基于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 包)
java_63">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;}
}
java_107">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;}
}
java_151">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 包)
java_216">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> {
}
java_227">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> {
}
java_238">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 包)
java_251">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);}
}
java_285">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);}
}
java_319">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 包)
java_355">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);}
}
java_400">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);}
}
java_445">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);}
}
java_490">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/news/1571008.html

相关文章

Java基础(十三):Java中的数组使用

目录 java的数组数组的定义创建数组和初始化数组的声明方式(以一维数组为例)案例 数组的初始化1. 静态初始化2. 动态初始化3. 默认初始化 数组常见操作数组的遍历【例】使用循环初始化和遍历数组数组的拷贝**java.util.Arrays 类**多维数组数组存储表格数据 java的数组 数组的…

云原生微服务

能够认识到云原生微服务对应用程序设计的影响&#xff0c;描述无状态微服务&#xff0c;并比较单体和微服务架构。要充分利用运营模式&#xff0c;您需要以不同的方式思考应用程序设计。您需要考虑云原生微服务。此图像显示了一个应用程序&#xff0c;该应用程序被设计为小型微…

Android车机DIY开发之软件篇(十二)编译Automotive OS错误(3)

Android车机DIY开发之软件篇(十二)编译Automotive OS错误(3) 问题 [ 85% 113538/132897] //hardware/interfaces/neuralnetworks/1.1/utils:neuralnetworks_utils_hal_1_1 clang src/Device.cpp [ 85% 113539/132897] //hardware/interfaces/neuralnetworks/1.1/utils:neural…

学JDBC 第二日

数据库连接池 作用 使数据库连接达到重用的效果&#xff0c;较少的消耗资源 原理 在创建连接池对象时&#xff0c;创建好指定个数的连接对象 之后直接获取连接对象使用即可&#xff0c;不用每次都创建连接对象 从数据库连接池中获取的对象的close方法真的关闭连接对象了吗…

基于布谷鸟算法实现率定系数的starter

布谷鸟算法&#xff08;Cuckoo Search, CS&#xff09;是一种基于群体智能的优化算法&#xff0c;灵感来源于布谷鸟的繁殖行为以及宿主鸟发现外来蛋的概率。该算法由 Xin-She Yang 和 Suash Deb 在2009年提出。它结合了莱维飞行&#xff08;Lvy flight&#xff09;这一随机漫步…

CSS(三)less一篇搞定

目录 一、less 1.1什么是less 1.2Less编译 1.3变量 1.4混合 1.5嵌套 1.6运算 1.7函数 1.8作用域 1.9注释与导入 一、less 1.1什么是less 我们写了这么久的CSS,里面有很多重复代码&#xff0c;包括通配颜色值、容器大小。那我们能否通过js声明变量来解决这些问题&…

DeepSeek-V3:开源多模态大模型的突破与未来

目录 引言 一、DeepSeek-V3 的概述 1.1 什么是 DeepSeek-V3&#xff1f; 1.2 DeepSeek-V3 的定位 二、DeepSeek-V3 的核心特性 2.1 多模态能力 2.2 开源与可扩展性 2.3 高性能与高效训练 2.4 多语言支持 2.5 安全与伦理 三、DeepSeek-V3 的技术架构 3.1 模型架构 3…

Android 稳定性优化总结

对稳定性的理解 应用稳定性是最重要的性能指标之一&#xff0c;是APP质量构建体系中的基本盘&#xff0c;如果应用的稳定性出现问题&#xff0c;对产品、用户造成的伤害将是致命的。本文将从以下几个方面对应用稳定性优化进行整理。 需要说明&#xff0c;广义的稳定性不仅仅是…