帮写一个基于SSM的学生信息管理
时间: 2023-07-06 14:42:09 浏览: 179
好的,下面是一个基于SSM的学生信息管理系统的示例代码,包括了注册、登录、增加、修改和查询等功能:
1. 创建数据库和表
首先需要在MySQL中创建一个名为`student_management`的数据库,然后创建一个名为`student`的表,包含以下字段:
```sql
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`age` int(11) NOT NULL,
`gender` varchar(255) NOT NULL,
`major` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
2. 创建Java项目
使用Eclipse等IDE创建一个Maven项目,然后在`pom.xml`中添加SSM依赖:
```xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
```
3. 配置Spring
在`src/main/resources`目录下创建一个名为`applicationContext.xml`的Spring配置文件,添加以下内容:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.example.controller" />
<mvc:annotation-driven />
<tx:annotation-driven />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/student_management?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="password" />
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
</bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.example.entity" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.dao" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
```
其中`dataSource`为数据库连接池配置,`sqlSessionFactory`为MyBatis配置,`transactionManager`为事务管理配置。
4. 创建实体类
在`src/main/java`下创建一个名为`com.example.entity`的包,然后创建一个名为`Student`的Java类,代码如下:
```java
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String major;
// getter and setter methods
}
```
5. 创建Mapper接口
在`src/main/java`下创建一个名为`com.example.dao`的包,然后创建一个名为`StudentMapper`的Java接口,代码如下:
```java
public interface StudentMapper {
public void addStudent(Student student);
public void updateStudent(Student student);
public void deleteStudent(int id);
public Student getStudentById(int id);
public List<Student> getAllStudents();
}
```
6. 创建Mapper映射文件
在`src/main/resources`下创建一个名为`StudentMapper.xml`的文件,代码如下:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.StudentMapper">
<resultMap id="studentResultMap" type="com.example.entity.Student">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="gender" column="gender" />
<result property="major" column="major" />
</resultMap>
<insert id="addStudent" parameterType="com.example.entity.Student">
INSERT INTO student(name, age, gender, major)
VALUES(#{name}, #{age}, #{gender}, #{major})
</insert>
<update id="updateStudent" parameterType="com.example.entity.Student">
UPDATE student SET name=#{name}, age=#{age}, gender=#{gender},
major=#{major} WHERE id=#{id}
</update>
<delete id="deleteStudent" parameterType="int">
DELETE FROM student WHERE id=#{id}
</delete>
<select id="getStudentById" resultMap="studentResultMap" parameterType="int">
SELECT * FROM student WHERE id=#{id}
</select>
<select id="getAllStudents" resultMap="studentResultMap">
SELECT * FROM student
</select>
</mapper>
```
7. 创建Service接口和实现类
在`src/main/java`下创建一个名为`com.example.service`的包,然后创建一个名为`StudentService`的Java接口,代码如下:
```java
public interface StudentService {
public void addStudent(Student student);
public void updateStudent(Student student);
public void deleteStudent(int id);
public Student getStudentById(int id);
public List<Student> getAllStudents();
}
```
然后创建一个名为`StudentServiceImpl`的Java类,代码如下:
```java
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public void addStudent(Student student) {
studentMapper.addStudent(student);
}
@Override
public void updateStudent(Student student) {
studentMapper.updateStudent(student);
}
@Override
public void deleteStudent(int id) {
studentMapper.deleteStudent(id);
}
@Override
public Student getStudentById(int id) {
return studentMapper.getStudentById(id);
}
@Override
public List<Student> getAllStudents() {
return studentMapper.getAllStudents();
}
}
```
其中`@Service`用于声明该类为Spring的一个服务组件,`@Transactional`用于开启事务管理。
8. 创建Controller
在`src/main/java`下创建一个名为`com.example.controller`的包,然后创建一个名为`StudentController`的Java类,代码如下:
```java
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/add")
public String addStudent(@RequestParam("name") String name,
@RequestParam("age") int age,
@RequestParam("gender") String gender,
@RequestParam("major") String major) {
Student student = new Student();
student.setName(name);
student.setAge(age);
student.setGender(gender);
student.setMajor(major);
studentService.addStudent(student);
return "redirect:/student/list";
}
@RequestMapping("/update")
public String updateStudent(@RequestParam("id") int id,
@RequestParam("name") String name,
@RequestParam("age") int age,
@RequestParam("gender") String gender,
@RequestParam("major") String major) {
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setGender(gender);
student.setMajor(major);
studentService.updateStudent(student);
return "redirect:/student/list";
}
@RequestMapping("/delete")
public String deleteStudent(@RequestParam("id") int id) {
studentService.deleteStudent(id);
return "redirect:/student/list";
}
@RequestMapping("/list")
public String getAllStudents(Model model) {
List<Student> students = studentService.getAllStudents();
model.addAttribute("students", students);
return "student_list";
}
@RequestMapping("/edit")
public String getStudentById(@RequestParam("id") int id, Model model) {
Student student = studentService.getStudentById(id);
model.addAttribute("student", student);
return "student_edit";
}
}
```
其中`@Controller`用于声明该类为Spring的一个控制器组件,`@RequestMapping`用于指定Controller的访问路径。
9. 创建视图文件
在`src/main/webapp/WEB-INF/views`下创建一个名为`student_list.jsp`的视图文件,代码如下:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<h1>学生信息列表</h1>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</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.age}</td>
<td>${student.gender}</td>
<td>${student.major}</td>
<td>
<a href="/student/edit?id=${student.id}">编辑</a>
<a href="/student/delete?id=${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<a href="/student/edit">添加学生</a>
</body>
</html>
```
在`src/main/webapp/WEB-INF/views`下创建一个名为`student_edit.jsp`的视图文件,代码如下:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<h1>学生信息编辑</h1>
<form action="/student/${student.id == null ? 'add' : '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="age"
value="${student.age}" /><br>
<label>性别:</label><select name="gender">
<c:if test="${student.gender eq '男'}">
<option value="男" selected="selected">男</option>
<option value="女">女</option>
</c:if>
<c:if test="${student.gender eq '女'}">
<option value="男">男</option>
<option value="女" selected="selected">女</option>
</c:if>
</select><br>
<label>专业:</label><input type="text" name="major"
value="${student.major}" /><br>
<input type="submit" value="保存" />
</form>
<br>
<a href="/student/list">返回列表</a>
</body>
</html>
```
10. 启动应用程序
在Tomcat等Web服务器中部署该应用程序,然后访问`http://localhost:8080/student/list`即可进入学生信息列表页面,可以进行增加、编辑、删除等操作。
阅读全文