在线教育项目【课程科目板块】前端vue和后端查询功能实现

举报
爱吃豆的土豆 发表于 2022/09/25 03:19:49 2022/09/25
【摘要】 目录 1,分析 1.1:环境搭建 1.1.1:数据库 1.1.2:后端:环境 1.1.3:后端:基本模块 1.1.4:前端:课程板块 2,查询所有课程 2.1:后端实现 2.2:前端ajax发送 2.3:前端实现 1,分析 1.1:环境搭建 1.1.1:数据库 CREATE DATABASE zx_...

目录

1,分析

1.1:环境搭建

1.1.1:数据库

1.1.2:后端:环境

1.1.3:后端:基本模块

1.1.4:前端:课程板块

2,查询所有课程

2.1:后端实现

2.2:前端ajax发送

2.3:前端实现


1,分析

1.1:环境搭建

1.1.1:数据库


  
  1. CREATE DATABASE zx_edu_course;
  2. USE zx_edu_course;
  3. CREATE TABLE `edu_subject` (
  4. `id` VARCHAR(32) NOT NULL PRIMARY KEY COMMENT '课程科目ID',
  5. `title` VARCHAR(10) NOT NULL COMMENT '科目名称',
  6. `parent_id` VARCHAR(32) NOT NULL DEFAULT '0' COMMENT '父ID',
  7. `sort` INT(10) NOT NULL DEFAULT 0 COMMENT '排序字段',
  8. `gmt_create` DATETIME NOT NULL COMMENT '创建时间',
  9. `gmt_modified` DATETIME NOT NULL COMMENT '更新时间'
  10. ) COMMENT = '课程科目';
  11. INSERT INTO `edu_subject` VALUES ('1', '云计算', '0', 0, '2020-06-26 09:41:21', '2020-02-20 23:25:58');
  12. INSERT INTO `edu_subject` VALUES ('2', '系统/运维', '0', 0, '2020-02-20 23:29:59', '2020-02-20 23:29:59');
  13. INSERT INTO `edu_subject` VALUES ('3', '数据库', '0', 0, '2020-02-20 23:30:13', '2020-02-20 23:30:13');
  14. INSERT INTO `edu_subject` VALUES ('4', '服务器', '0', 0, '2020-02-20 23:30:19', '2020-02-20 23:30:19');
  15. INSERT INTO `edu_subject` VALUES ('5', 'MySQL', '3', 1, '2020-02-20 23:30:13', '2020-02-20 23:30:13');
  16. INSERT INTO `edu_subject` VALUES ('6', 'Oracle', '3', 2, '2020-02-20 23:30:13', '2020-02-20 23:30:13');
  17. INSERT INTO `edu_subject` VALUES ('7', 'Tomcat', '4', 1, '2020-02-20 23:30:13', '2020-02-20 23:30:13');
  18. INSERT INTO `edu_subject` VALUES ('8', 'Nginx ', '4', 2, '2020-02-20 23:30:13', '2020-02-20 23:30:13');
  19. INSERT INTO `edu_subject` VALUES ('9', 'MySQL优化', '5', 1, '2020-02-20 23:30:13', '2020-02-20 23:30:13');

1.1.2:后端:环境

创建项目:zx_service_course

