java+springboot+mysql日程管理系统设计与实现
由于篇幅所限,以下仅展示如何使用Spring Boot和MySQL创建一个简单的日程管理系统的核心函数。
// 导入Spring Boot相关依赖
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
// 导入MySQL相关依赖
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.sql.SQLException;
@Controller
public class ScheduleController {
// 注入数据源
@Autowired
private DataSource dataSource;
// 展示日程页面
@GetMapping("/schedule")
public String schedule(Model model) throws SQLException {
List<Schedule> schedules = getSchedules();
model.addAttribute("schedules", schedules);
return "schedule"; // 对应的Thymeleaf模板名称
}
// 添加日程
@PostMapping("/addSchedule")
public String addSchedule(Schedule schedule) throws SQLException {
insertSchedule(schedule);
return "redirect:/schedule";
}
// 获取所有日程
private List<Schedule> getSchedules() throws SQLException {
List<Schedule> schedules = new ArrayList<>();
Connection connection = dataSource.getConnection();
String sql = "SELECT * FROM schedule";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Schedule schedule = new Schedule();
// 假设数据库中有id, title, description, date等字段
schedule.setId(resultSet.getInt("id"));
schedule.setTitle(resultSet.getString("title"));
schedule.setDescription(resultSet.getString("description"));
schedule.setDate(resultSet.getString("date"));
schedules.add(schedule);
}
return schedules;
}
// 插入一个新日程
private void insertSchedule(Schedule schedule) throws SQLException {
Connection connection = dataSource.getConnection();
String sql = "INSERT INTO schedule (title, description, date) VALUES (?, ?, ?)";
PreparedStatement state
评论已关闭