使用 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 {
@Autowired
private 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 {
@Autowired
private BookService bookService;
@GetMapping
public 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
即可看到图书列表页面,并且可以进行图书的添加、编辑和删除操作。