pom文件


  
  1. <dependencies>
  2. <!--web起步依赖-->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-web</artifactId>
  6. </dependency>
  7. <!-- nacos 客户端 -->
  8. <dependency>
  9. <groupId>com.alibaba.nacos</groupId>
  10. <artifactId>nacos-client</artifactId>
  11. </dependency>
  12. <!-- nacos 服务发现 -->
  13. <dependency>
  14. <groupId>com.alibaba.cloud</groupId>
  15. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  16. </dependency>
  17. <!--swagger2-->
  18. <dependency>
  19. <groupId>io.springfox</groupId>
  20. <artifactId>springfox-swagger2</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>io.springfox</groupId>
  24. <artifactId>springfox-swagger-ui</artifactId>
  25. </dependency>
  26. <!-- feign 远程调用 -->
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-openfeign</artifactId>
  30. </dependency>
  31. <!--测试-->
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. </dependency>
  36. <!-- mybatis plus-->
  37. <dependency>
  38. <groupId>com.baomidou</groupId>
  39. <artifactId>mybatis-plus-boot-starter</artifactId>
  40. <version>${mybatis.plus.version}</version>
  41. </dependency>
  42. <!-- mysql驱动 -->
  43. <dependency>
  44. <groupId>mysql</groupId>
  45. <artifactId>mysql-connector-java</artifactId>
  46. </dependency>
  47. <!--自定义项目-->
  48. <dependency>
  49. <groupId>com.czxy.zx</groupId>
  50. <artifactId>zx_common_fx</artifactId>
  51. </dependency>
  52. <dependency>
  53. <groupId>com.czxy.zx</groupId>
  54. <artifactId>zx_domain_fx</artifactId>
  55. </dependency>
  56. <!-- redis 启动器 -->
  57. <dependency>
  58. <groupId>org.springframework.boot</groupId>
  59. <artifactId>spring-boot-starter-data-redis</artifactId>
  60. </dependency>
  61. <!-- JavaMail 启动器 -->
  62. <dependency>
  63. <groupId>org.springframework.boot</groupId>
  64. <artifactId>spring-boot-starter-mail</artifactId>
  65. </dependency>
  66. <!-- MQ 启动器 -->
  67. <dependency>
  68. <groupId>org.springframework.boot</groupId>
  69. <artifactId>spring-boot-starter-amqp</artifactId>
  70. </dependency>
  71. <!-- fastjson -->
  72. <dependency>
  73. <groupId>com.alibaba</groupId>
  74. <artifactId>fastjson</artifactId>
  75. </dependency>
  76. <!--开发者工具-->
  77. <dependency>
  78. <groupId>org.springframework.boot</groupId>
  79. <artifactId>spring-boot-devtools</artifactId>
  80. <optional>true</optional>
  81. </dependency>
  82. <dependency>
  83. <groupId>com.alibaba</groupId>
  84. <artifactId>easyexcel</artifactId>
  85. <version>3.0.5</version>
  86. </dependency>
  87. </dependencies>

 application.yml

# 服务端口号
server:
  port: 9020
