设计和实现一个简单的基于SpringBoot技术实现的学生信息管理系统,系统可以对学生信息进行基本的增加、修改、删除和查询功能;学生信息的字段有主键ID、学号、姓名、性别、出生年月、年级、班级名、系名。程序的实现步骤要写清楚,程序的代码要注释明白。
时间: 2024-06-08 08:08:55 浏览: 110
基于SpringBoot开发的学生管理系统
实现步骤:
1. 创建Spring Boot项目
2. 导入相关依赖
```
<!--Spring Boot Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Boot Mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!--MySQL JDBC Driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--Swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
```
3. 创建数据库
```
CREATE DATABASE student_info;
USE student_info;
CREATE TABLE student (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
student_no VARCHAR(20) NOT NULL,
name VARCHAR(20) NOT NULL,
gender VARCHAR(10) NOT NULL,
birthday DATE NOT NULL,
grade VARCHAR(20) NOT NULL,
class_name VARCHAR(20) NOT NULL,
department VARCHAR(20) NOT NULL
);
```
4. 配置application.yml文件
```
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/student_info?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.entity
swagger:
title: 学生信息管理系统API文档
description: 学生信息管理系统API文档
version: 1.0
contact:
name: Lony
email: lony@example.com
url: https://www.example.com
```
5. 创建实体类
```java
@Data
public class Student {
private int id;
private String studentNo;
private String name;
private String gender;
private Date birthday;
private String grade;
private String className;
private String department;
}
```
注意:使用Lombok的@Data注解可以自动生成getter、setter、toString等方法。
6. 创建Mapper接口
```java
@Mapper
public interface StudentMapper {
List<Student> getAllStudents();
Student getStudentById(int id);
int addStudent(Student student);
int updateStudent(Student student);
int deleteStudent(int id);
}
```
注意:使用@Mapper注解标注Mapper接口,Mybatis会自动生成Mapper实现类。
7. 创建Mapper的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.demo.mapper.StudentMapper">
<select id="getAllStudents" resultType="com.example.demo.entity.Student">
SELECT * FROM student
</select>
<select id="getStudentById" parameterType="int" resultType="com.example.demo.entity.Student">
SELECT * FROM student WHERE id = #{id}
</select>
<insert id="addStudent" parameterType="com.example.demo.entity.Student" useGeneratedKeys="true" keyProperty="id">
INSERT INTO student(student_no, name, gender, birthday, grade, class_name, department)
VALUES (#{studentNo}, #{name}, #{gender}, #{birthday}, #{grade}, #{className}, #{department})
</insert>
<update id="updateStudent" parameterType="com.example.demo.entity.Student">
UPDATE student
SET student_no = #{studentNo}, name = #{name}, gender = #{gender}, birthday = #{birthday}, grade = #{grade}, class_name = #{className}, department = #{department}
WHERE id = #{id}
</update>
<delete id="deleteStudent" parameterType="int">
DELETE FROM student WHERE id = #{id}
</delete>
</mapper>
```
注意:使用Mybatis的XML文件来定义SQL语句。
8. 创建Service接口和实现类
```java
public interface StudentService {
List<Student> getAllStudents();
Student getStudentById(int id);
int addStudent(Student student);
int updateStudent(Student student);
int deleteStudent(int id);
}
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> getAllStudents() {
return studentMapper.getAllStudents();
}
@Override
public Student getStudentById(int id) {
return studentMapper.getStudentById(id);
}
@Override
public int addStudent(Student student) {
return studentMapper.addStudent(student);
}
@Override
public int updateStudent(Student student) {
return studentMapper.updateStudent(student);
}
@Override
public int deleteStudent(int id) {
return studentMapper.deleteStudent(id);
}
}
```
注意:使用@Service注解标注Service实现类,通过@Autowired注解注入Mapper。
9. 创建Controller
```java
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/getAllStudents")
public List<Student> getAllStudents() {
return studentService.getAllStudents();
}
@GetMapping("/getStudentById/{id}")
public Student getStudentById(@PathVariable("id") int id) {
return studentService.getStudentById(id);
}
@PostMapping("/addStudent")
public String addStudent(@RequestBody Student student) {
int result = studentService.addStudent(student);
if (result > 0) {
return "添加成功";
} else {
return "添加失败";
}
}
@PutMapping("/updateStudent")
public String updateStudent(@RequestBody Student student) {
int result = studentService.updateStudent(student);
if (result > 0) {
return "修改成功";
} else {
return "修改失败";
}
}
@DeleteMapping("/deleteStudent/{id}")
public String deleteStudent(@PathVariable("id") int id) {
int result = studentService.deleteStudent(id);
if (result > 0) {
return "删除成功";
} else {
return "删除失败";
}
}
}
```
注意:使用@RestController注解标注Controller,通过@Autowired注解注入Service。
10. 启动应用,访问http://localhost:8080/swagger-ui.html可以查看API文档。
代码注释见代码:https://github.com/LonyYoung/Spring-Boot-Demo/tree/master/student-info-management-system
阅读全文