基于Java的预约系统通过Spring Boot实现前后端功能,涵盖用户预约、时段管理与数据存储。1. 系统划分前端页面、控制器、服务逻辑与数据模型;2. 使用Spring Boot搭建后端,集成Thymeleaf模板渲染界面;3. AppointmentService管理预约状态,防止时间冲突;4. Controller处理表单提交并返回结果;5. 前端展示可选时段并提示预约成功或失败;6. 可扩展数据库支持、登录验证与动态前端交互。

开发一个小型在线预约系统,用Java可以很好地实现前后端功能。这类系统常见于诊所、理发店、会议室等场景,核心是用户选择时间并提交预约。下面从设计思路到代码结构,一步步说明如何用Java搭建这样一个系统。
1. 系统功能与模块划分
一个基础的预约系统应包含以下功能:
-
用户预约界面:查看可选时间段,填写姓名、联系方式等信息
-
时间段管理:设定每日开放时段,避免重复预约
-
数据存储:保存预约记录
-
后端服务:处理请求、校验、存取数据
主要模块包括:
-
前端页面(HTML + CSS + JS)
- 后端控制器(Servlet 或 Spring Boot)
- 业务逻辑类(如 AppointmentService)
- 数据模型(Appointment 类)
- 存储方式(内存或数据库)
2. 使用Spring Boot快速搭建后端
推荐使用 Spring Boot 来简化开发。它内置了 Web 服务器,能快速暴露 REST 接口。
立即学习“Java免费学习笔记(深入)”;
添加基本依赖(在 pom.xml 中):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
创建一个 Appointment 模型类:
public class Appointment {
private String name;
private String phone;
private String date;
private String time;
// 构造方法、getter 和 setter 省略
}
用一个静态集合模拟数据库:
@Service
public class AppointmentService {
private List<Appointment> appointments = new ArrayList<>();
private Set<String> bookedSlots = new HashSet<>(); // 格式:日期+时间
public boolean isSlotAvailable(String date, String time) {
return !bookedSlots.contains(date + "-" + time);
}
public void save(Appointment appointment) {
appointments.add(appointment);
bookedSlots.add(appointment.getDate() + "-" + appointment.getTime());
}
public List<Appointment> getAll() {
return appointments;
}
}
3. 创建控制器处理请求
编写 Controller 提供页面和接口:
@Controller
public class AppointmentController {
@Autowired
private AppointmentService service;
@GetMapping("/")
public String index(Model model) {
model.addAttribute("appointment", new Appointment());
// 假设提供这几个时间段
model.addAttribute("timeSlots", Arrays.asList("09:00", "10:00", "11:00", "14:00", "15:00"));
return "index"; // 对应 templates/index.html
}
@PostMapping("/book")
public String book(@ModelAttribute Appointment appointment, Model model) {
if (!service.isSlotAvailable(appointment.getDate(), appointment.getTime())) {
model.addAttribute("error", "该时间段已被预约,请选择其他时间。");
model.addAttribute("appointment", appointment);
model.addAttribute("timeSlots", Arrays.asList("09:00", "10:00", "11:00", "14:00", "15:00"));
return "index";
}
service.save(appointment);
model.addAttribute("success", "预约成功!");
return "index";
}
}
Thymeleaf 页面(src/main/resources/templates/index.html)示例:
<form method="post" action="/book">
姓名: <input type="text" name="name" required /><br>
电话: <input type="tel" name="phone" required /><br>
日期: <input type="date" name="date" required /><br>
时间:
<select name="time" required>
<option th:each="slot : ${timeSlots}" th:value="${slot}" th:text="${slot}"></option>
</select><br>
<button type="submit">预约</button>
</form>
<p th:if="${error}" style="color:red" th:text="${error}"></p>
<p th:if="${success}" style="color:green" th:text="${success}"></p>
4. 扩展建议
当前版本使用内存存储,适合学习和演示。实际项目中可考虑:
- 接入 MySQL 或 SQLite,用 JPA 保存数据
- 加入用户登录功能,区分管理员和普通用户
- 提供预约查询或取消功能
- 用 JavaScript 增强前端体验,比如动态加载可用时间
- 部署到 Tomcat 或云服务器(如阿里云、腾讯云)
基本上就这些。用 Java 开发小型预约系统不复杂但容易忽略细节,比如时间冲突判断和输入验证。从简单做起,逐步迭代功能,就能做出实用的小工具。
以上就是如何用Java开发小型在线预约系统的详细内容,更多请关注php中文网其它相关文章!