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

打印 上一主题 下一主题

主题 898|帖子 898|积分 2694

技能栈



  • 前端: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. 创建项目结构

创建项目的文件结构如下:
  1. library-management-system/
  2. ├── src/
  3. │   ├── main/
  4. │   │   ├── java/
  5. │   │   │   └── com/
  6. │   │   │       └── example/
  7. │   │   │           └── librarymanagement/
  8. │   │   │               ├── controller/
  9. │   │   │               ├── model/
  10. │   │   │               ├── repository/
  11. │   │   │               ├── service/
  12. │   │   │               └── LibraryManagementApplication.java
  13. │   │   └── resources/
  14. │   │       ├── static/
  15. │   │       ├── templates/
  16. │   │       └── application.properties
  17. └── pom.xml
复制代码
3. 配置数据库

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

在 model 包下创建实体类。
  1. // User.java
  2. package com.example.librarymanagement.model;
  3. import javax.persistence.*;
  4. import java.util.Set;
  5. @Entity
  6. public class User {
  7.     @Id
  8.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  9.     private Long id;
  10.     private String username;
  11.     private String password;
  12.     private String email;
  13.     @OneToMany(mappedBy = "user")
  14.     private Set<Borrow> borrows;
  15.     // Getters and Setters
  16. }
  17. // Book.java
  18. package com.example.librarymanagement.model;
  19. import javax.persistence.*;
  20. import java.util.Set;
  21. @Entity
  22. public class Book {
  23.     @Id
  24.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  25.     private Long id;
  26.     private String isbn;
  27.     private String title;
  28.     private String author;
  29.     private String publisher;
  30.     private String publicationDate;
  31.     private String category;
  32.     private int stock;
  33.     @OneToMany(mappedBy = "book")
  34.     private Set<Borrow> borrows;
  35.     // Getters and Setters
  36. }
  37. // Borrow.java
  38. package com.example.librarymanagement.model;
  39. import javax.persistence.*;
  40. @Entity
  41. public class Borrow {
  42.     @Id
  43.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  44.     private Long id;
  45.     @ManyToOne
  46.     @JoinColumn(name = "user_id")
  47.     private User user;
  48.     @ManyToOne
  49.     @JoinColumn(name = "book_id")
  50.     private Book book;
  51.     private String borrowDate;
  52.     private String returnDate;
  53.     // Getters and Setters
  54. }
复制代码
5. 创建堆栈接口

在 repository 包下创建堆栈接口。
  1. // UserRepository.java
  2. package com.example.librarymanagement.repository;
  3. import com.example.librarymanagement.model.User;
  4. import org.springframework.data.jpa.repository.JpaRepository;
  5. public interface UserRepository extends JpaRepository<User, Long> {
  6. }
  7. // BookRepository.java
  8. package com.example.librarymanagement.repository;
  9. import com.example.librarymanagement.model.Book;
  10. import org.springframework.data.jpa.repository.JpaRepository;
  11. public interface BookRepository extends JpaRepository<Book, Long> {
  12. }
  13. // BorrowRepository.java
  14. package com.example.librarymanagement.repository;
  15. import com.example.librarymanagement.model.Borrow;
  16. import org.springframework.data.jpa.repository.JpaRepository;
  17. public interface BorrowRepository extends JpaRepository<Borrow, Long> {
  18. }
复制代码
6. 创建服务类

