书写使用springboot和Vue建立学生选课系统的代码

时间: 2024-01-08 17:04:14 浏览: 29
这个需求涉及到前后端的开发,需要分别建立Spring Boot后端和Vue前端的工程。下面给出一个简单的示例代码: Spring Boot后端: 1. 创建Spring Boot工程 2. 添加依赖 ```xml <!--Spring Boot Web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Spring Boot Data JPA--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--MySQL驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> ``` 3. 配置数据库连接 ```properties spring.datasource.url=jdbc:mysql://localhost:3306/db_example spring.datasource.username=springuser spring.datasource.password=ThePassword spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 4. 定义实体类 ```java @Entity @Table(name="student") public class Student { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private String name; private String gender; private String major; private Integer grade; // Getters and Setters } ``` 5. 定义Repository接口 ```java @Repository public interface StudentRepository extends JpaRepository<Student, Integer> { } ``` 6. 定义Controller ```java @RestController @RequestMapping("/api") public class StudentController { @Autowired private StudentRepository studentRepository; @GetMapping("/students") public List<Student> getStudents() { return studentRepository.findAll(); } @PostMapping("/students") public Student addStudent(@RequestBody Student student) { return studentRepository.save(student); } @DeleteMapping("/students/{id}") public void deleteStudent(@PathVariable Integer id) { studentRepository.deleteById(id); } } ``` 7. 运行工程 Vue前端: 1. 创建Vue工程 2. 安装axios ```bash npm install axios --save ``` 3. 编写组件 ```vue <template> <div class="container"> <h2>学生选课系统</h2> <div class="form-group"> <label for="name">姓名:</label> <input type="text" class="form-control" v-model="student.name" id="name"> </div> <div class="form-group"> <label for="gender">性别:</label> <input type="text" class="form-control" v-model="student.gender" id="gender"> </div> <div class="form-group"> <label for="major">专业:</label> <input type="text" class="form-control" v-model="student.major" id="major"> </div> <div class="form-group"> <label for="grade">年级:</label> <input type="number" class="form-control" v-model="student.grade" id="grade"> </div> <button class="btn btn-primary" @click="addStudent">添加学生</button> <table class="table mt-3"> <thead> <tr> <th>编号</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.gender}}</td> <td>{{student.major}}</td> <td>{{student.grade}}</td> <td> <button class="btn btn-danger" @click="deleteStudent(student.id)">删除</button> </td> </tr> </tbody> </table> </div> </template> <script> import axios from 'axios'; export default { data() { return { student: { name: '', gender: '', major: '', grade: '' }, students: [] } }, mounted() { this.getStudents(); }, methods: { getStudents() { axios.get('/api/students') .then(response => { this.students = response.data; }); }, addStudent() { axios.post('/api/students', this.student) .then(response => { this.students.push(response.data); this.student.name = ''; this.student.gender = ''; this.student.major = ''; this.student.grade = ''; }); }, deleteStudent(id) { axios.delete(`/api/students/${id}`) .then(() => { this.getStudents(); }); } } }; </script> ``` 4. 运行工程 以上是一个简单的示例代码,具体实现需要根据实际需求进行调整。

相关推荐

最新推荐

recommend-type

基于SpringBoot和Vue框架的电子招投标系统的设计与实现.pdf

本 文 围 绕 电 子 招 投 标 系 统 的 设 计 与 实 现 , 进 行 了...(3)基于需求分析和项目 整体设计, 着重阐述了电子招投标系统的工作流 管理、 项目 管理、 招标管理、 投标管理和系统管理等相关模块的实现。
recommend-type

Springboot vue导出功能实现代码

主要介绍了Springboot vue导出功能实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

使用springboot结合vue实现sso单点登录

主要为大家详细介绍了如何使用springboot+vue实现sso单点登录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

websocket在springboot+vue中的使用教程

主要介绍了websocket在springboot+vue中的使用教程,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

vue+springboot图片上传和显示的示例代码

主要介绍了vue+springboot图片上传和显示的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。