使用 Spring Boot 框架實現圖書管理系統可以按照以下步驟進行,涵蓋了從項目搭建、數據庫設計、后端接口開發到前端頁面展示的整個流程。
1. 項目搭建
可以使用 Spring Initializr(https://start.spring.io/?)來快速創建一個 Spring Boot 項目,選擇以下依賴:
- Spring Web:用于構建 RESTful API 和 Web 應用。
- Spring Data JPA:用于簡化數據庫操作。
- MySQL Driver:如果使用 MySQL 數據庫。
- Thymeleaf:作為模板引擎來構建前端頁面。
2. 數據庫設計
設計圖書管理系統的數據庫,主要涉及兩個實體:圖書(Book)和用戶(User),這里以 MySQL 為例創建相應的表。
2.1 創建數據庫
收起
sql
CREATE DATABASE book_management;
USE book_management;
2.2 創建圖書表
收起
sql
CREATE TABLE books (id INT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(255) NOT NULL,author VARCHAR(255) NOT NULL,isbn VARCHAR(20) UNIQUE
);
3. 配置數據庫連接
在?src/main/resources/application.properties
?中配置數據庫連接信息:
收起
properties
spring.datasource.url=jdbc:mysql://localhost:3306/book_management
spring.datasource.username=root
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
4. 創建實體類
在?src/main/java
?下創建相應的實體類,例如?Book
?類:
收起
java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class Book {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String title;private String author;private String isbn;// 無參構造函數public Book() {}// 有參構造函數public Book(String title, String author, String isbn) {this.title = title;this.author = author;this.isbn = isbn;}// Getter 和 Setter 方法public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getIsbn() {return isbn;}public void setIsbn(String isbn) {this.isbn = isbn;}
}
5. 創建數據訪問層(Repository)
創建一個?BookRepository
?接口,繼承?JpaRepository
?來實現對?Book
?實體的基本數據庫操作:
收起
java
import org.springframework.data.jpa.repository.JpaRepository;public interface BookRepository extends JpaRepository<Book, Long> {
}
6. 創建服務層(Service)
創建一個?BookService
?類來處理圖書的業務邏輯:
收起
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class BookService {@Autowiredprivate BookRepository bookRepository;public List<Book> getAllBooks() {return bookRepository.findAll();}public Book saveBook(Book book) {return bookRepository.save(book);}public Book getBookById(Long id) {return bookRepository.findById(id).orElse(null);}public void deleteBook(Long id) {bookRepository.deleteById(id);}
}
7. 創建控制器層(Controller)
創建一個?BookController
?類來處理 HTTP 請求:
收起
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import java.util.List;@Controller
@RequestMapping("/books")
public class BookController {@Autowiredprivate BookService bookService;@GetMappingpublic String getAllBooks(Model model) {List<Book> books = bookService.getAllBooks();model.addAttribute("books", books);return "books";}@GetMapping("/add")public String showAddBookForm(Model model) {model.addAttribute("book", new Book());return "add-book";}@PostMapping("/add")public String addBook(@ModelAttribute Book book) {bookService.saveBook(book);return "redirect:/books";}@GetMapping("/edit/{id}")public String showEditBookForm(@PathVariable Long id, Model model) {Book book = bookService.getBookById(id);model.addAttribute("book", book);return "edit-book";}@PostMapping("/edit/{id}")public String editBook(@PathVariable Long id, @ModelAttribute Book book) {book.setId(id);bookService.saveBook(book);return "redirect:/books";}@GetMapping("/delete/{id}")public String deleteBook(@PathVariable Long id) {bookService.deleteBook(id);return "redirect:/books";}
}
8. 創建前端頁面
在?src/main/resources/templates
?目錄下創建相應的 Thymeleaf 模板頁面。
8.1?books.html
收起
html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>圖書列表</title>
</head>
<body><h1>圖書列表</h1><a href="/books/add">添加圖書</a><table><thead><tr><th>ID</th><th>標題</th><th>作者</th><th>ISBN</th><th>操作</th></tr></thead><tbody><tr th:each="book : ${books}"><td th:text="${book.id}"></td><td th:text="${book.title}"></td><td th:text="${book.author}"></td><td th:text="${book.isbn}"></td><td><a th:href="@{/books/edit/{id}(id=${book.id})}">編輯</a><a th:href="@{/books/delete/{id}(id=${book.id})}">刪除</a></td></tr></tbody></table>
</body>
</html>
8.2?add-book.html
收起
html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>添加圖書</title>
</head>
<body><h1>添加圖書</h1><form method="post" th:action="@{/books/add}" th:object="${book}"><label for="title">標題:</label><input type="text" id="title" th:field="*{title}" required><br><label for="author">作者:</label><input type="text" id="author" th:field="*{author}" required><br><label for="isbn">ISBN:</label><input type="text" id="isbn" th:field="*{isbn}" required><br><input type="submit" value="添加"></form><a href="/books">返回圖書列表</a>
</body>
</html>
8.3?edit-book.html
收起
html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>編輯圖書</title>
</head>
<body><h1>編輯圖書</h1><form method="post" th:action="@{/books/edit/{id}(id=${book.id})}" th:object="${book}"><label for="title">標題:</label><input type="text" id="title" th:field="*{title}" required><br><label for="author">作者:</label><input type="text" id="author" th:field="*{author}" required><br><label for="isbn">ISBN:</label><input type="text" id="isbn" th:field="*{isbn}" required><br><input type="submit" value="保存"></form><a href="/books">返回圖書列表</a>
</body>
</html>
9. 啟動應用程序
創建一個主應用類,通常命名為?BookManagementSystemApplication
,并添加?@SpringBootApplication
?注解:
收起
java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class BookManagementSystemApplication {public static void main(String[] args) {SpringApplication.run(BookManagementSystemApplication.class, args);}
}
運行?main
?方法啟動 Spring Boot 應用程序,訪問?http://localhost:8080/books
?即可看到圖書列表頁面,并且可以進行圖書的添加、編輯和刪除操作。