在 service 包下创建服务类。
  1. // UserService.java
  2. package com.example.librarymanagement.service;
  3. import com.example.librarymanagement.model.User;
  4. import com.example.librarymanagement.repository.UserRepository;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. @Service
  9. public class UserService {
  10.     @Autowired
  11.     private UserRepository userRepository;
  12.     public List<User> getAllUsers() {
  13.         return userRepository.findAll();
  14.     }
  15.     public User getUserById(Long id) {
  16.         return userRepository.findById(id).orElse(null);
  17.     }
  18.     public User createUser(User user) {
  19.         return userRepository.save(user);
  20.     }
  21.     public User updateUser(Long id, User userDetails) {
  22.         User user = userRepository.findById(id).orElse(null);
  23.         if (user != null) {
  24.             user.setUsername(userDetails.getUsername());
  25.             user.setPassword(userDetails.getPassword());
  26.             user.setEmail(userDetails.getEmail());
  27.             return userRepository.save(user);
  28.         }
  29.         return null;
  30.     }
  31.     public void deleteUser(Long id) {
  32.         userRepository.deleteById(id);
  33.     }
  34. }
  35. // BookService.java
  36. package com.example.librarymanagement.service;
  37. import com.example.librarymanagement.model.Book;
  38. import com.example.librarymanagement.repository.BookRepository;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.stereotype.Service;
  41. import java.util.List;
  42. @Service
  43. public class BookService {
  44.     @Autowired
  45.     private BookRepository bookRepository;
  46.     public List<Book> getAllBooks() {
  47.         return bookRepository.findAll();
  48.     }
  49.     public Book getBookById(Long id) {
  50.         return bookRepository.findById(id).orElse(null);
  51.     }
  52.     public Book createBook(Book book) {
  53.         return bookRepository.save(book);
  54.     }
  55.     public Book updateBook(Long id, Book bookDetails) {
  56.         Book book = bookRepository.findById(id).orElse(null);
  57.         if (book != null) {
  58.             book.setIsbn(bookDetails.getIsbn());
  59.             book.setTitle(bookDetails.getTitle());
  60.             book.setAuthor(bookDetails.getAuthor());
  61.             book.setPublisher(bookDetails.getPublisher());
  62.             book.setPublicationDate(bookDetails.getPublicationDate());
  63.             book.setCategory(bookDetails.getCategory());
  64.             book.setStock(bookDetails.getStock());
  65.             return bookRepository.save(book);
  66.         }
  67.         return null;
  68.     }
  69.     public void deleteBook(Long id) {
  70.         bookRepository.deleteById(id);
  71.     }
  72. }
  73. // BorrowService.java
  74. package com.example.librarymanagement.service;
  75. import com.example.librarymanagement.model.Borrow;
  76. import com.example.librarymanagement.repository.BorrowRepository;
  77. import org.springframework.beans.factory.annotation.Autowired;
  78. import org.springframework.stereotype.Service;
  79. import java.util.List;
  80. @Service
  81. public class BorrowService {
  82.     @Autowired
  83.     private BorrowRepository borrowRepository;
  84.     public List<Borrow> getAllBorrows() {
  85.         return borrowRepository.findAll();
  86.     }
  87.     public Borrow getBorrowById(Long id) {
  88.         return borrowRepository.findById(id).orElse(null);
  89.     }
  90.     public Borrow createBorrow(Borrow borrow) {
  91.         return borrowRepository.save(borrow);
  92.     }
  93.     public void deleteBorrow(Long id) {
  94.         borrowRepository.deleteById(id);
  95.     }
  96. }
复制代码
7. 创建控制器

