用idea,利用web+ssm框架编译出学生信息管理系统,包括具体代码
时间: 2023-07-16 12:13:19 浏览: 130
抱歉,由于篇幅限制,无法在此处提供完整的学生信息管理系统的代码。不过,以下是用Web+SSM框架编写学生信息管理系统的基本步骤:
1. 创建Maven项目并添加相关依赖。
2. 配置Spring MVC框架,包括配置DispatcherServlet、ViewResolver、HandlerMapping等。
3. 配置MyBatis框架,包括配置SqlSessionFactory、SqlSessionTemplate等。
4. 创建数据库表以存储学生信息,例如学生姓名、学号、班级等。
5. 创建实体类Student,包含与数据库表对应的属性。
6. 创建Mapper接口和Mapper.xml文件,用于实现与数据库的交互。
7. 创建Service层和Controller层,分别实现业务逻辑和控制层操作。
8. 创建前端页面,包括展示学生信息、添加学生信息、修改学生信息、删除学生信息等功能。
9. 部署项目并运行,测试功能是否正常。
以下是示例代码:
1. Maven依赖
```xml
<dependencies>
<!--Spring MVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!--JSP-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
```
2. Spring MVC配置
```xml
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置ViewResolver-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置HandlerMapping-->
<mvc:annotation-driven/>
<context:component-scan base-package="com.example.controller"/>
```
3. MyBatis配置
```xml
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
</bean>
<!--配置SqlSessionTemplate-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
```
4. 数据库表
```sql
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20),
number VARCHAR(10),
class_name VARCHAR(20)
);
```
5. 实体类Student
```java
public class Student {
private int id;
private String name;
private String number;
private String className;
//getter和setter方法
}
```
6. Mapper接口和Mapper.xml文件
```java
public interface StudentMapper {
List<Student> getAllStudents();
Student getStudentById(int id);
void addStudent(Student student);
void updateStudent(Student student);
void deleteStudent(int id);
}
```
```xml
<mapper namespace="com.example.mapper.StudentMapper">
<select id="getAllStudents" resultType="com.example.entity.Student">
SELECT * FROM student
</select>
<select id="getStudentById" parameterType="int" resultType="com.example.entity.Student">
SELECT * FROM student WHERE id=#{id}
</select>
<insert id="addStudent" parameterType="com.example.entity.Student">
INSERT INTO student (name, number, class_name)
VALUES (#{name}, #{number}, #{className})
</insert>
<update id="updateStudent" parameterType="com.example.entity.Student">
UPDATE student SET name=#{name}, number=#{number}, class_name=#{className} WHERE id=#{id}
</update>
<delete id="deleteStudent" parameterType="int">
DELETE FROM student WHERE id=#{id}
</delete>
</mapper>
```
7. Service层和Controller层
```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);
}
}
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/list")
public String list(Model model) {
List<Student> students = studentService.getAllStudents();
model.addAttribute("students", students);
return "studentList";
}
@RequestMapping("/toAdd")
public String toAdd() {
return "addStudent";
}
@RequestMapping("/add")
public String add(Student student) {
studentService.addStudent(student);
return "redirect:/student/list";
}
@RequestMapping("/toUpdate")
public String toUpdate(int id, Model model) {
Student student = studentService.getStudentById(id);
model.addAttribute("student", student);
return "updateStudent";
}
@RequestMapping("/update")
public String update(Student student) {
studentService.updateStudent(student);
return "redirect:/student/list";
}
@RequestMapping("/delete")
public String delete(int id) {
studentService.deleteStudent(id);
return "redirect:/student/list";
}
}
```
8. 前端页面
studentList.jsp
```html
<table>
<tr>
<th>ID</th>
<th>姓名</th>
<th>学号</th>
<th>班级</th>
<th>操作</th>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.number}</td>
<td>${student.className}</td>
<td>
<a href="${pageContext.request.contextPath}/student/toUpdate?id=${student.id}">修改</a>
<a href="${pageContext.request.contextPath}/student/delete?id=${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<a href="${pageContext.request.contextPath}/student/toAdd">添加学生</a>
```
addStudent.jsp
```html
<form action="${pageContext.request.contextPath}/student/add" method="post">
<label>姓名:</label><input type="text" name="name"><br>
<label>学号:</label><input type="text" name="number"><br>
<label>班级:</label><input type="text" name="className"><br>
<input type="submit" value="添加">
</form>
```
updateStudent.jsp
```html
<form action="${pageContext.request.contextPath}/student/update" method="post">
<input type="hidden" name="id" value="${student.id}">
<label>姓名:</label><input type="text" name="name" value="${student.name}"><br>
<label>学号:</label><input type="text" name="number" value="${student.number}"><br>
<label>班级:</label><input type="text" name="className" value="${student.className}"><br>
<input type="submit" value="修改">
</form>
```
9. 运行项目
在浏览器中输入http://localhost:8080/student/list,即可进入学生信息列表页。
阅读全文