0


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

文章目录

技术栈
  • 前端:HTML5、CSS3、JavaScript
  • 后端:Java(Spring Boot框架)
  • 数据库:SQLite(或 MySQL、PostgreSQL 等)
功能需求
  1. 用户管理:- 用户注册与登录- 用户信息管理
  2. 图书管理:- 添加、删除和修改书籍信息- 图书搜索功能
  3. 借阅管理:- 借阅和归还书籍- 查看借阅记录
  4. 系统设置:- 参数配置(如借阅期限、最大借阅数量)
实现步骤
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.javapackagecom.example.librarymanagement.model;importjavax.persistence.*;importjava.util.Set;@EntitypublicclassUser{@Id@GeneratedValue(strategy =GenerationType.IDENTITY)privateLong id;privateString username;privateString password;privateString email;@OneToMany(mappedBy ="user")privateSet<Borrow> borrows;// Getters and Setters}// Book.javapackagecom.example.librarymanagement.model;importjavax.persistence.*;importjava.util.Set;@EntitypublicclassBook{@Id@GeneratedValue(strategy =GenerationType.IDENTITY)privateLong id;privateString isbn;privateString title;privateString author;privateString publisher;privateString publicationDate;privateString category;privateint stock;@OneToMany(mappedBy ="book")privateSet<Borrow> borrows;// Getters and Setters}// Borrow.javapackagecom.example.librarymanagement.model;importjavax.persistence.*;@EntitypublicclassBorrow{@Id@GeneratedValue(strategy =GenerationType.IDENTITY)privateLong id;@ManyToOne@JoinColumn(name ="user_id")privateUser user;@ManyToOne@JoinColumn(name ="book_id")privateBook book;privateString borrowDate;privateString returnDate;// Getters and Setters}
5. 创建仓库接口

repository

包下创建仓库接口。

// UserRepository.javapackagecom.example.librarymanagement.repository;importcom.example.librarymanagement.model.User;importorg.springframework.data.jpa.repository.JpaRepository;publicinterfaceUserRepositoryextendsJpaRepository<User,Long>{}// BookRepository.javapackagecom.example.librarymanagement.repository;importcom.example.librarymanagement.model.Book;importorg.springframework.data.jpa.repository.JpaRepository;publicinterfaceBookRepositoryextendsJpaRepository<Book,Long>{}// BorrowRepository.javapackagecom.example.librarymanagement.repository;importcom.example.librarymanagement.model.Borrow;importorg.springframework.data.jpa.repository.JpaRepository;publicinterfaceBorrowRepositoryextendsJpaRepository<Borrow,Long>{}
6. 创建服务类

service

包下创建服务类。

// UserService.javapackagecom.example.librarymanagement.service;importcom.example.librarymanagement.model.User;importcom.example.librarymanagement.repository.UserRepository;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@ServicepublicclassUserService{@AutowiredprivateUserRepository userRepository;publicList<User>getAllUsers(){return userRepository.findAll();}publicUsergetUserById(Long id){return userRepository.findById(id).orElse(null);}publicUsercreateUser(User user){return userRepository.save(user);}publicUserupdateUser(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);}returnnull;}publicvoiddeleteUser(Long id){
        userRepository.deleteById(id);}}// BookService.javapackagecom.example.librarymanagement.service;importcom.example.librarymanagement.model.Book;importcom.example.librarymanagement.repository.BookRepository;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@ServicepublicclassBookService{@AutowiredprivateBookRepository bookRepository;publicList<Book>getAllBooks(){return bookRepository.findAll();}publicBookgetBookById(Long id){return bookRepository.findById(id).orElse(null);}publicBookcreateBook(Book book){return bookRepository.save(book);}publicBookupdateBook(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);}returnnull;}publicvoiddeleteBook(Long id){
        bookRepository.deleteById(id);}}// BorrowService.javapackagecom.example.librarymanagement.service;importcom.example.librarymanagement.model.Borrow;importcom.example.librarymanagement.repository.BorrowRepository;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@ServicepublicclassBorrowService{@AutowiredprivateBorrowRepository borrowRepository;publicList<Borrow>getAllBorrows(){return borrowRepository.findAll();}publicBorrowgetBorrowById(Long id){return borrowRepository.findById(id).orElse(null);}publicBorrowcreateBorrow(Borrow borrow){return borrowRepository.save(borrow);}publicvoiddeleteBorrow(Long id){
        borrowRepository.deleteById(id);}}
