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

一个基于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 Setters
    public 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 Setters
    public 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 Setters
    public 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 {
    @Autowired
    private 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 {
    @Autowired
    private 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 {
    @Autowired
    private 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 {
    @Autowired
    private ComputerService computerService;

    @GetMapping
    public List<Computer> getAllComputers() {
        return computerService.getAllComputers();
    }

    @GetMapping("/{id}")
    public Computer getComputerById(@PathVariable Long id) {
        return computerService.getComputerById(id);
    }

    @PostMapping
    public 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 {
    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    @PostMapping
    public 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 {
    @Autowired
    private RecordService recordService;

    @GetMapping
    public List<Record> getAllRecords() {
        return recordService.getAllRecords();
    }

    @GetMapping("/{id}")
    public Record getRecordById(@PathVariable Long id) {
        return recordService.getRecordById(id);
    }

    @PostMapping
    public 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等前端框架)。

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

相关推荐
i***66506 小时前
SpringBoot实战(三十二)集成 ofdrw,实现 PDF 和 OFD 的转换、SM2 签署OFD
spring boot·后端·pdf
靠沿7 小时前
Java数据结构初阶——LinkedList
java·开发语言·数据结构
qq_12498707537 小时前
基于springboot的建筑业数据管理系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
一 乐7 小时前
宠物管理|宠物共享|基于Java+vue的宠物共享管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·springboot·宠物
a crazy day7 小时前
Spring相关知识点【详细版】
java·spring·rpc
IT_陈寒7 小时前
Vite 5.0实战:10个你可能不知道的性能优化技巧与插件生态深度解析
前端·人工智能·后端
z***3357 小时前
SQL Server2022版+SSMS安装教程(保姆级)
后端·python·flask
白露与泡影7 小时前
MySQL中的12个良好SQL编写习惯
java·数据库·面试
foundbug9997 小时前
配置Spring框架以连接SQL Server数据库
java·数据库·spring
凯酱8 小时前
@JsonSerialize
java