# 服务名
spring:
  application:
    name: course-service
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/zx_edu_course?useUnicode=true&characterEncoding=utf8
    username: root
    password: root
    druid:    #druid 连接池配置
      initial-size: 1       #初始化连接池大小
      min-idle: 1           #最小连接数
      max-active: 20        #最大连接数
      test-on-borrow: true  #获取连接时候验证,会影响性能
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848   #nacos服务地址
  redis:
    database: 0       #数据库索引,取值0-15,表示16个库可选择
    host: 127.0.0.1   #服务器地址
    port: 6379        #服务器连接端口号
  mail:
    host: smtp.126.com          #发送邮件服务器
    username: itcast_lt@126.com #账号
    password: 1qaz2wsx          #密码
    default-encoding: UTF-8     #默认编码时
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    virtualHost: /
  devtools:
    restart:
      enabled: true  #设置开启热部署
      additional-paths: src/main/java #重启目录
      exclude: WEB-INF/**
  freemarker:
    cache: false    #页面不加载缓存,修改即时生效

#开启log4j打印SQL语句
logging:
  level:
    com:
      czxy:
        mapper: debug

# mp日志打印
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  

启动类:


  
  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableFeignClients
  4. public class CourseApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(CourseApplication.class,args);
  7. }
  8. }

 拷贝配置

1.1.3:后端:基本模块

 创建JavaBean


  
  1. package com.czxy.zx.domain;
  2. @Data
  3. @TableName("edu_subject")
  4. public class EduSubject{
  5. @TableId(type = IdType.ASSIGN_UUID)
  6. //课程科目ID
  7. private String id;
  8. //科目名称
  9. private String title;
  10. //父ID
  11. private String parentId;
  12. //排序字段
  13. private Integer sort;
  14. //创建时间
  15. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
  16. @TableField(fill = FieldFill.INSERT)
  17. private Date gmtCreate;
  18. //更新时间
  19. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
  20. @TableField(fill = FieldFill.INSERT)
  21. private Date gmtModified;
  22. @TableField(exist = false)
  23. @JsonInclude(JsonInclude.Include.NON_EMPTY) //生成json数据,不包含空元素
  24. private List<EduSubject> children = new ArrayList<>();
  25. }

创建mapper


  
  1. @Mapper
  2. public interface EduSubjectMapper extends BaseMapper<EduSubject> {
  3. }

 

创建service

 

接口


  
  1. public interface EduSubjectService extends IService<EduSubject> {
  2. }

实现类


  
  1. @Service
  2. public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
  3. }

创建controller

 


  
  1. @RequestMapping("/course")
  2. @RestController
  3. public class EduSubjectController {
  4. @Resource
  5. private EduSubjectService eduSubjectService;
  6. @GetMapping
  7. public BaseResult findAll(){
  8. System.out.println("查询所有");
  9. QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
  10. queryWrapper.orderByAsc("parent_id");
  11. List<EduSubject> eduSubjectList = eduSubjectService.list(queryWrapper);
  12. //提供list集合用来缓存一级目录
  13. ArrayList<EduSubject> resultsubject = new ArrayList<>();
  14. //创建map集合用来缓存
  15. Map<String,EduSubject> subjectMap = new HashMap<>();
  16. //进行遍历查询结果
  17. for (EduSubject eduSubject : eduSubjectList) {
  18. //通过id进行判断map集合中是否保存
  19. EduSubject parentsubject = subjectMap.get(eduSubject.getParentId());
  20. //进行判断父是否存在,如果不存在存放在list集合中为一级目录
  21. if (parentsubject == null){
  22. resultsubject.add(eduSubject);
  23. }else {
  24. //如果存在说明是二级的通过id找到父亲放入父亲的属性中
  25. parentsubject.getChildren().add(eduSubject);
  26. }
  27. subjectMap.put(eduSubject.getId(),eduSubject);
  28. }
  29. System.out.println(resultsubject);
  30. return BaseResult.ok("查询成功",resultsubject);
  31. }
  32. }

1.1.4:前端:课程板块

创建路由模块

 


  
  1. /** When your routing table is too long, you can split it into small modules **/
  2. import Layout from '@/layout' //导入布局组件
  3. const courseRouter = {
  4. path: '/course', //一级路径
  5. component: Layout, //页面布局,固定写法
  6. redirect: '/course/subjectList', //访问/teacher重定向的页面位置
  7. name: '课程管理',
  8. meta: {
  9. title: '课程管理', //左侧一级菜单的菜单名
  10. icon: 'table', //左侧一级菜单的图标,取值参考:src/icons/svg目录下文件名
  11. // roles: ['admin']
  12. },
  13. children: [
  14. {
  15. path: 'subjectList', //二级路径
  16. component: () => import('@/views/edu/course/SubjectList'), //页面位置
  17. name: '科目列表',
  18. meta: { //二级菜单的菜单名称
  19. title: '科目列表',
  20. icon: 'list',
  21. // roles: ['admin']
  22. }
  23. },
  24. ]
  25. }
  26. export default courseRouter

创建 subjectList.vue 页面


  
  1. <template>
  2. <div>
  3. <el-table
  4. v-loading="loading"
  5. :data="result"
  6. row-key="title"
  7. :tree-props="{children: 'children'}"
  8. style="width: 50%">
  9. <el-table-column
  10. prop="title"
  11. label="科目名称"
  12. width="180">
  13. </el-table-column>
  14. <el-table-column
  15. prop="sort"
  16. label="排序"
  17. width="180">
  18. </el-table-column>
  19. <el-table-column
  20. prop="gmtCreate"
  21. label="添加时间"
  22. width="150">
  23. </el-table-column>
  24. <el-table-column
  25. prop="gmtModified"
  26. label="修改时间"
  27. width="150">
  28. </el-table-column>
  29. <el-table-column label="操作">
  30. <template slot-scope="">
  31. <el-button
  32. size="mini">编辑</el-button>
  33. <el-button
  34. size="mini"
  35. type="danger">删除</el-button>
  36. </template>
  37. </el-table-column>
  38. </el-table>
  39. </div>
  40. </template>
  41. <script>
  42. //导入ajax
  43. import { findAll } from "@/api/edu/course"
  44. export default {
  45. name: 'WORKSPACE_NAMESubjectList',
  46. data() {
  47. return {
  48. result:[],
  49. loading:false
  50. };
  51. },
  52. mounted() {
  53. this.selectAll()
  54. },
  55. methods: {
  56. async selectAll() {
  57. this.loading = true
  58. let baseResult = await findAll()
  59. this.result = baseResult.data
  60. this.loading = false
  61. }
  62. },
  63. };
  64. </script>
  65. <style lang="scss" scoped>
  66. </style>