7. 创建控制器

controller

包下创建控制器类。

// UserController.javapackagecom.example.librarymanagement.controller;importcom.example.librarymanagement.model.User;importcom.example.librarymanagement.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importjava.util.List;@RestController@RequestMapping("/users")publicclassUserController{@AutowiredprivateUserService userService;@GetMappingpublicList<User>getAllUsers(){return userService.getAllUsers();}@GetMapping("/{id}")publicUsergetUserById(@PathVariableLong id){return userService.getUserById(id);}@PostMappingpublicUsercreateUser(@RequestBodyUser user){return userService.createUser(user);}@PutMapping("/{id}")publicUserupdateUser(@PathVariableLong id,@RequestBodyUser userDetails){return userService.updateUser(id, userDetails);}@DeleteMapping("/{id}")publicvoiddeleteUser(@PathVariableLong id){
        userService.deleteUser(id);}}// BookController.javapackagecom.example.librarymanagement.controller;importcom.example.librarymanagement.model.Book;importcom.example.librarymanagement.service.BookService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importjava.util.List;@RestController@RequestMapping("/books")publicclassBookController{@AutowiredprivateBookService bookService;@GetMappingpublicList<Book>getAllBooks(){return bookService.getAllBooks();}@GetMapping("/{id}")publicBookgetBookById(@PathVariableLong id){return bookService.getBookById(id);}@PostMappingpublicBookcreateBook(@RequestBodyBook book){return bookService.createBook(book);}@PutMapping("/{id}")publicBookupdateBook(@PathVariableLong id,@RequestBodyBook bookDetails){return bookService.updateBook(id, bookDetails);}@DeleteMapping("/{id}")publicvoiddeleteBook(@PathVariableLong id){
        bookService.deleteBook(id);}}// BorrowController.javapackagecom.example.librarymanagement.controller;importcom.example.librarymanagement.model.Borrow;importcom.example.librarymanagement.service.BorrowService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importjava.util.List;@RestController@RequestMapping("/borrows")publicclassBorrowController{@AutowiredprivateBorrowService borrowService;@GetMappingpublicList<Borrow>getAllBorrows(){return borrowService.getAllBorrows();}@GetMapping("/{id}")publicBorrowgetBorrowById(@PathVariableLong id){return borrowService.getBorrowById(id);}@PostMappingpublicBorrowcreateBorrow(@RequestBodyBorrow borrow){return borrowService.createBorrow(borrow);}@DeleteMapping("/{id}")publicvoiddeleteBorrow(@PathVariableLong id){
        borrowService.deleteBorrow(id);}}
8. 创建前端页面

src/main/resources/templates

目录下创建 Thymeleaf 模板文件,用于展示用户界面。

<!-- index.html --><!DOCTYPEhtml><htmlxmlns:th="http://www.thymeleaf.org"><head><metacharset="UTF-8"><title>Library Management System</title><linkrel="stylesheet"th:href="@{/css/style.css}"></head><body><header><h1>Library Management System</h1><nav><ath:href="@{/users}">Users</a><ath:href="@{/books}">Books</a><ath: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
标签: 安全 java spring boot

本文转载自: https://blog.csdn.net/2301_77163982/article/details/144122008
版权归原作者 爪哇学长 所有, 如有侵权,请联系我们删除。

“如何构建一个高效安全的图书管理系统”的评论:

还没有评论