用 java框架 springboot 做一个添加修改删除学生信息管理系统,
时间: 2023-06-04 18:05:50 浏览: 77
这个问题属于技术问题,我可以为您提供相应的解答和帮助。Springboot 是一个优秀的 Java 开源框架之一,适用于构建Web应用程序和微服务。它提供了一组开箱即用的工具和插件,可以帮助我们快速创建和部署应用程序。对于学生信息管理系统,您可以使用 Springboot 结合数据库技术实现添加、修改和删除的功能。
相关问题
写一个springboot+vue框架的学生信息管理系统,使用java语言
好的,我可以为您提供一个简单的学生信息管理系统的示例,使用Spring Boot和Vuejs前端框架。以下是实现步骤:
1. 创建Spring Boot项目
使用Spring Initializr创建一个新的Spring Boot项目。在“Dependencies”选项卡中添加以下依赖项:
- Spring Web
- Spring Data JPA
- H2 Database
- Lombok
2. 创建实体类
创建一个名为“Student”的实体类,该类包含以下属性:id、姓名、年龄、性别和成绩。
```java
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Integer age;
private String gender;
private Double score;
}
```
3. 创建JPA Repository
创建一个名为“StudentRepository”的JPA存储库,用于与数据库交互。
```java
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
}
```
4. 创建REST控制器
创建一个名为“StudentController”的REST控制器,用于处理来自前端的HTTP请求。
```java
@RestController
@RequestMapping("/api/students")
@AllArgsConstructor
public class StudentController {
private final StudentRepository studentRepository;
@GetMapping
public List<Student> getAllStudents() {
return studentRepository.findAll();
}
@PostMapping
public void addStudent(@RequestBody Student student) {
studentRepository.save(student);
}
@PutMapping("/{id}")
public void updateStudent(@PathVariable Long id, @RequestBody Student student) {
Student existingStudent = studentRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Student not found"));
existingStudent.setName(student.getName());
existingStudent.setAge(student.getAge());
existingStudent.setGender(student.getGender());
existingStudent.setScore(student.getScore());
studentRepository.save(existingStudent);
}
@DeleteMapping("/{id}")
public void deleteStudent(@PathVariable Long id) {
studentRepository.deleteById(id);
}
}
```
5. 创建Vuejs前端
创建一个名为“StudentList.vue”的Vuejs组件,用于显示学生列表和添加/编辑/删除学生。
```vue
<template>
<div>
<h1>学生信息管理系统</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>成绩</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="student in students" :key="student.id">
<td>{{ student.id }}</td>
<td>{{ student.name }}</td>
<td>{{ student.age }}</td>
<td>{{ student.gender }}</td>
<td>{{ student.score }}</td>
<td>
<button @click="editStudent(student)">编辑</button>
<button @click="deleteStudent(student)">删除</button>
</td>
</tr>
</tbody>
</table>
<div>
<h2>{{ isNewStudent ? '添加学生' : '编辑学生' }}</h2>
<form @submit.prevent="submitForm">
<label>姓名:</label>
<input type="text" v-model="form.name" required>
<br>
<label>年龄:</label>
<input type="number" v-model="form.age" required>
<br>
<label>性别:</label>
<select v-model="form.gender" required>
<option value="男">男</option>
<option value="女">女</option>
</select>
<br>
<label>成绩:</label>
<input type="number" v-model="form.score" required>
<br>
<button type="submit">{{ isNewStudent ? '添加' : '保存' }}</button>
<button type="button" @click="cancelForm">取消</button>
</form>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
students: [],
form: {
name: '',
age: '',
gender: '',
score: ''
},
isNewStudent: true
}
},
mounted() {
this.fetchStudents();
},
methods: {
fetchStudents() {
axios.get('/api/students')
.then(response => {
this.students = response.data;
})
.catch(error => {
console.log(error);
});
},
editStudent(student) {
this.form = Object.assign({}, student);
this.isNewStudent = false;
},
deleteStudent(student) {
axios.delete(`/api/students/${student.id}`)
.then(response => {
this.students = this.students.filter(s => s.id !== student.id);
})
.catch(error => {
console.log(error);
});
},
submitForm() {
if (this.isNewStudent) {
axios.post('/api/students', this.form)
.then(response => {
this.students.push(response.data);
this.form = {
name: '',
age: '',
gender: '',
score: ''
};
})
.catch(error => {
console.log(error);
});
} else {
axios.put(`/api/students/${this.form.id}`, this.form)
.then(response => {
const index = this.students.findIndex(s => s.id === this.form.id);
this.students.splice(index, 1, response.data);
this.form = {
name: '',
age: '',
gender: '',
score: ''
};
this.isNewStudent = true;
})
.catch(error => {
console.log(error);
});
}
},
cancelForm() {
this.form = {
name: '',
age: '',
gender: '',
score: ''
};
this.isNewStudent = true;
}
}
}
</script>
```
6. 运行应用程序
启动Spring Boot应用程序并访问http://localhost:8080/。您应该能够看到一个包含学生列表的页面,您可以添加,编辑和删除学生。
以上就是一个简单的学生信息管理系统的示例,使用Spring Boot和Vuejs前端框架。希望对您有所帮助!
用springboot写一个学生信息管理系统
好的,下面是一个简单的学生信息管理系统的示例,使用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}",可以删除一个学生信息。
以上就是一个简单的学生信息管理系统的示例,希望可以帮助到你!