springboot实现图书管理系统过程例子
时间: 2023-09-15 19:24:32 浏览: 123
好的,下面是一个简单的基于Spring Boot实现的图书管理系统的例子,包含基本的增删改查功能。
1. 创建一个Spring Boot项目,引入相关依赖。
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
```
2. 创建一个图书实体类Book,使用JPA注解标识实体属性和关系。
```java
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String author;
private String description;
// getter/setter
}
```
3. 创建一个BookRepository接口,继承JpaRepository接口,用于操作数据库。
```java
public interface BookRepository extends JpaRepository<Book, Long> {
}
```
4. 创建一个BookController类,用于处理请求和返回视图。
```java
@Controller
public class BookController {
@Autowired
private BookRepository bookRepository;
@GetMapping("/")
public String index(Model model) {
List<Book> books = bookRepository.findAll();
model.addAttribute("books", books);
return "index";
}
@GetMapping("/add")
public String addForm(Model model) {
model.addAttribute("book", new Book());
return "add";
}
@PostMapping("/add")
public String addSubmit(@ModelAttribute Book book) {
bookRepository.save(book);
return "redirect:/";
}
@GetMapping("/edit/{id}")
public String editForm(@PathVariable Long id, Model model) {
Book book = bookRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid book id:" + id));
model.addAttribute("book", book);
return "edit";
}
@PostMapping("/edit/{id}")
public String editSubmit(@PathVariable Long id, @ModelAttribute Book book) {
book.setId(id);
bookRepository.save(book);
return "redirect:/";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable Long id) {
bookRepository.deleteById(id);
return "redirect:/";
}
}
```
5. 创建Thymeleaf模板,用于展示图书信息和表单。
index.html
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
</head>
<body>
<h1>图书列表</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>简介</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.description}"></td>
<td>
<a th:href="@{/edit/{id}(id=${book.id})}">编辑</a>
<a th:href="@{/delete/{id}(id=${book.id})}">删除</a>
</td>
</tr>
</tbody>
</table>
<br>
<a th:href="@{/add}">添加图书</a>
</body>
</html>
```
add.html
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加图书</title>
</head>
<body>
<h1>添加图书</h1>
<form th:action="@{/add}" th:object="${book}" method="post">
<table>
<tr>
<td>书名:</td>
<td><input type="text" th:field="*{title}"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" th:field="*{author}"></td>
</tr>
<tr>
<td>简介:</td>
<td><textarea th:field="*{description}"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="添加"></td>
</tr>
</table>
</form>
</body>
</html>
```
edit.html
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>编辑图书</title>
</head>
<body>
<h1>编辑图书</h1>
<form th:action="@{/edit/{id}(id=${book.id})}" th:object="${book}" method="post">
<table>
<tr>
<td>书名:</td>
<td><input type="text" th:field="*{title}"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" th:field="*{author}"></td>
</tr>
<tr>
<td>简介:</td>
<td><textarea th:field="*{description}"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="保存"></td>
</tr>
</table>
</form>
</body>
</html>
```
6. 启动应用,访问http://localhost:8080/,可以看到图书列表,点击添加图书按钮,可以添加一本新图书,点击编辑按钮,可以修改图书信息,点击删除按钮,可以删除图书。
以上就是一个简单的基于Spring Boot实现的图书管理系统例子,希望能对你有所帮助。
阅读全文