当前位置:   article > 正文

Vue入门项目:学生管理系统之班级管理 【含源码】_vue3学生管理系统源码

vue3学生管理系统源码

目录

1.概述

2.表结构

3.搭建环境

        3.1后端环境

                3.1.1项目名:student

                3.1.2坐标

                3.1.3核心配置文件

                3.1.4启动类

                3.1.5封装类

                3.1.6配置类(可选)

        3.2前端环境

                3.2.1拷贝静态资源

                3.2.2vs打开资源

4.班级管理

        4.1JavaBean:Class

        4.2查询所有

                4.2.1后端

                4.2.2前端:axios+then

                4.2.3前端:async+await

        4.3添加班级

                4.3.1需求:

                4.3.2后端

                4.3.3前端

        4.4修改班级

                4.4.1需求:

                4.4.2后端

                4.4.3前端

        4.5删除班级

                4.5.1分析

                4.5.2后端

                4.5.3前端

                4.5.4完善后端:级联删除


1.概述

  • 学生管理系统

        ——班级管理:添加班级、修改班级、查询班级、删除班级

        ——学生管理:添加学生、修改学生、查询学生(含条件)、删除学生

2.表结构

  1. create database ssm_db3;
  2. use ssm_db3;
  3. -- 班级表
  4. create table tb_class(
  5. `c_id` varchar(32) primary key comment '班级ID',
  6. `c_name` varchar(50) comment '班级名称',
  7. `desc` varchar(200) comment '班级描述'
  8. );
  9. insert into tb_class(`c_id`,`c_name`,`desc`) values('c001','Java12班','。。。。');
  10. insert into tb_class(`c_id`,`c_name`,`desc`) values('c002','Java34班','。。。。');
  11. # 学生表
  12. create table tb_student(
  13. s_id varchar(32) primary key comment '学生ID',
  14. sname varchar(50) comment '姓名',
  15. age int comment '年龄',
  16. birthday datetime comment '生日',
  17. gender char(1) comment '性别',
  18. c_id varchar(32)
  19. );
  20. alter table tb_student add constraint foreign key (c_id) references tb_class (c_id);
  21. insert into tb_student(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`) values('s001','赵三',19,'2001-01-17','1','c001');
  22. insert into tb_student(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`) values('s002','钱四',19,'2001-05-16','1','c001');
  23. insert into tb_student(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`) values('s003','孙五',18,'2002-03-15','1','c001');
  24. insert into tb_student(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`) values('s004','李三',19,'2001-04-14','0','c002');
  25. insert into tb_student(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`) values('s005','周四',19,'2001-02-13','0','c002');
  26. insert into tb_student(`s_id`,`sname`,`age`,`birthday`,`gender`,`c_id`) values('s006','王五',18,'2002-06-12','1','c002');

3.搭建环境

        3.1后端环境

                3.1.1项目名:day21_student

                3.1.2坐标

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.czxy.ssm</groupId>
  7. <artifactId>day21_student</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <!-- 1 确定spring boot的版本-->
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.2.5.RELEASE</version>
  14. </parent>
  15. <!--2 确定版本-->
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <java.version>1.8</java.version>
  19. <spring-cloud-release.version>Hoxton.SR3</spring-cloud-release.version>
  20. <nacos.version>1.1.0</nacos.version>
  21. <alibaba.cloud.version>2.2.1.RELEASE</alibaba.cloud.version>
  22. <mybatis.starter.version>1.3.2</mybatis.starter.version>
  23. <mapper.starter.version>2.0.2</mapper.starter.version>
  24. <pageHelper.starter.version>1.2.5</pageHelper.starter.version>
  25. <mysql.version>5.1.32</mysql.version>
  26. <durid.starter.version>1.1.10</durid.starter.version>
  27. <mybatis.plus.version>3.4.0</mybatis.plus.version>
  28. <swagger.version>2.7.0</swagger.version>
  29. <jwt.jjwt.version>0.9.0</jwt.jjwt.version>
  30. <jwt.joda.version>2.9.7</jwt.joda.version>
  31. <beanutils.version>1.9.3</beanutils.version>
  32. </properties>
  33. <!-- 3 锁定版本-->
  34. <dependencyManagement>
  35. <dependencies>
  36. <!-- sprig cloud-->
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-dependencies</artifactId>
  40. <version>${spring-cloud-release.version}</version>
  41. <type>pom</type>
  42. <scope>import</scope>
  43. </dependency>
  44. <!--nacos -->
  45. <dependency>
  46. <groupId>com.alibaba.nacos</groupId>
  47. <artifactId>nacos-client</artifactId>
  48. <version>${nacos.version}</version>
  49. </dependency>
  50. <!--nacos cloud 发现 -->
  51. <dependency>
  52. <groupId>com.alibaba.cloud</groupId>
  53. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  54. <version>${alibaba.cloud.version}</version>
  55. </dependency>
  56. <!--nacos cloud 配置 -->
  57. <dependency>
  58. <groupId>com.alibaba.cloud</groupId>
  59. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  60. <version>${alibaba.cloud.version}</version>
  61. </dependency>
  62. <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel -->
  63. <dependency>
  64. <groupId>com.alibaba.cloud</groupId>
  65. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  66. <version>${alibaba.cloud.version}</version>
  67. </dependency>
  68. <!-- mybatis启动器 -->
  69. <dependency>
  70. <groupId>org.mybatis.spring.boot</groupId>
  71. <artifactId>mybatis-spring-boot-starter</artifactId>
  72. <version>${mybatis.starter.version}</version>
  73. </dependency>
  74. <!-- 通用Mapper启动器 -->
  75. <dependency>
  76. <groupId>tk.mybatis</groupId>
  77. <artifactId>mapper-spring-boot-starter</artifactId>
  78. <version>${mapper.starter.version}</version>
  79. </dependency>
  80. <!-- 分页助手启动器 -->
  81. <dependency>
  82. <groupId>com.github.pagehelper</groupId>
  83. <artifactId>pagehelper-spring-boot-starter</artifactId>
  84. <version>${pageHelper.starter.version}</version>
  85. </dependency>
  86. <!-- mybatis plus-->
  87. <dependency>
  88. <groupId>com.baomidou</groupId>
  89. <artifactId>mybatis-plus-boot-starter</artifactId>
  90. <version>${mybatis.plus.version}</version>
  91. </dependency>
  92. <dependency>
  93. <groupId>com.baomidou</groupId>
  94. <artifactId>mybatis-plus-annotation</artifactId>
  95. <version>${mybatis.plus.version}</version>
  96. </dependency>
  97. <!-- mysql驱动 -->
  98. <dependency>
  99. <groupId>mysql</groupId>
  100. <artifactId>mysql-connector-java</artifactId>
  101. <version>${mysql.version}</version>
  102. </dependency>
  103. <!-- Druid连接池 -->
  104. <dependency>
  105. <groupId>com.alibaba</groupId>
  106. <artifactId>druid-spring-boot-starter</artifactId>
  107. <version>${durid.starter.version}</version>
  108. </dependency>
  109. <!--swagger2-->
  110. <dependency>
  111. <groupId>io.springfox</groupId>
  112. <artifactId>springfox-swagger2</artifactId>
  113. <version>${swagger.version}</version>
  114. </dependency>
  115. <dependency>
  116. <groupId>io.springfox</groupId>
  117. <artifactId>springfox-swagger-ui</artifactId>
  118. <version>${swagger.version}</version>
  119. </dependency>
  120. <!--jwt-->
  121. <!--JavaBean工具类,用于JavaBean数据封装-->
  122. <dependency>
  123. <groupId>commons-beanutils</groupId>
  124. <artifactId>commons-beanutils</artifactId>
  125. <version>${beanutils.version}</version>
  126. </dependency>
  127. <!--jwt工具-->
  128. <dependency>
  129. <groupId>io.jsonwebtoken</groupId>
  130. <artifactId>jjwt</artifactId>
  131. <version>${jwt.jjwt.version}</version>
  132. </dependency>
  133. <!--joda 时间工具类 -->
  134. <dependency>
  135. <groupId>joda-time</groupId>
  136. <artifactId>joda-time</artifactId>
  137. <version>${jwt.joda.version}</version>
  138. </dependency>
  139. </dependencies>
  140. </dependencyManagement>
  141. <dependencies>
  142. <!--web起步依赖-->
  143. <dependency>
  144. <groupId>org.springframework.boot</groupId>
  145. <artifactId>spring-boot-starter-web</artifactId>
  146. </dependency>
  147. <!-- mybatis启动器 -->
  148. <dependency>
  149. <groupId>org.mybatis.spring.boot</groupId>
  150. <artifactId>mybatis-spring-boot-starter</artifactId>
  151. </dependency>
  152. <!-- 通用Mapper启动器 -->
  153. <dependency>
  154. <groupId>tk.mybatis</groupId>
  155. <artifactId>mapper-spring-boot-starter</artifactId>
  156. </dependency>
  157. <!-- 分页助手启动器 -->
  158. <dependency>
  159. <groupId>com.github.pagehelper</groupId>
  160. <artifactId>pagehelper-spring-boot-starter</artifactId>
  161. </dependency>
  162. <!-- mysql驱动 -->
  163. <dependency>
  164. <groupId>mysql</groupId>
  165. <artifactId>mysql-connector-java</artifactId>
  166. </dependency>
  167. <!-- Druid连接池 -->
  168. <dependency>
  169. <groupId>com.alibaba</groupId>
  170. <artifactId>druid-spring-boot-starter</artifactId>
  171. </dependency>
  172. <!--swagger2-->
  173. <dependency>
  174. <groupId>io.springfox</groupId>
  175. <artifactId>springfox-swagger2</artifactId>
  176. </dependency>
  177. <dependency>
  178. <groupId>io.springfox</groupId>
  179. <artifactId>springfox-swagger-ui</artifactId>
  180. </dependency>
  181. </dependencies>
  182. </project>

                3.1.3核心配置文件

  • application.properties

  1. #端口号
  2. server.port=8080
  3. #数据库基本配置
  4. spring.datasource.driverClassName=com.mysql.jdbc.Driver
  5. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ssm_db3?useUnicode=true&characterEncoding=utf8
  6. spring.datasource.username=root
  7. spring.datasource.password=1234
  8. #druid 连接池配置
  9. #驱动
  10. #spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  11. #初始化连接池大小
  12. spring.datasource.druid.initial-size=1
  13. #最小连接数
  14. spring.datasource.druid.min-idle=1
  15. #最大连接数
  16. spring.datasource.druid.max-active=20
  17. #获取连接时候验证,会影响性能
  18. spring.datasource.druid.test-on-borrow=true
  19. # mybatis
  20. # mybatis.type-aliases-package=com.czxy.domain.base
  21. # mybatis.mapper-locations=classpath:mappers/*.xml
  22. #mapper
  23. mapper.not-empty=false
  24. mapper.identity=MYSQL
  25. #开启驼峰映射
  26. mybatis.configuration.map-underscore-to-camel-case=true
  27. mybatis.mapper-locations=classpath*:mapper/*.xml
  28. #开启log4j打印SQL语句
  29. logging.level.com.czxy.dao=debug

                3.1.4启动类

  1. package com.czxy.ssm;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class StudentApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(StudentApplication.class, args);
  8. }
  9. }

                3.1.5封装类

  1. package com.czxy.ssm.vo;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class BaseResult<T> {
  5. //成功状态码
  6. public static final int OK = 1;
  7. //失败状态码
  8. public static final int ERROR = 0;
  9. //返回码
  10. private Integer code;
  11. //返回消息
  12. private String message;
  13. //存放数据
  14. private T data;
  15. //其他数据
  16. private Map<String,Object> other = new HashMap<>();
  17. public BaseResult() {
  18. }
  19. public BaseResult(Integer code, String message) {
  20. this.code = code;
  21. this.message = message;
  22. }
  23. public BaseResult(Integer code, String message, T data) {
  24. this.code = code;
  25. this.message = message;
  26. this.data = data;
  27. }
  28. /**
  29. * 快捷成功BaseResult对象
  30. * @param message
  31. * @return
  32. */
  33. public static BaseResult ok(String message){
  34. return new BaseResult(BaseResult.OK , message);
  35. }
  36. public static BaseResult ok(String message, Object data){
  37. return new BaseResult(BaseResult.OK , message, data );
  38. }
  39. /**
  40. * 快捷失败BaseResult对象
  41. * @param message
  42. * @return
  43. */
  44. public static BaseResult error(String message){
  45. return new BaseResult(BaseResult.ERROR , message);
  46. }
  47. /**
  48. * 自定义数据区域
  49. * @param key
  50. * @param msg
  51. * @return
  52. */
  53. public BaseResult append(String key , Object msg){
  54. other.put(key , msg);
  55. return this;
  56. }
  57. public Integer getCode() {
  58. return code;
  59. }
  60. public String getMessage() {
  61. return message;
  62. }
  63. public T getData() {
  64. return data;
  65. }
  66. public Map<String, Object> getOther() {
  67. return other;
  68. }
  69. }

                3.1.6配置类(可选)

  1. package com.czxy.student.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.*;
  8. import springfox.documentation.spi.DocumentationType;
  9. import springfox.documentation.spi.service.contexts.SecurityContext;
  10. import springfox.documentation.spring.web.plugins.Docket;
  11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. /**
  15. * Swagger2 配置类,
  16. * 访问路径:swagger-ui.html
  17. * 自动注册:
  18. * 位置:resources/META-INF/spring.factories
  19. * 内容:
  20. * org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  21. * com.czxy.config.Swagger2Configuration
  22. */
  23. @Configuration
  24. @EnableSwagger2
  25. public class Swagger2ConfigurationV3 {
  26. @Bean
  27. public Docket createRestApi() {
  28. // 1 确定文档Swagger版本
  29. Docket docket = new Docket(DocumentationType.SWAGGER_2);
  30. // 2 设置 api基本信息
  31. docket.apiInfo(apiInfo());
  32. // 3 设置自定义加载路径
  33. docket = docket.select()
  34. .apis(RequestHandlerSelectors.basePackage("com.czxy"))
  35. .paths(PathSelectors.any())
  36. .build();
  37. //4 设置权限
  38. docket.securitySchemes(securitySchemes());
  39. docket.securityContexts(securityContexts());
  40. return docket;
  41. }
  42. private ApiInfo apiInfo() {
  43. return new ApiInfoBuilder()
  44. .title("API")
  45. .description("基于swagger接口文档")
  46. .contact(new Contact("梁桐","http://www.javaliang.com","liangtong@itcast.cn"))
  47. .version("1.0")
  48. .build();
  49. }
  50. private List<ApiKey> securitySchemes() {
  51. List<ApiKey> list = new ArrayList<>();
  52. // name 为参数名 keyname是页面传值显示的 keyname, name在swagger鉴权中使用
  53. list.add(new ApiKey("Authorization", "Authorization", "header"));
  54. return list;
  55. }
  56. private List<SecurityContext> securityContexts() {
  57. List<SecurityContext> list = new ArrayList<>();
  58. list.add(SecurityContext.builder()
  59. .securityReferences(defaultAuth())
  60. .forPaths(PathSelectors.regex("^(?!auth).*$"))
  61. .build());
  62. return list;
  63. }
  64. private List<SecurityReference> defaultAuth() {
  65. AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
  66. AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
  67. authorizationScopes[0] = authorizationScope;
  68. List<SecurityReference> list = new ArrayList();
  69. list.add(new SecurityReference("Authorization", authorizationScopes));
  70. return list;
  71. }
  72. }

        3.2前端环境

                3.2.1拷贝静态资源

      

                3.2.2vs打开资源

4.班级管理

        4.1JavaBean:Classes

  1. package com.czxy.ssm.domain;
  2. import javax.persistence.Column;
  3. import javax.persistence.Id;
  4. import javax.persistence.Table;
  5. @Table(name = "tb_class")
  6. public class Classes {
  7. /*
  8. CREATE TABLE tb_class(
  9. `c_id` VARCHAR(32) PRIMARY KEY COMMENT '班级ID',
  10. `c_name` VARCHAR(50) COMMENT '班级名称',
  11. `desc` VARCHAR(200) COMMENT '班级描述'
  12. );
  13. */
  14. @Id
  15. @Column(name = "c_id")
  16. private String cid;
  17. @Column(name = "c_name")
  18. private String cname;
  19. @Column(name = "`desc`")
  20. private String desc;
  21. public String getCid() {
  22. return cid;
  23. }
  24. public void setCid(String cid) {
  25. this.cid = cid;
  26. }
  27. public String getCname() {
  28. return cname;
  29. }
  30. public void setCname(String cname) {
  31. this.cname = cname;
  32. }
  33. public String getDesc() {
  34. return desc;
  35. }
  36. public void setDesc(String desc) {
  37. this.desc = desc;
  38. }
  39. public Classes() {
  40. }
  41. public Classes(String cid, String cname, String desc) {
  42. this.cid = cid;
  43. this.cname = cname;
  44. this.desc = desc;
  45. }
  46. @Override
  47. public String toString() {
  48. return "Classes{" +
  49. "cid='" + cid + '\'' +
  50. ", cname='" + cname + '\'' +
  51. ", desc='" + desc + '\'' +
  52. '}';
  53. }
  54. }

        4.2查询所有

                4.2.1后端

  • 编写Mapper
  1. package com.czxy.ssm.mapper;
  2. import com.czxy.ssm.domain.Classes;
  3. import tk.mybatis.mapper.common.Mapper;
  4. @org.apache.ibatis.annotations.Mapper
  5. public interface ClassesMapper extends Mapper<Classes> {
  6. }
  • 编写service
  • 接口
  1. package com.czxy.ssm.service;
  2. import com.czxy.ssm.domain.Classes;
  3. import java.util.List;
  4. public interface ClassesService {
  5. /**
  6. * 查询所有
  7. * @return
  8. */
  9. public List<Classes> selectAll() ;
  10. }

        -实现类

  1. package com.czxy.ssm.service.impl;
  2. import com.czxy.ssm.domain.Classes;
  3. import com.czxy.ssm.mapper.ClassesMapper;
  4. import com.czxy.ssm.service.ClassesService;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import javax.annotation.Resource;
  8. import java.util.List;
  9. @Service
  10. @Transactional
  11. public class ClassesServiceImpl implements ClassesService {
  12. @Resource
  13. private ClassesMapper classesMapper;
  14. @Override
  15. public List<Classes> selectAll() {
  16. List<Classes> list = classesMapper.selectAll();
  17. return list;
  18. }
  19. }
  •  编写Controller

  1. package com.czxy.ssm.controller;
  2. import com.czxy.ssm.domain.Classes;
  3. import com.czxy.ssm.service.ClassesService;
  4. import com.czxy.ssm.vo.BaseResult;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import javax.annotation.Resource;
  9. import java.util.List;
  10. @RestController
  11. @RequestMapping("/classes")
  12. public class ClassesController {
  13. @Resource
  14. private ClassesService classesService;
  15. @GetMapping
  16. public BaseResult<List<Classes>> list() {
  17. // 查询所有
  18. List<Classes> list = classesService.selectAll();
  19. // 返回
  20. /*
  21. BaseResult baseResult = new BaseResult();
  22. baseResult.setCode(1); // 0 错误, 20000 成功
  23. baseResult.setMessage("提示信息");
  24. baseResult.setData(list);
  25. return baseResult;
  26. */
  27. return BaseResult.ok("查询成功", list);
  28. }
  29. }
  • 查询结果

                4.2.2前端:axios+then

  • 编写页面、发送ajax、显示数据
  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  6. <title>Document</title>
  7. <script src="js/vue.js"></script>
  8. <script src="js/axios.js"></script>
  9. </head>
  10. <body>
  11. <div id="app">
  12. <table border="1">
  13. <tr>
  14. <td>编号</td>
  15. <td>名称</td>
  16. <td>描述</td>
  17. </tr>
  18. <tr v-for="(classes,index) in classesList">
  19. <td>{{classes.cid}}</td>
  20. <td>{{classes.cname}} </td>
  21. <td>{{classes.desc}}</td>
  22. </tr>
  23. </table>
  24. </div>
  25. </body>
  26. </html>
  27. <script>
  28. new Vue({
  29. el: '#app',
  30. data: {
  31. classesList: []
  32. },
  33. methods: {
  34. selectAllClasses() {
  35. // ajax 查询
  36. var url = 'http://localhost:8080/classes'
  37. axios.get(url)
  38. .then(res => {
  39. // 获得查询结果 BaseResult
  40. var baseResult = res.data
  41. // 判断
  42. if(baseResult.code == 1) {
  43. // 成功 -- 将结果存放到数据区域
  44. this.classesList = baseResult.data
  45. } else {
  46. // 失败
  47. alert(baseResult.message)
  48. }
  49. })
  50. .catch(err => {
  51. console.error(err);
  52. })
  53. }
  54. },
  55. created() { //mounted()
  56. // 查询所有
  57. this.selectAllClasses()
  58. },
  59. })
  60. </script>

                4.2.3前端:async+await

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>班级列表</title>
  6. <script src="../js/axios.js"></script>
  7. <script src="../js/vue.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <table border="1">
  12. <tr>
  13. <td>编号</td>
  14. <td>班级名称</td>
  15. <td>描述</td>
  16. <td>操作</td>
  17. </tr>
  18. <tr v-for="(classes,index) in classesList">
  19. <td>{{classes.cid}}</td>
  20. <td>{{classes.cname}}</td>
  21. <td>{{classes.desc}}</td>
  22. <td>
  23. 修改
  24. 删除
  25. </td>
  26. </tr>
  27. </table>
  28. </div>
  29. </body>
  30. </html>
  31. <script>
  32. new Vue({
  33. el: '#app',
  34. data: {
  35. classesList: [], //所有班级
  36. },
  37. methods: {
  38. async findAllClasses() {
  39. // response.data
  40. let { data: baseResult } = await axios.get(`http://localhost:8080/classes`)
  41. // 判断
  42. if(baseResult.code == 20000) {
  43. this.classesList = baseResult.data
  44. }
  45. }
  46. },
  47. mounted() {
  48. // 查询所有班级
  49. this.findAllClasses()
  50. }
  51. })
  52. </script>

        4.3添加班级

                4.3.1需求:

  • 完成班级的添加
  • 后端
    • 获得提交的班级数据(json数据)
    • 保存班级数据(controller-service)
    • 根据操作结果提示:成功true-->添加成功,失败false-->添加失败
  • 前端
    • 绘制表单,数据绑定
    • 点击添加,发送ajax完成添加
    • 成功,跳转到列表页面。失败,给出提示

                4.3.2后端

  • 步骤1:编写service,完成添加
  • 接口
  1. /**
  2. * 添加班级
  3. * @param classes
  4. * @return
  5. */
  6. public boolean add(Classes classes);
  • 实现类
  1. @Override
  2. public boolean add(Classes classes) {
  3. int result = classesMapper.insert(classes);
  4. return result == 1;
  5. }
  • 步骤2:编写controller,处理结果BaseResult
  1. /**
  2. * 添加班级
  3. * @param classes
  4. * @return
  5. */
  6. @PostMapping
  7. public BaseResult add(@RequestBody Classes classes) {
  8. // 添加
  9. boolean result =classesService.add(classes);
  10. // 提示
  11. if(result) {
  12. // 成功
  13. return BaseResult.ok("添加成功");
  14. }
  15. // 失败
  16. return BaseResult.error("添加失败");
  17. }

              4.3.3前端

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>班级列表</title>
  6. <script src="../js/axios.js"></script>
  7. <script src="../js/vue.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <table>
  12. <tr>
  13. <td>编号</td>
  14. <td>
  15. <input type="text" v-model="classes.cid" />
  16. </td>
  17. </tr>
  18. <tr>
  19. <td>班级名称</td>
  20. <td>
  21. <input type="text" v-model="classes.cname" />
  22. </td>
  23. </tr>
  24. <tr>
  25. <td>班级描述</td>
  26. <td>
  27. <textarea cols="30" rows="10" v-model="classes.desc"></textarea>
  28. </td>
  29. </tr>
  30. <tr>
  31. <td>
  32. <input type="button" value="添加" @click="addClasses" />
  33. </td>
  34. <td></td>
  35. </tr>
  36. </table>
  37. </div>
  38. </body>
  39. </html>
  40. <script>
  41. new Vue({
  42. el: '#app',
  43. data: {
  44. classes: {
  45. }
  46. },
  47. methods: {
  48. async addClasses() {
  49. let { data: baseResult } = await axios.post(`http://localhost:8080/classes`, this.classes)
  50. // 提示
  51. if(baseResult.code == 20000) {
  52. //成功
  53. location.href = '/classes/list.html'
  54. } else {
  55. //失败
  56. alert(baseResult.message)
  57. }
  58. }
  59. },
  60. mounted() {
  61. }
  62. })
  63. </script>

        4.4修改班级

                4.4.1需求:

  • 完成班级的修改
    • 表单的回显:通过id查询详情
    • 修改功能:通过id更新

                4.4.2后端

  • 步骤
    • 编写service:selectById、update
      • 接口
      • 实现类
    • 编写controller:selectById、update
  • 编写service:selectById、update
  • 接口
  1. /**
  2. * 通过id查询详情
  3. * @param cid
  4. * @return
  5. */
  6. public Classes selectById(String cid);
  7. /**
  8. * 更新班级
  9. * @param classes
  10. * @return
  11. */
  12. public boolean update(Classes classes);
  • 实现类

  1. @Override
  2. public Classes selectById(String cid) {
  3. return classesMapper.selectByPrimaryKey(cid);
  4. }
  5. @Override
  6. public boolean update(Classes classes) {
  7. int result = classesMapper.updateByPrimaryKeySelective(classes);
  8. return result == 1;
  9. }
  • 编写controller:selectById、update
  1. /**
  2. * 查询详情
  3. * @param cid
  4. * @return
  5. */
  6. @GetMapping("/{cid}")
  7. public BaseResult<Classes> selectById(@PathVariable("cid") String cid) {
  8. //查询
  9. Classes classes = classesService.selectById(cid);
  10. //返回
  11. return BaseResult.ok("查询详情成功", classes);
  12. }
  13. /**
  14. * 更新
  15. * @param classes
  16. * @return
  17. */
  18. @PutMapping
  19. public BaseResult update(@RequestBody Classes classes) {
  20. try {
  21. // 更新
  22. boolean result = classesService.update(classes);
  23. // 提示
  24. if(result) {
  25. return BaseResult.ok("更新成功");
  26. }
  27. return BaseResult.error("更新失败");
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. return BaseResult.error(e.getMessage());
  31. }
  32. }

              4.4.3前端

  • 步骤:
    • 列表页面点击“修改”,添加到修改页面 edit.html?cid=c001
    • 页面加载成功时,通过id查询详情
    • 回显:将查询结果绑定表单(添加已经完成,采用复制)
    • 点击确定进行更新
  • 修改list.html页面

  <a :href="'/cls/edit.html?cid=' + classes.cid">修改</a>
  • 编写edit.html页面        
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>班级列表</title>
  6. <script src="../js/axios.js"></script>
  7. <script src="../js/vue.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <!--3 表单回显 -->
  12. <table>
  13. <tr>
  14. <td>编号</td>
  15. <td>
  16. {{classes.cid}}
  17. </td>
  18. </tr>
  19. <tr>
  20. <td>班级名称</td>
  21. <td>
  22. <input type="text" v-model="classes.cname" />
  23. </td>
  24. </tr>
  25. <tr>
  26. <td>班级描述</td>
  27. <td>
  28. <textarea cols="30" rows="10" v-model="classes.desc"></textarea>
  29. </td>
  30. </tr>
  31. <tr>
  32. <td>
  33. <!--4 更新 -->
  34. <input type="button" value="修改" @click="updateClasses" />
  35. </td>
  36. <td></td>
  37. </tr>
  38. </table>
  39. </div>
  40. </body>
  41. </html>
  42. <script>
  43. new Vue({
  44. el: '#app',
  45. data: {
  46. classes: {
  47. },
  48. classesId: ''
  49. },
  50. methods: {
  51. async selectClassesById() { //2 查询详情
  52. let { data: baseResult } = await axios.get(`http://localhost:8080/classes/${this.classesId}`)
  53. // 将查询结果存放classes对象,将自动回显
  54. if( baseResult.code == 20000) {
  55. this.classes = baseResult.data
  56. }
  57. },
  58. async updateClasses() { // 4 更新函数
  59. let { data: baseResult } = await axios.put(`http://localhost:8080/classes`, this.classes)
  60. // 提示
  61. if(baseResult.code == 20000) {
  62. //成功
  63. location.href = '/cls/list.html'
  64. } else {
  65. //失败
  66. alert(baseResult.message)
  67. }
  68. }
  69. },
  70. mounted() {
  71. // 2.1 获得id
  72. let arr = location.href.split("?cid=")
  73. this.classesId = arr[1]
  74. // 2.2 查询详情
  75. this.selectClassesById()
  76. }
  77. })
  78. </script>

        4.5删除班级

                4.5.1分析

  • 需求:通过id删除班级
  • 思路:
    • 确定访问路径:delete /classes/c001
    • 点击发送ajax进行删除(成功:刷新页面;失败:提示)
    • 编写controller获得id
    • 编写service通过id删除

                4.5.2后端

  • 步骤:
    • 编写controller获得id
    • 编写service通过id删除
  • 编写controller获得id
  1. @DeleteMapping("/{classesId}")
  2. public BaseResult delete(@PathVariable("classesId") String classesId) {
  3. // 删除
  4. boolean result = classesService.deleteById(classesId);
  5. // 处理
  6. // 提示
  7. if(result) {
  8. return BaseResult.ok("删除成功");
  9. }
  10. return BaseResult.error("删除失败");
  11. }
  • 编写service通过id删除
    • 接口
  1. /**
  2. * 通过id删除
  3. * @param classesId
  4. * @return
  5. */
  6. boolean deleteById(String classesId);
  7. –实现类
  8. @Override
  9. public boolean deleteById(String classesId) {
  10. int result = classesMapper.deleteByPrimaryKey(classesId);
  11. return result == 1 ;
  12. }

                4.5.3前端

  • 需求:点击发送删除ajax
  1. <a href="" @click.prevent="deleteClasses(classes.cid)">删除</a>
  2. async deleteClasses( cid ) {
  3. // 询问
  4. if(!confirm("您确定要删除么?")) {
  5. return
  6. }
  7. let { data: baseResult } = await axios.delete(`http://localhost:8080/classes/${cid}`)
  8. if(baseResult.code == 20000) {
  9. // 成功
  10. this.findAllClasses()
  11. } else {
  12. // 失败
  13. alert(baseResult.message)
  14. }
  15. }

                4.5.4完善后端:级联删除

  • 级联删除:删除班级前,先删除所关联的学生。
  • 需求:完善班级删除的service,先删除指定班级的所有学生。
  • 步骤:
    • 编写JavaBen:Student
    • 编写Mapper:StudentMapper
    • 修改班级的service
    • 编写JavaBen:Student
  1. package com.czxy.student.domain;
  2. import lombok.Data;
  3. import javax.persistence.Column;
  4. import javax.persistence.Id;
  5. import javax.persistence.Table;
  6. import java.util.Date;
  7. @Table(name = "tb_student")
  8. @Data
  9. public class Student {
  10. @Id
  11. @Column(name = "s_id")
  12. private String sid;
  13. private String sname;
  14. private Integer age;
  15. private Date birthday;
  16. @Column(name = "c_id")
  17. private String cid;
  18. }
  19. /*
  20. Create Table
  21. CREATE TABLE `tb_student` (
  22. `s_id` varchar(32) NOT NULL COMMENT '学生ID',
  23. `sname` varchar(50) DEFAULT NULL COMMENT '姓名',
  24. `age` int(11) DEFAULT NULL COMMENT '年龄',
  25. `birthday` datetime DEFAULT NULL COMMENT '生日',
  26. `gender` char(1) DEFAULT NULL COMMENT '性别',
  27. `c_id` varchar(32) DEFAULT NULL,
  28. PRIMARY KEY (`s_id`),
  29. KEY `c_id` (`c_id`),
  30. CONSTRAINT `tb_student_ibfk_1` FOREIGN KEY (`c_id`) REFERENCES `tb_class` (`c_id`)
  31. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  32. */
  • 编写Mapper:StudentMapper
  1. package com.czxy.student.mapper;
  2. import com.czxy.student.domain.Student;
  3. import tk.mybatis.mapper.common.Mapper;
  4. @org.apache.ibatis.annotations.Mapper
  5. public interface StudentMapper extends Mapper<Student> {
  6. }
  7. •修改班级的service
  8. @Resource
  9. private StudentMapper studentMapper;
  10. @Override
  11. public boolean deleteById(String classesId) {
  12. // 先删除班级中的学生
  13. Example example = new Example(Student.class);
  14. Example.Criteria criteria = example.createCriteria();
  15. criteria.andEqualTo("cid", classesId);
  16. studentMapper.deleteByExample(example);
  17. // 删除班级
  18. int result = classesMapper.deleteByPrimaryKey(classesId);
  19. return result == 1 ;
  20. }

 5.后期更新学生管理系统之学生管理

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号