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


    目录

    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. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-webartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>com.alibaba.nacosgroupId>
    8. <artifactId>nacos-clientartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>com.alibaba.cloudgroupId>
    12. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    13. dependency>
    14. <dependency>
    15. <groupId>io.springfoxgroupId>
    16. <artifactId>springfox-swagger2artifactId>
    17. dependency>
    18. <dependency>
    19. <groupId>io.springfoxgroupId>
    20. <artifactId>springfox-swagger-uiartifactId>
    21. dependency>
    22. <dependency>
    23. <groupId>org.springframework.cloudgroupId>
    24. <artifactId>spring-cloud-starter-openfeignartifactId>
    25. dependency>
    26. <dependency>
    27. <groupId>org.springframework.bootgroupId>
    28. <artifactId>spring-boot-starter-testartifactId>
    29. dependency>
    30. <dependency>
    31. <groupId>com.baomidougroupId>
    32. <artifactId>mybatis-plus-boot-starterartifactId>
    33. <version>${mybatis.plus.version}version>
    34. dependency>
    35. <dependency>
    36. <groupId>mysqlgroupId>
    37. <artifactId>mysql-connector-javaartifactId>
    38. dependency>
    39. <dependency>
    40. <groupId>com.czxy.zxgroupId>
    41. <artifactId>zx_common_fxartifactId>
    42. dependency>
    43. <dependency>
    44. <groupId>com.czxy.zxgroupId>
    45. <artifactId>zx_domain_fxartifactId>
    46. dependency>
    47. <dependency>
    48. <groupId>org.springframework.bootgroupId>
    49. <artifactId>spring-boot-starter-data-redisartifactId>
    50. dependency>
    51. <dependency>
    52. <groupId>org.springframework.bootgroupId>
    53. <artifactId>spring-boot-starter-mailartifactId>
    54. dependency>
    55. <dependency>
    56. <groupId>org.springframework.bootgroupId>
    57. <artifactId>spring-boot-starter-amqpartifactId>
    58. dependency>
    59. <dependency>
    60. <groupId>com.alibabagroupId>
    61. <artifactId>fastjsonartifactId>
    62. dependency>
    63. <dependency>
    64. <groupId>org.springframework.bootgroupId>
    65. <artifactId>spring-boot-devtoolsartifactId>
    66. <optional>trueoptional>
    67. dependency>
    68. <dependency>
    69. <groupId>com.alibabagroupId>
    70. <artifactId>easyexcelartifactId>
    71. <version>3.0.5version>
    72. dependency>
    73. 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 children = new ArrayList<>();
    25. }

    创建mapper

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

     

    创建service

     

    接口

    1. public interface EduSubjectService extends IService {
    2. }

    实现类

    1. @Service
    2. public class EduSubjectServiceImpl extends ServiceImpl 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 queryWrapper = new QueryWrapper<>();
    10. queryWrapper.orderByAsc("parent_id");
    11. List eduSubjectList = eduSubjectService.list(queryWrapper);
    12. //提供list集合用来缓存一级目录
    13. ArrayList resultsubject = new ArrayList<>();
    14. //创建map集合用来缓存
    15. Map 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. <script>
    2. //导入ajax
    3. import { findAll } from "@/api/edu/course"
    4. export default {
    5. name: 'WORKSPACE_NAMESubjectList',
    6. data() {
    7. return {
    8. result:[],
    9. loading:false
    10. };
    11. },
    12. mounted() {
    13. this.selectAll()
    14. },
    15. methods: {
    16. async selectAll() {
    17. this.loading = true
    18. let baseResult = await findAll()
    19. this.result = baseResult.data
    20. this.loading = false
    21. }
    22. },
    23. };
    24. script>
    25. <style lang="scss" scoped>
    26. 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 queryWrapper = new QueryWrapper<>();
    23. queryWrapper.orderByAsc("parent_id");
    24. List eduSubjectList = eduSubjectService.list(queryWrapper);
    25. //提供list集合用来缓存一级目录
    26. ArrayList resultsubject = new ArrayList<>();
    27. //创建map集合用来缓存
    28. Map 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. <script>
    2. //导入ajax
    3. import { findAll } from "@/api/edu/course"
    4. export default {
    5. name: 'WORKSPACE_NAMESubjectList',
    6. data() {
    7. return {
    8. result:[],
    9. loading:false
    10. };
    11. },
    12. mounted() {
    13. this.selectAll()
    14. },
    15. methods: {
    16. async selectAll() {
    17. this.loading = true
    18. let baseResult = await findAll()
    19. this.result = baseResult.data
    20. this.loading = false
    21. }
    22. },
    23. };
    24. script>
    25. <style lang="scss" scoped>
    26. style>

  • 相关阅读:
    编译链接(Compile Link)
    win 服务操作 删除服务
    北大肖臻老师《区块链技术与应用》系列课程学习笔记[9]比特币-思考
    在顺序表中使用顺序查找法查找某个关键字
    【毕业季·进击的技术er】努力只能及格,拼命才能优秀!
    Unity—项目小技术总结
    综述 | 关于点云配准的全面综述(二)
    经典/最新计算机视觉论文及代码推荐
    HTML网页设计制作——响应式网页影视动漫资讯bootstrap网页(9页)
    linux ssh 密钥配置
  • 原文地址:https://blog.csdn.net/m0_64550837/article/details/126712564