用springboot写一个图书管理系统
时间: 2024-03-09 20:25:35 浏览: 86
好的,下面是一个简单的图书管理系统的实现,使用了Spring Boot框架和MySQL数据库。
首先,我们需要创建一个Spring Boot项目。在pom.xml文件中添加以下依赖:
```xml
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
```
接下来,我们需要创建一个Book实体类,用于表示图书信息,代码如下:
```java
@Entity
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String title;
@Column(nullable = false)
private String author;
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;
}
}
```
然后,我们需要创建一个BookRepository接口,用于对图书信息进行增删改查操作,代码如下:
```java
public interface BookRepository extends JpaRepository<Book, Long> {
List<Book> findByTitleContainingOrAuthorContaining(String keyword1, String keyword2);
}
```
接下来,我们需要创建一个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("/search")
public String search(@RequestParam("q") String query, Model model) {
List<Book> books = bookRepository.findByTitleContainingOrAuthorContaining(query, query);
model.addAttribute("books", books);
return "search";
}
@GetMapping("/add")
public String add(Model model) {
model.addAttribute("book", new Book());
return "add";
}
@PostMapping("/add")
public String add(@ModelAttribute("book") Book book) {
bookRepository.save(book);
return "redirect:/";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Long id, Model model) {
Optional<Book> book = bookRepository.findById(id);
if (book.isPresent()) {
model.addAttribute("book", book.get());
return "edit";
} else {
return "redirect:/";
}
}
@PostMapping("/edit")
public String edit(@ModelAttribute("book") Book book) {
bookRepository.save(book);
return "redirect:/";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Long id) {
bookRepository.deleteById(id);
return "redirect:/";
}
}
```
最后,我们需要创建一些HTML模板文件,用于显示图书信息和处理用户的请求,代码如下:
index.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Book Management System</title>
</head>
<body>
<h1>Book Management System</h1>
<form action="/search" method="get">
<input type="text" name="q" placeholder="Search...">
<button type="submit">Search</button>
</form>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Action</th>
</tr>
<tr th:each="book : ${books}">
<td th:text="${book.id}"></td>
<td th:text="${book.title}"></td>
<td th:text="${book.author}"></td>
<td>
<a th:href="@{/edit/{id}(id=${book.id})}">Edit</a>
<a th:href="@{/delete/{id}(id=${book.id})}" onclick="return confirm('Are you sure?')">Delete</a>
</td>
</tr>
</table>
<br>
<a href="/add">Add New Book</a>
</body>
</html>
```
search.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Book Management System</title>
</head>
<body>
<h1>Book Management System</h1>
<form action="/search" method="get">
<input type="text" name="q" placeholder="Search..." value="${param.q}">
<button type="submit">Search</button>
</form>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Action</th>
</tr>
<tr th:each="book : ${books}">
<td th:text="${book.id}"></td>
<td th:text="${book.title}"></td>
<td th:text="${book.author}"></td>
<td>
<a th:href="@{/edit/{id}(id=${book.id})}">Edit</a>
<a th:href="@{/delete/{id}(id=${book.id})}" onclick="return confirm('Are you sure?')">Delete</a>
</td>
</tr>
</table>
<br>
<a href="/add">Add New Book</a>
</body>
</html>
```
add.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Book Management System</title>
</head>
<body>
<h1>Book Management System</h1>
<form action="/add" method="post">
<label>Title:</label><br>
<input type="text" name="title"><br>
<label>Author:</label><br>
<input type="text" name="author"><br>
<button type="submit">Add Book</button>
</form>
<br>
<a href="/">Back to Home</a>
</body>
</html>
```
edit.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Book Management System</title>
</head>
<body>
<h1>Book Management System</h1>
<form action="/edit" method="post">
<input type="hidden" name="id" th:value="${book.id}">
<label>Title:</label><br>
<input type="text" name="title" th:value="${book.title}"><br>
<label>Author:</label><br>
<input type="text" name="author" th:value="${book.author}"><br>
<button type="submit">Save Changes</button>
</form>
<br>
<a href="/">Back to Home</a>
</body>
</html>
```
这样,一个简单的图书管理系统就完成了。启动应用后,访问 http://localhost:8080/ 即可进入主界面,可以进行图书信息的增删改查操作。
阅读全文