基于JAVA+Springboot+Thymeleaf前后端分离项目:学生信息管理和新生报到系统设计与实现
由于问题描述不具体,我将提供一个简化版的学生信息管理系统的核心功能代码示例。
实体类 Student.java
public class Student {
private Integer id;
private String name;
private String department;
// 省略getter和setter方法
}
Repository接口 StudentRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Integer> {
}
Service接口 StudentService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public List<Student> findAllStudents() {
return studentRepository.findAll();
}
public void saveStudent(Student student) {
studentRepository.save(student);
}
}
Controller类 StudentController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/students")
public String listStudents(Model model) {
List<Student> students = studentService.findAllStudents();
model.addAttribute("students", students);
return "students";
}
@PostMapping("/addStudent")
public String addStudent(Student student) {
studentService.saveStudent(student);
return "redirect:/students";
}
}
Thymeleaf模板 students.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Student Management</title>
</head>
<body>
<h1>Student Information</h1>
<a href="/addStudent" th:href="@{/addStudent}">Add New Student</a>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Department</th>
</tr>
<tr th:each="student : ${students}">
<td th:text="${student.id}">1</td>
<td th:text="${student.name}">Student Name</td>
<td th:text="${student.department}">Department</td>
</tr>
</table>
</body>
</html>
**添加学生的表单
评论已关闭