慢吞云雾缓吐愁 发表于 2025-1-18 10:07:37

如何构建一个高效安全的图书管理系统

技能栈



[*]前端:HTML5、CSS3、JavaScript
[*]后端:Java(Spring Boot框架)
[*]数据库:SQLite(或 MySQL、PostgreSQL 等)
功能需求


[*] 用户管理:

[*]用户注册与登录
[*]用户信息管理

[*] 图书管理:

[*]添加、删除和修改书籍信息
[*]图书搜刮功能

[*] 借阅管理:

[*]借阅和归还书籍
[*]查看借阅记录

[*] 系统设置:

[*]参数配置(如借阅期限、最大借阅数量)

实现步骤

1. 预备开发环境

确保你的开发环境中安装了 JDK 和 IDE(如 IntelliJ IDEA 或 Eclipse)。然后,创建一个新的 Spring Boot 项目。
你可以利用 Spring Initializr 来快速创建项目:


[*]访问 Spring Initializr
[*]选择 Maven 项目,Java 语言,Spring Boot 版本(例如 2.7.x)
[*]添加依赖:Spring Web, Thymeleaf, Spring Data JPA, SQLite JDBC
[*]下载并解压项目
2. 创建项目结构

创建项目的文件结构如下:
library-management-system/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │         └── librarymanagement/
│   │   │               ├── controller/
│   │   │               ├── model/
│   │   │               ├── repository/
│   │   │               ├── service/
│   │   │               └── LibraryManagementApplication.java
│   │   └── resources/
│   │       ├── static/
│   │       ├── templates/
│   │       └── application.properties
└── pom.xml
3. 配置数据库

编辑 src/main/resources/application.properties 文件,配置 SQLite 数据库毗连。
spring.datasource.url=jdbc:sqlite:./library.db
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.jpa.database-platform=org.hibernate.dialect.SQLiteDialect
spring.jpa.hibernate.ddl-auto=update
4. 创建实体类

在 model 包下创建实体类。
// User.java
package com.example.librarymanagement.model;

import javax.persistence.*;
import java.util.Set;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;
    private String email;

    @OneToMany(mappedBy = "user")
    private Set<Borrow> borrows;

    // Getters and Setters
}

// Book.java
package com.example.librarymanagement.model;

import javax.persistence.*;
import java.util.Set;

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String isbn;
    private String title;
    private String author;
    private String publisher;
    private String publicationDate;
    private String category;
    private int stock;

    @OneToMany(mappedBy = "book")
    private Set<Borrow> borrows;

    // Getters and Setters
}

// Borrow.java
package com.example.librarymanagement.model;

import javax.persistence.*;

@Entity
public class Borrow {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "book_id")
    private Book book;

    private String borrowDate;
    private String returnDate;

    // Getters and Setters
}
5. 创建堆栈接口

在 repository 包下创建堆栈接口。
// UserRepository.java
package com.example.librarymanagement.repository;

import com.example.librarymanagement.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}

// BookRepository.java
package com.example.librarymanagement.repository;

import com.example.librarymanagement.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BookRepository extends JpaRepository<Book, Long> {
}

// BorrowRepository.java
package com.example.librarymanagement.repository;

import com.example.librarymanagement.model.Borrow;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BorrowRepository extends JpaRepository<Borrow, Long> {
}
6. 创建服务类

在 service 包下创建服务类。
// UserService.java
package com.example.librarymanagement.service;

import com.example.librarymanagement.model.User;
import com.example.librarymanagement.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 createUser(User user) {
      return userRepository.save(user);
    }

    public User updateUser(Long id, User userDetails) {
      User user = userRepository.findById(id).orElse(null);
      if (user != null) {
            user.setUsername(userDetails.getUsername());
            user.setPassword(userDetails.getPassword());
            user.setEmail(userDetails.getEmail());
            return userRepository.save(user);
      }
      return null;
    }

    public void deleteUser(Long id) {
      userRepository.deleteById(id);
    }
}

// BookService.java
package com.example.librarymanagement.service;