在 controller 包下创建控制器类。
  1. // UserController.java
  2. package com.example.librarymanagement.controller;
  3. import com.example.librarymanagement.model.User;
  4. import com.example.librarymanagement.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.*;
  7. import java.util.List;
  8. @RestController
  9. @RequestMapping("/users")
  10. public class UserController {
  11.     @Autowired
  12.     private UserService userService;
  13.     @GetMapping
  14.     public List<User> getAllUsers() {
  15.         return userService.getAllUsers();
  16.     }
  17.     @GetMapping("/{id}")
  18.     public User getUserById(@PathVariable Long id) {
  19.         return userService.getUserById(id);
  20.     }
  21.     @PostMapping
  22.     public User createUser(@RequestBody User user) {
  23.         return userService.createUser(user);
  24.     }
  25.     @PutMapping("/{id}")
  26.     public User updateUser(@PathVariable Long id, @RequestBody User userDetails) {
  27.         return userService.updateUser(id, userDetails);
  28.     }
  29.     @DeleteMapping("/{id}")
  30.     public void deleteUser(@PathVariable Long id) {
  31.         userService.deleteUser(id);
  32.     }
  33. }
  34. // BookController.java
  35. package com.example.librarymanagement.controller;
  36. import com.example.librarymanagement.model.Book;
  37. import com.example.librarymanagement.service.BookService;
  38. import org.springframework.beans.factory.annotation.Autowired;
  39. import org.springframework.web.bind.annotation.*;
  40. import java.util.List;
  41. @RestController
  42. @RequestMapping("/books")
  43. public class BookController {
  44.     @Autowired
  45.     private BookService bookService;
  46.     @GetMapping
  47.     public List<Book> getAllBooks() {
  48.         return bookService.getAllBooks();
  49.     }
  50.     @GetMapping("/{id}")
  51.     public Book getBookById(@PathVariable Long id) {
  52.         return bookService.getBookById(id);
  53.     }
  54.     @PostMapping
  55.     public Book createBook(@RequestBody Book book) {
  56.         return bookService.createBook(book);
  57.     }
  58.     @PutMapping("/{id}")
  59.     public Book updateBook(@PathVariable Long id, @RequestBody Book bookDetails) {
  60.         return bookService.updateBook(id, bookDetails);
  61.     }
  62.     @DeleteMapping("/{id}")
  63.     public void deleteBook(@PathVariable Long id) {
  64.         bookService.deleteBook(id);
  65.     }
  66. }
  67. // BorrowController.java
  68. package com.example.librarymanagement.controller;
  69. import com.example.librarymanagement.model.Borrow;
  70. import com.example.librarymanagement.service.BorrowService;
  71. import org.springframework.beans.factory.annotation.Autowired;
  72. import org.springframework.web.bind.annotation.*;
  73. import java.util.List;
  74. @RestController
  75. @RequestMapping("/borrows")
  76. public class BorrowController {
  77.     @Autowired
  78.     private BorrowService borrowService;
  79.     @GetMapping
  80.     public List<Borrow> getAllBorrows() {
  81.         return borrowService.getAllBorrows();
  82.     }
  83.     @GetMapping("/{id}")
  84.     public Borrow getBorrowById(@PathVariable Long id) {
  85.         return borrowService.getBorrowById(id);
  86.     }
  87.     @PostMapping
  88.     public Borrow createBorrow(@RequestBody Borrow borrow) {
  89.         return borrowService.createBorrow(borrow);
  90.     }
  91.     @DeleteMapping("/{id}")
  92.     public void deleteBorrow(@PathVariable Long id) {
  93.         borrowService.deleteBorrow(id);
  94.     }
  95. }
复制代码
8. 创建前端页面

在 src/main/resources/templates 目次下创建 Thymeleaf 模板文件,用于展示用户界面。
  1. <!-- index.html -->
  2. <!DOCTYPE html>
  3. <html xmlns:th="http://www.thymeleaf.org">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>Library Management System</title>
  7.     <link rel="stylesheet" th:href="@{/css/style.css}">
  8. </head>
  9. <body>
  10. <header>
  11.     <h1>Library Management System</h1>
  12.     <nav>
  13.         <a th:href="@{/users}">Users</a>
  14.         <a th:href="@{/books}">Books</a>
  15.         <a th:href="@{/borrows}">Borrows</a>
  16.     </nav>
  17. </header>
  18. <main>
  19.     <p>Welcome to the Library Management System!</p>
  20. </main>
  21. <footer>
  22.     <p>&copy; 2024 Library Management System</p>
  23. </footer>
  24. </body>
  25. </html>
复制代码
9. 运行项目

启动项目,访问 http://localhost:8080,即可看到图书管理系统的首页。
  1. mvn spring-boot:run
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

慢吞云雾缓吐愁

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表