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

天机学堂-分页查询

需求

分页查询我的课表

返回:

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

返回的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.lryc.cn/news/2403616.html

相关文章:

  • 业态即战场:零售平台的生意模型与系统设计解构
  • 微算法科技(NASDAQ:MLGO)基于信任的集成共识和灰狼优化(GWO)算法,搭建高信任水平的区块链网络
  • 全新Xsens Animate版本是迄今为止最大的软件升级,提供更清晰的数据、快捷的工作流程以及从录制开始就更直观的体验
  • 大语言模型评测体系全解析(下篇):工具链、学术前沿与实战策略
  • python打卡day46@浙大疏锦行
  • C++.OpenGL (1/64) 创建窗口(Hello Window)
  • Excel 发现此工作表中有一处或多处公式引用错误。请检查公式中的单元格引用、区域名称、已定义名称以及到其他工作簿的链接是否均正确无误。弹窗
  • NVIDIA DRIVE AGX平台:引领智能驾驶安全新时代
  • 推荐12个wordpress企业网站模板
  • 沙市区举办资本市场赋能培训会 点赋科技分享智能消费新实践
  • Docker 容器化基础:镜像、容器与仓库的本质解析
  • 九.C++ 对引用的学习
  • 探秘鸿蒙 HarmonyOS NEXT:实战用 CodeGenie 构建鸿蒙应用页面
  • art-pi2 上手记录(二)
  • 数据库SQLite基础
  • 1.3 古典概型和几何概型
  • html-pre标签
  • 【WPF】WPF 项目实战:用ObservableCollection构建一个可增删、排序的管理界面(含源码)
  • MCU_IO驱动LED
  • 上门预约行业技术方案全解析:小程序、App还是H5?如何选择?
  • Java 集合面试题 PDF 及常见考点解析与备考指南
  • Java 大视界 -- 基于 Java 的大数据分布式计算在蛋白质组学数据分析中的加速与优化(255)
  • 如何通过外网访问内网?哪个方案比较好用?跨网远程连接网络知识早知道
  • Vue.js教学第十八章:Vue 与后端交互(二):Axios 拦截器与高级应用
  • C#中datagridview单元格value为{}大括号
  • 46、web实验-遍历数据与页面bug修改
  • 华为OD机试_2025 B卷_数组去重和排序(Python,100分)(附详细解题思路)
  • 云计算 Linux Rocky day03(which、快捷键、mount、家目录、ls、alias、mkdir、rm、mv、cp、grep)
  • gh hugging face使用
  • SQL Server全局搜索:在整个数据库中查找特定值的高效方法