import com.example.librarymanagement.model.Book;
import com.example.librarymanagement.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookRepository bookRepository;

    public List<Book> getAllBooks() {
      return bookRepository.findAll();
    }

    public Book getBookById(Long id) {
      return bookRepository.findById(id).orElse(null);
    }

    public Book createBook(Book book) {
      return bookRepository.save(book);
    }

    public Book updateBook(Long id, Book bookDetails) {
      Book book = bookRepository.findById(id).orElse(null);
      if (book != null) {
            book.setIsbn(bookDetails.getIsbn());
            book.setTitle(bookDetails.getTitle());
            book.setAuthor(bookDetails.getAuthor());
            book.setPublisher(bookDetails.getPublisher());
            book.setPublicationDate(bookDetails.getPublicationDate());
            book.setCategory(bookDetails.getCategory());
            book.setStock(bookDetails.getStock());
            return bookRepository.save(book);
      }
      return null;
    }

    public void deleteBook(Long id) {
      bookRepository.deleteById(id);
    }
}

// BorrowService.java
package com.example.librarymanagement.service;

import com.example.librarymanagement.model.Borrow;
import com.example.librarymanagement.repository.BorrowRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BorrowService {
    @Autowired
    private BorrowRepository borrowRepository;

    public List<Borrow> getAllBorrows() {
      return borrowRepository.findAll();
    }

    public Borrow getBorrowById(Long id) {
      return borrowRepository.findById(id).orElse(null);
    }

    public Borrow createBorrow(Borrow borrow) {
      return borrowRepository.save(borrow);
    }

    public void deleteBorrow(Long id) {
      borrowRepository.deleteById(id);
    }
}
7. 创建控制器

在 controller 包下创建控制器类。
// UserController.java
package com.example.librarymanagement.controller;

import com.example.librarymanagement.model.User;
import com.example.librarymanagement.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.createUser(user);
    }

    @PutMapping("/{id}")
    public User updateUser(@PathVariable Long id, @RequestBody User userDetails) {
      return userService.updateUser(id, userDetails);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
      userService.deleteUser(id);
    }
}

// BookController.java
package com.example.librarymanagement.controller;

import com.example.librarymanagement.model.Book;
import com.example.librarymanagement.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping
    public List<Book> getAllBooks() {
      return bookService.getAllBooks();
    }

    @GetMapping("/{id}")
    public Book getBookById(@PathVariable Long id) {
      return bookService.getBookById(id);
    }

    @PostMapping
    public Book createBook(@RequestBody Book book) {
      return bookService.createBook(book);
    }

    @PutMapping("/{id}")
    public Book updateBook(@PathVariable Long id, @RequestBody Book bookDetails) {
      return bookService.updateBook(id, bookDetails);
    }

    @DeleteMapping("/{id}")
    public void deleteBook(@PathVariable Long id) {
      bookService.deleteBook(id);
    }
}

// BorrowController.java
package com.example.librarymanagement.controller;

import com.example.librarymanagement.model.Borrow;
import com.example.librarymanagement.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/borrows")
public class BorrowController {
    @Autowired
    private BorrowService borrowService;

    @GetMapping
    public List<Borrow> getAllBorrows() {
      return borrowService.getAllBorrows();
    }

    @GetMapping("/{id}")
    public Borrow getBorrowById(@PathVariable Long id) {
      return borrowService.getBorrowById(id);
    }

    @PostMapping
    public Borrow createBorrow(@RequestBody Borrow borrow) {
      return borrowService.createBorrow(borrow);
    }

    @DeleteMapping("/{id}")
    public void deleteBorrow(@PathVariable Long id) {
      borrowService.deleteBorrow(id);
    }
}
8. 创建前端页面

在 src/main/resources/templates 目次下创建 Thymeleaf 模板文件,用于展示用户界面。
<!-- index.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Library Management System</title>
    <link rel="stylesheet" th:href="@{/css/style.css}">
</head>
<body>
<header>
    <h1>Library Management System</h1>
    <nav>
      <a th:href="@{/users}">Users</a>
      <a th:href="@{/books}">Books</a>
      <a th:href="@{/borrows}">Borrows</a>
    </nav>
</header>
<main>
    <p>Welcome to the Library Management System!</p>
</main>
<footer>
    <p>&copy; 2024 Library Management System</p>
</footer>
</body>
</html>
9. 运行项目

启动项目,访问 http://localhost:8080,即可看到图书管理系统的首页。
mvn spring-boot:run

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 如何构建一个高效安全的图书管理系统