idea实现学生信息管理系统
时间: 2023-12-28 13:03:21 浏览: 127
根据提供的引用内容,可以使用以下步骤在IntelliJ IDEA中实现学生信息管理系统:
1. 创建一个新的Maven项目,并添加SpringMVC、Spring和MyBatis依赖项。
2. 创建一个数据库并设计学生信息表,包括学生ID、姓名、性别、年龄、班级等字段。
3. 创建一个Java类来表示学生信息,并使用MyBatis框架将其映射到数据库表中。
4. 创建一个SpringMVC控制器类来处理HTTP请求,并使用Spring框架将其注入到应用程序上下文中。
5. 创建一个JSP页面来显示学生信息,并使用JSTL标签库来访问控制器中的数据。
6. 使用过滤器拦截器来验证用户是否登录,并使用MVC模式分层来组织应用程序。
以下是一个简单的示例代码,用于在IntelliJ IDEA中实现学生信息管理系统:
```java
// Student.java
public class Student {
private int id;
private String name;
private String gender;
private int age;
private String className;
// getters and setters
}
// StudentMapper.xml
<mapper namespace="com.example.mapper.StudentMapper">
<resultMap id="studentMap" type="com.example.model.Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="gender" column="gender"/>
<result property="age" column="age"/>
<result property="className" column="class_name"/>
</resultMap>
<select id="getStudentById" resultMap="studentMap">
SELECT * FROM student WHERE id = #{id}
</select>
<select id="getAllStudents" resultMap="studentMap">
SELECT * FROM student
</select>
<insert id="addStudent" parameterType="com.example.model.Student">
INSERT INTO student (name, gender, age, class_name) VALUES (#{name}, #{gender}, #{age}, #{className})
</insert>
<update id="updateStudent" parameterType="com.example.model.Student">
UPDATE student SET name = #{name}, gender = #{gender}, age = #{age}, class_name = #{className} WHERE id = #{id}
</update>
<delete id="deleteStudent" parameterType="int">
DELETE FROM student WHERE id = #{id}
</delete>
</mapper>
// StudentController.java
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/list")
public ModelAndView list() {
List<Student> students = studentService.getAllStudents();
ModelAndView modelAndView = new ModelAndView("student/list");
modelAndView.addObject("students", students);
return modelAndView;
}
@RequestMapping("/add")
public String add() {
return "student/add";
}
@RequestMapping(value = "/doAdd", method = RequestMethod.POST)
public String doAdd(Student student) {
studentService.addStudent(student);
return "redirect:/student/list";
}
@RequestMapping("/edit/{id}")
public ModelAndView edit(@PathVariable int id) {
Student student = studentService.getStudentById(id);
ModelAndView modelAndView = new ModelAndView("student/edit");
modelAndView.addObject("student", student);
return modelAndView;
}
@RequestMapping(value = "/doEdit", method = RequestMethod.POST)
public String doEdit(Student student) {
studentService.updateStudent(student);
return "redirect:/student/list";
}
@RequestMapping("/delete/{id}")
public String delete(@PathVariable int id) {
studentService.deleteStudent(id);
return "redirect:/student/list";
}
}
// StudentService.java
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public List<Student> getAllStudents() {
return studentMapper.getAllStudents();
}
public Student getStudentById(int id) {
return studentMapper.getStudentById(id);
}
public void addStudent(Student student) {
studentMapper.addStudent(student);
}
public void updateStudent(Student student) {
studentMapper.updateStudent(student);
}
public void deleteStudent(int id) {
studentMapper.deleteStudent(id);
}
}
<!-- student/list.jsp -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Class Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.gender}</td>
<td>${student.age}</td>
<td>${student.className}</td>
<td>
<a href="/student/edit/${student.id}">Edit</a>
<a href="/student/delete/${student.id}">Delete</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- student/add.jsp -->
<form action="/student/doAdd" method="post">
<label for="name">Name:</label>
<input type="text" name="name" id="name"/><br/>
<label for="gender">Gender:</label>
<input type="text" name="gender" id="gender"/><br/>
<label for="age">Age:</label>
<input type="text" name="age" id="age"/><br/>
<label for="className">Class Name:</label>
<input type="text" name="className" id="className"/><br/>
<input type="submit" value="Add"/>
</form>
<!-- student/edit.jsp -->
<form action="/student/doEdit" method="post">
<input type="hidden" name="id" value="${student.id}"/>
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="${student.name}"/><br/>
<label for="gender">Gender:</label>
<input type="text" name="gender" id="gender" value="${student.gender}"/><br/>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="${student.age}"/><br/>
<label for="className">Class Name:</label>
<input type="text" name="className" id="className" value="${student.className}"/><br/>
<input type="submit" value="Save"/>
</form>
阅读全文