使用Spring框架写学生信息管理系统
时间: 2023-10-24 14:24:16 浏览: 89
学生信息管理系统是一个比较典型的MVC应用,使用Spring框架可以快速地实现应用程序的开发。下面是一个简单的学生信息管理系统的实现示例:
1. 首先,定义一个学生类,包含学生的基本信息:
```java
public class Student {
private int id;
private String name;
private int age;
private String gender;
//getters and setters
}
```
2. 创建一个控制器类,处理用户请求并调用相应的服务方法:
```java
@Controller
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping
public String getAllStudents(Model model) {
List<Student> students = studentService.getAllStudents();
model.addAttribute("students", students);
return "students";
}
@GetMapping("/{id}")
public String getStudentById(@PathVariable int id, Model model) {
Student student = studentService.getStudentById(id);
model.addAttribute("student", student);
return "student";
}
@PostMapping
public String addStudent(@ModelAttribute Student student) {
studentService.addStudent(student);
return "redirect:/students";
}
@PutMapping("/{id}")
public String updateStudent(@PathVariable int id, @ModelAttribute Student student) {
student.setId(id);
studentService.updateStudent(student);
return "redirect:/students";
}
@DeleteMapping("/{id}")
public String deleteStudent(@PathVariable int id) {
studentService.deleteStudent(id);
return "redirect:/students";
}
}
```
3. 创建一个服务类,实现对学生信息的增删改查操作:
```java
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public List<Student> getAllStudents() {
return studentDao.getAllStudents();
}
@Override
public Student getStudentById(int id) {
return studentDao.getStudentById(id);
}
@Override
public void addStudent(Student student) {
studentDao.addStudent(student);
}
@Override
public void updateStudent(Student student) {
studentDao.updateStudent(student);
}
@Override
public void deleteStudent(int id) {
studentDao.deleteStudent(id);
}
}
```
4. 创建一个数据访问对象类,实现对学生信息的持久化操作:
```java
@Repository
public class StudentDaoImpl implements StudentDao {
private static final Map<Integer, Student> students = new HashMap<>();
@Override
public List<Student> getAllStudents() {
return new ArrayList<>(students.values());
}
@Override
public Student getStudentById(int id) {
return students.get(id);
}
@Override
public void addStudent(Student student) {
students.put(student.getId(), student);
}
@Override
public void updateStudent(Student student) {
students.put(student.getId(), student);
}
@Override
public void deleteStudent(int id) {
students.remove(id);
}
}
```
5. 配置Spring框架,定义相关组件的依赖关系:
```xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.example"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="studentDao" class="com.example.dao.StudentDaoImpl"/>
<bean id="studentService" class="com.example.service.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
</beans>
```
6. 编写JSP页面,展示学生信息和处理用户输入:
```jsp
<!-- students.jsp -->
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.gender}</td>
</tr>
</c:forEach>
</table>
<form action="/students" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br>
<label for="age">Age:</label>
<input type="text" id="age" name="age"><br>
<label for="gender">Gender:</label>
<select id="gender" name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select><br>
<button type="submit">Add Student</button>
</form>
```
```jsp
<!-- student.jsp -->
<h1>${student.name}</h1>
<p>ID: ${student.id}</p>
<p>Age: ${student.age}</p>
<p>Gender: ${student.gender}</p>
<form action="/students/${student.id}" method="post">
<input type="hidden" name="_method" value="put">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="${student.name}"><br>
<label for="age">Age:</label>
<input type="text" id="age" name="age" value="${student.age}"><br>
<label for="gender">Gender:</label>
<select id="gender" name="gender">
<option value="male" ${student.gender == "male" ? "selected" : ""}>Male</option>
<option value="female" ${student.gender == "female" ? "selected" : ""}>Female</option>
</select><br>
<button type="submit">Update Student</button>
</form>
<form action="/students/${student.id}" method="post">
<input type="hidden" name="_method" value="delete">
<button type="submit">Delete Student</button>
</form>
```
7. 启动应用程序,访问http://localhost:8080/students即可查看学生列表,点击学生名称可以查看学生详细信息,点击Add Student可以添加新的学生,点击Update Student可以更新学生信息,点击Delete Student可以删除学生记录。
阅读全文