用springboot写一个学生信息管理系统
时间: 2023-07-01 10:19:39 浏览: 72
好的,下面是一个简单的学生信息管理系统的示例,使用Spring Boot框架实现:
1. 创建Spring Boot项目
首先,我们需要创建一个Spring Boot项目。你可以使用Spring Initializr或者在IDEA中创建一个新的Spring Boot项目。
2. 添加依赖
在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加MySQL驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 添加MyBatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
```
3. 创建数据库和表
使用MySQL或其他数据库软件创建一个名为"student_db"的数据库,再创建一个名为"student"的表,该表包含以下字段:
```sql
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
4. 创建实体类和Mapper
创建一个名为"Student"的实体类,包含id、name、gender、age、address等属性,并为该实体类创建一个对应的Mapper接口。
```java
public class Student {
private int id;
private String name;
private String gender;
private int age;
private String address;
// 省略getter和setter方法
}
@Mapper
public interface StudentMapper {
List<Student> getAllStudents();
Student getStudentById(int id);
void addStudent(Student student);
void updateStudent(Student student);
void deleteStudent(int id);
}
```
5. 创建Service和Controller
创建一个名为"StudentService"的服务类,实现对学生信息的增删改查操作,并在"StudentController"中使用该服务类进行请求的响应。
```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);
}
}
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/")
public List<Student> getAllStudents() {
return studentService.getAllStudents();
}
@GetMapping("/{id}")
public Student getStudentById(@PathVariable("id") int id) {
return studentService.getStudentById(id);
}
@PostMapping("/")
public void addStudent(@RequestBody Student student) {
studentService.addStudent(student);
}
@PutMapping("/")
public void updateStudent(@RequestBody Student student) {
studentService.updateStudent(student);
}
@DeleteMapping("/{id}")
public void deleteStudent(@PathVariable("id") int id) {
studentService.deleteStudent(id);
}
}
```
6. 修改配置文件
在application.properties或application.yml中,添加以下配置:
```properties
# 数据库配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
# MyBatis配置
mybatis.mapper-locations=classpath:mapper/*.xml
```
7. 测试
启动项目,访问"http://localhost:8080/students/"即可获取所有学生信息;访问"http://localhost:8080/students/{id}"即可获取指定id的学生信息;通过POST请求"http://localhost:8080/students/",可以添加一个新的学生信息;通过PUT请求"http://localhost:8080/students/",可以修改一个学生信息;通过DELETE请求"http://localhost:8080/students/{id}",可以删除一个学生信息。
以上就是一个简单的学生信息管理系统的示例,希望可以帮助到你!