Spring Boot座位预约系统的示例项目
时间: 2023-06-19 21:03:30 浏览: 83
Springboot项目示例
下面是一个简单的 Spring Boot 座位预约系统的示例项目。该系统允许用户浏览可用的座位,选择一个座位进行预约,并取消预约。
1. 创建Spring Boot项目
首先,在你的IDE中创建一个新的Spring Boot项目。你可以使用Spring Initializr或者在IDE中创建项目。
2. 添加依赖项
在项目的pom.xml文件中添加以下依赖项:
```xml
<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>
```
这些依赖项将添加Spring Data JPA、Thymeleaf模板引擎和Spring MVC Web框架。
3. 创建数据模型
在 src/main/java 目录下创建一个名为“model”的包,并创建以下实体类:
Seat.java:
```java
@Entity
public class Seat {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private boolean isReserved;
// constructors, getters, setters
}
```
Reservation.java:
```java
@Entity
public class Reservation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "seat_id")
private Seat seat;
private String name;
// constructors, getters, setters
}
```
4. 创建Repository
在src/main/java目录下创建一个名为“repository”的包,并创建以下接口:
SeatRepository.java:
```java
public interface SeatRepository extends JpaRepository<Seat, Long> {
List<Seat> findByIsReservedFalse();
}
```
ReservationRepository.java:
```java
public interface ReservationRepository extends JpaRepository<Reservation, Long> {
}
```
5. 创建Controller和View
在src/main/java目录下创建一个名为“controller”的包,并创建以下控制器:
SeatController.java:
```java
@Controller
public class SeatController {
@Autowired
private SeatRepository seatRepository;
@GetMapping("/")
public String listSeats(Model model) {
List<Seat> availableSeats = seatRepository.findByIsReservedFalse();
model.addAttribute("seats", availableSeats);
return "listSeats";
}
@GetMapping("/reserve/{id}")
public String reserveSeat(@PathVariable("id") long id, Model model) {
Seat seat = seatRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid seat id:" + id));
model.addAttribute("seat", seat);
return "reserveSeat";
}
@PostMapping("/reserve/{id}")
public String saveReservation(@PathVariable("id") long id, @RequestParam("name") String name) {
Seat seat = seatRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid seat id:" + id));
seat.setReserved(true);
seatRepository.save(seat);
Reservation reservation = new Reservation();
reservation.setSeat(seat);
reservation.setName(name);
reservationRepository.save(reservation);
return "redirect:/";
}
@GetMapping("/cancel/{id}")
public String cancelReservation(@PathVariable("id") long id, Model model) {
Reservation reservation = reservationRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid reservation id:" + id));
Seat seat = reservation.getSeat();
seat.setReserved(false);
seatRepository.save(seat);
reservationRepository.delete(reservation);
return "redirect:/";
}
}
```
这些方法处理列出可用座位、预约座位、保存预约和取消预约的逻辑。
在src/main/resources/templates目录下创建以下视图:
listSeats.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Seat Reservation System</title>
</head>
<body>
<h1>Available Seats</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="seat : ${seats}">
<td th:text="${seat.name}"></td>
<td>
<a th:href="@{/reserve/{id}(id=${seat.id})}">Reserve</a>
</td>
</tr>
</tbody>
</table>
</body>
</html>
```
reserveSeat.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Seat Reservation System</title>
</head>
<body>
<h1>Reserve Seat</h1>
<form method="post" th:action="@{/reserve/{id}(id=${seat.id})}">
<label>Name:</label>
<input type="text" name="name"/>
<button type="submit">Submit</button>
</form>
</body>
</html>
```
6. 运行应用程序
现在,你可以启动应用程序并在浏览器中访问 http://localhost:8080/ 来查看可用的座位并进行预约。
阅读全文