天机学堂-分页查询
需求
分页查询我的课表
返回:
总条数、总页数、当前页的课表信息的集合
返回的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);}