当前位置: 首页 > news >正文

天机学堂-分页查询

需求

分页查询我的课表

返回:

总条数、总页数、当前页的课表信息的集合

返回的VO(已经封装成统一的LearningLessonsVO)

定义Controller

@RestController
@RequestMapping("/lessons")
@RequiredArgsConstructor
public class LearningLessonController {private final ILearningLessonService lessonService;@GetMapping("/page")public PageDTO<LearningLessonVO> queryMyLesson(PageQuery query) {return lessonService.querMyLesson(query);}
}

2.service实现类

代码逻辑:

(1).因为要查询当前用户下的所有课程分页查询信息,所以首先要拿到用户userId

Long userId = UserContext.getUser();

(2)分页查询并作非空判断,将userId当作条件传给eq,传一个page对象给page方法.

//2.分页查询Page<LearningLesson> page = lambdaQuery().eq(LearningLesson::getUserId, userId).page(query.toMpPage("latest_learn_time", false));List<LearningLesson> records = page.getRecords();if (CollUtils.isEmpty(records)) {return PageDTO.empty(page);}

(3)

先根据从数据库里面查询出来的records拿到每个课程的id。

再根据id调用课程的接口查询每个id对应的课程信息。

判空处理

用stream流的方式将每个课程的id和对应的课程数据放在map中。

//3.查询课程信息//3.1获取课程idSet<Long> Ids = records.stream().map(LearningLesson::getCourseId).collect(Collectors.toSet());//3.2查询课程信息List<CourseSimpleInfoDTO> infoList = courseClient.getSimpleInfoList(Ids);if (CollUtils.isEmpty(infoList)) {throw new BadRequestException("课程信息不存在");}//3.3把课程集合处理成map,key是courseId,value是course本身Map<Long, CourseSimpleInfoDTO> cMap = infoList.stream().collect(Collectors.toMap(CourseSimpleInfoDTO::getId, c -> c));

 (4)

创建一个VO空数组

将po拷贝到VO中

将课程信息set到VO中

最后返回分页查询信息。

//4.封装vo返回List<LearningLessonVO> list = new ArrayList<>(records.size());//循环遍历,吧LearningLesson转变成VOfor (LearningLesson r : records) {//4.1拷贝基础属性到LearningLessonVOLearningLessonVO learningLessonVO = BeanUtils.copyProperties(r, LearningLessonVO.class);//4.2获取课程信息,填充到voCourseSimpleInfoDTO courseSimpleInfoDTO = cMap.get(r.getCourseId());learningLessonVO.setCourseName(courseSimpleInfoDTO.getName());learningLessonVO.setCourseCoverUrl(courseSimpleInfoDTO.getCoverUrl());learningLessonVO.setSections(courseSimpleInfoDTO.getSectionNum());list.add(learningLessonVO);}return PageDTO.of(page, list);

 

 @Overridepublic PageDTO<LearningLessonVO> querMyLesson(PageQuery query) {//1.获取当前登录用户Long userId = UserContext.getUser();//2.分页查询Page<LearningLesson> page = lambdaQuery().eq(LearningLesson::getUserId, userId).page(query.toMpPage("latest_learn_time", false));List<LearningLesson> records = page.getRecords();if (CollUtils.isEmpty(records)) {return PageDTO.empty(page);}//3.查询课程信息//3.1获取课程idSet<Long> Ids = records.stream().map(LearningLesson::getCourseId).collect(Collectors.toSet());//3.2查询课程信息List<CourseSimpleInfoDTO> infoList = courseClient.getSimpleInfoList(Ids);if (CollUtils.isEmpty(infoList)) {throw new BadRequestException("课程信息不存在");}//3.3把课程集合处理成map,key是courseId,value是course本身Map<Long, CourseSimpleInfoDTO> cMap = infoList.stream().collect(Collectors.toMap(CourseSimpleInfoDTO::getId, c -> c));//4.封装vo返回List<LearningLessonVO> list = new ArrayList<>(records.size());//循环遍历,吧LearningLesson转变成VOfor (LearningLesson r : records) {//4.1拷贝基础属性到LearningLessonVOLearningLessonVO learningLessonVO = BeanUtils.copyProperties(r, LearningLessonVO.class);//4.2获取课程信息,填充到voCourseSimpleInfoDTO courseSimpleInfoDTO = cMap.get(r.getCourseId());learningLessonVO.setCourseName(courseSimpleInfoDTO.getName());learningLessonVO.setCourseCoverUrl(courseSimpleInfoDTO.getCoverUrl());learningLessonVO.setSections(courseSimpleInfoDTO.getSectionNum());list.add(learningLessonVO);}return PageDTO.of(page, list);}

 

http://www.lqws.cn/news/85285.html

相关文章:

  • 21-CS61B-lab6:java文件操作以及持久化一见
  • PNG文件格式
  • 【技术支持】安卓开发中queryUsageStats不准确的问题
  • 【latex】易遗忘的表达
  • cpper 转 Golang
  • 【LLM】AI Agents vs. Agentic AI(概念应用挑战)
  • K-匿名模型
  • 英语中什么时候用that?
  • 电磁场与电磁波公式汇总
  • DeepSeek 部署中的常见问题及解决方案
  • 【Java EE初阶 --- 多线程(初阶)】多线程的实现案例
  • torch.randn vs torch.rand
  • win32相关(虚拟内存和物理内存)
  • 探索大语言模型(LLM):RSE流程详解——从文档中精准识别高相关片段
  • 第17讲、odoo18可视化操作代码生成模块
  • 基于springboot的家政服务预约系统
  • 算法-集合的使用
  • 统计字符数
  • 知识图谱系列(5):表示学习
  • 【Linux】pthread多线程基础
  • 【python深度学习】Day43 复习日
  • RNN循环网络:给AI装上“记忆“(superior哥AI系列第5期)
  • 快手可灵视频V1.6模型API如何接入免费AI开源项目工具
  • gcc编译构建流程-函数未定义问题
  • UE特效Niagara性能分析
  • 数据资产评估进阶:精读资产评估专家指引第9号——数据资产评估指导【附全文阅读】
  • 【Godot引擎】如何使用内置的全局搜索功能提升开发效率
  • selenium-自动更新谷歌浏览器驱动
  • Redis-6.2.9 cluster集群部署和扩容缩容
  • bismark OT CTOT OB CTOB 以及mapping后的bam文件中的XG,XR列的含义