学生管理系统javaee
时间: 2024-01-06 19:25:15 浏览: 92
学生管理系统是一个常见的JavaEE项目,用于管理学生的信息。下面是一个简单的学生管理系统的示例代码:
1. 创建控制器(StudentController.java):
```java
package com.example.demo.controller;
import com.example.demo.model.Student;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/students")
public class StudentController {
private List<Student> studentList = new ArrayList<>();
@GetMapping("/")
public List<Student> getAllStudents() {
return studentList;
}
@PostMapping("/")
public Student addStudent(@RequestBody Student student) {
studentList.add(student);
return student;
}
@GetMapping("/{id}")
public Student getStudentById(@PathVariable int id) {
for (Student student : studentList) {
if (student.getStuid() == id) {
return student;
}
}
return null;
}
@PutMapping("/{id}")
public Student updateStudent(@PathVariable int id, @RequestBody Student updatedStudent) {
for (Student student : studentList) {
if (student.getStuid() == id) {
student.setStuname(updatedStudent.getStuname());
return student;
}
}
return null;
}
@DeleteMapping("/{id}")
public void deleteStudent(@PathVariable int id) {
studentList.removeIf(student -> student.getStuid() == id);
}
}
```
2. 创建模型类(Student.java):
```java
package com.example.demo.model;
public class Student {
private int stuid;
private String stuname;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
@Override
public String toString() {
return "Student{" +
"stuid=" + stuid +
", stuname='" + stuname + '\'' +
'}';
}
}
```
3. 创建Mapper类(StudentMapper.java):
在问题中没有提到具体的Mapper类,所以无法提供相关代码。
以上是一个简单的学生管理系统的JavaEE示例代码,包括控制器和模型类。你可以根据自己的需求进行修改和扩展。