【项目学习01_2024.05.05_Day05】

学习笔记

    • [4.3 接口开发](#4.3 接口开发)
      • [4.3.1 树型表查询](#4.3.1 树型表查询)
      • [4.3.2 开发Mapper](#4.3.2 开发Mapper)
      • [4.3.3 开发Service](#4.3.3 开发Service)
      • [4.3.4 测试Service](#4.3.4 测试Service)
    • [4.4 接口测试](#4.4 接口测试)
      • [4.4.1 接口层代码完善](#4.4.1 接口层代码完善)
      • [4.4.2 测试接口](#4.4.2 测试接口)

4.3 接口开发

4.3.1 树型表查询

4.3.2 开发Mapper

  1. 在对应的Mapper里定义一个方法
  2. 在同名的xml文件里具体定义相应的sql语句

4.3.3 开发Service

  1. 编写CourseCategoryService.java
  1. 编写对应的实现类
java 复制代码
package com.xuecheng.content.service.impl;

import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.model.dto.CourseCategoryTreeDto;
import com.xuecheng.content.service.CourseCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Sgy
 * @version 1.0
 * @date 2024/5/5 17:07
 * @description
 */
@Slf4j
@Service
public class CourseCategoryServiceImpl implements CourseCategoryService {

    @Autowired
    CourseCategoryMapper courseCategoryMapper;

    public List<CourseCategoryTreeDto> queryTreeNodes(String id){
        List<CourseCategoryTreeDto> courseCategoryTreeDtos = courseCategoryMapper.selectTreeNodes(id);

        //将list转map,以备使用,排除根节点
        Map<String,CourseCategoryTreeDto> mapTemp = courseCategoryTreeDtos.stream().filter(item->!id.equals(item.getId())).collect(Collectors.toMap(key -> key.getId(),value -> value,(key1,key2) -> key2));
        //最终返回的list
        List<CourseCategoryTreeDto> categoryTreeDtos = new ArrayList<>();
        //依次遍历每个元素,排除根节点
        courseCategoryTreeDtos.stream().filter(item->!id.equals(item.getId())).forEach(item->{
            if(item.getParentid().equals(id)){
                categoryTreeDtos.add(item);
            }
            //找到当前节点的父亲节点
            CourseCategoryTreeDto courseCategoryTreeDto = mapTemp.get(item.getParentid());
            if(courseCategoryTreeDto != null){
                if(courseCategoryTreeDto.getChildrenTreeNodes() == null){
                    courseCategoryTreeDto.setChildrenTreeNodes(new ArrayList<CourseCategoryTreeDto>());
                }
                //下边开始往ChildrenTreeNodes属性中放子节点
                courseCategoryTreeDto.getChildrenTreeNodes().add(item);
            }
        });
        return categoryTreeDtos;
    }
}

4.3.4 测试Service

java 复制代码
@SpringBootTest
class CourseCategoryServiceTests {

    @Autowired
    CourseCategoryService courseCategoryService;


    @Test
    void testqueryTreeNodes() {
        List<CourseCategoryTreeDto> categoryTreeDtos = courseCategoryService.queryTreeNodes("1");
        System.out.println(categoryTreeDtos);
    }

}

4.4 接口测试

4.4.1 接口层代码完善

4.4.2 测试接口

  1. 运行

    2.生成响应的json文件
  2. 完成前后端连调:
    打开前端工程,进入新增课程页面。
    课程分类下拉框可以正常显示

相关推荐
吴声子夜歌1 分钟前
Guava——集合(二)
java·guava
xiaoqiMikko1 分钟前
一句“为了保持同步”的注释,正好打破了同步 —— Netty 响应错配漏洞实测
java
疯狂打码的少年6 分钟前
【数据库技术】SQL高级查询(分组/排序/聚合函数)
java·数据库·笔记·sql
DDXYcoder7 分钟前
万字解析动态内存从编译到运行
java·前端·算法
旖旎夜光8 分钟前
【AI入门】大模型介绍全解析:从模型、LLM 到提示词与嵌入
人工智能·笔记·python·学习·ai编程
xxwl58521 分钟前
Git常用命令的学习
git·学习
具身AGI25 分钟前
从演示到作业,物理AI 人类学习路线 的深意
人工智能·学习
MSTcheng.27 分钟前
【Linux】Linux学习第三弹——Linux基本指令2
linux·windows·学习·ubuntu·操作系统
坐吃山猪29 分钟前
【多线程】什么时候必须/推荐使用Thread类
java·开发语言
tju新生代魔迷36 分钟前
Verilog HDL学习笔记(三)| 第3章 基本概念(上)——词法约定与数据类型
笔记·学习