学生管理界面实现(html与Java后端相连)后台应用Spring框架
由于原始代码已经包含了一个完整的Spring Boot后端应用,并且前端部分已经给出,因此这里只需要提供一个简化的Spring Boot后端应用的例子。
// StudentController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/students")
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public List<Student> getAllStudents() {
return studentService.findAll();
}
@PostMapping
public Student addStudent(@RequestBody Student student) {
return studentService.save(student);
}
@GetMapping("/{id}")
public Student getStudentById(@PathVariable(value = "id") Long id) {
return studentService.findById(id);
}
@PutMapping("/{id}")
public Student updateStudent(@PathVariable(value = "id") Long id, @RequestBody Student student) {
return studentService.update(id, student);
}
@DeleteMapping("/{id}")
public String deleteStudent(@PathVariable(value = "id") Long id) {
studentService.deleteById(id);
return "Student with id: " + id + " deleted successfully!";
}
}
// StudentService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public List<Student> findAll() {
return studentRepository.findAll();
}
public Student save(Student student) {
return studentRepository.save(student);
}
public Student findById(Long id) {
Optional<Stud
评论已关闭