配置路由

 

2,查询所有课程

2.1:后端实现

  • 修改 EduSubjectController


  
  1. package com.czxy.zx.course.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.czxy.zx.course.service.EduSubjectService;
  4. import com.czxy.zx.domain.EduSubject;
  5. import com.czxy.zx.vo.BaseResult;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import javax.annotation.Resource;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. @RequestMapping("/course")
  15. @RestController
  16. public class EduSubjectController {
  17. @Resource
  18. private EduSubjectService eduSubjectService;
  19. @GetMapping
  20. public BaseResult findAll(){
  21. System.out.println("查询所有");
  22. QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
  23. queryWrapper.orderByAsc("parent_id");
  24. List<EduSubject> eduSubjectList = eduSubjectService.list(queryWrapper);
  25. //提供list集合用来缓存一级目录
  26. ArrayList<EduSubject> resultsubject = new ArrayList<>();
  27. //创建map集合用来缓存
  28. Map<String,EduSubject> subjectMap = new HashMap<>();
  29. //进行遍历查询结果
  30. for (EduSubject eduSubject : eduSubjectList) {
  31. //通过id进行判断map集合中是否保存
  32. EduSubject parentsubject = subjectMap.get(eduSubject.getParentId());
  33. //进行判断父是否存在,如果不存在存放在list集合中为一级目录
  34. if (parentsubject == null){
  35. resultsubject.add(eduSubject);
  36. }else {
  37. //如果存在说明是二级的通过id找到父亲放入父亲的属性中
  38. parentsubject.getChildren().add(eduSubject);
  39. }
  40. subjectMap.put(eduSubject.getId(),eduSubject);
  41. }
  42. System.out.println(resultsubject);
  43. return BaseResult.ok("查询成功",resultsubject);
  44. }
  45. }

2.2:前端ajax发送

import axios from '@/utils/request'

// 查询所有课程科目
export function findAllSub() {
  return axios.get('/course-service/subject');
}

2.3:前端实现

修改 @/views/edu/course/subjectList.vue

 


  
  1. <template>
  2. <div>
  3. <el-table
  4. v-loading="loading"
  5. :data="result"
  6. row-key="title"
  7. :tree-props="{children: 'children'}"
  8. style="width: 50%">
  9. <el-table-column
  10. prop="title"
  11. label="科目名称"
  12. width="180">
  13. </el-table-column>
  14. <el-table-column
  15. prop="sort"
  16. label="排序"
  17. width="180">
  18. </el-table-column>
  19. <el-table-column
  20. prop="gmtCreate"
  21. label="添加时间"
  22. width="150">
  23. </el-table-column>
  24. <el-table-column
  25. prop="gmtModified"
  26. label="修改时间"
  27. width="150">
  28. </el-table-column>
  29. <el-table-column label="操作">
  30. <template slot-scope="">
  31. <el-button
  32. size="mini">编辑</el-button>
  33. <el-button
  34. size="mini"
  35. type="danger">删除</el-button>
  36. </template>
  37. </el-table-column>
  38. </el-table>
  39. </div>
  40. </template>
  41. <script>
  42. //导入ajax
  43. import { findAll } from "@/api/edu/course"
  44. export default {
  45. name: 'WORKSPACE_NAMESubjectList',
  46. data() {
  47. return {
  48. result:[],
  49. loading:false
  50. };
  51. },
  52. mounted() {
  53. this.selectAll()
  54. },
  55. methods: {
  56. async selectAll() {
  57. this.loading = true
  58. let baseResult = await findAll()
  59. this.result = baseResult.data
  60. this.loading = false
  61. }
  62. },
  63. };
  64. </script>
  65. <style lang="scss" scoped>
  66. </style>

文章来源: qianxu.blog.csdn.net,作者:爱吃豆的土豆,版权归原作者所有,如需转载,请联系作者。

原文链接:qianxu.blog.csdn.net/article/details/126712564

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。