一、常用
查询
// 方式一 条件是LearningLesson必须为LearningLessonServiceImpl的T
LearningLesson lesson = lambdaQuery().eq(LearningLesson::getUserId, userId).eq(LearningLesson::getStatus, LessonStatus.LEARNING.getValue()).orderByDesc(LearningLesson::getLatestLearnTime).last("limit 1").one();// 方式二
LambdaQueryWrapper<LearningLesson> wrapper = Wrappers.<LearningLesson>lambdaQuery().eq(LearningLesson::getUserId, userId).eq(LearningLesson::getStatus, LessonStatus.LEARNING.getValue()).orderByDesc(LearningLesson::getLatestLearnTime).last("limit 1");LearningLesson lesson = learningLessonMapper.selectOne(wrapper);// 方式三
Integer weekFinished = recordMapper.selectCount(new LambdaQueryWrapper<LearningRecord>().eq(LearningRecord::getUserId, userId).eq(LearningRecord::getFinished, true).gt(LearningRecord::getFinishTime, begin).lt(LearningRecord::getFinishTime, end)
);
分页
// 方式一 条件是LearningLesson必须为LearningLessonServiceImpl的T
Page<LearningLesson> pageInfo = new Page<>(query.getPageNo(), query.getPageSize());Page<LearningLesson> resultPage = this.lambdaQuery() //加不加this都行.eq(LearningLesson::getUserId, userId).orderByDesc(LearningLesson::getCreateTime);.page(pageInfo);//方式二
Page<LearningLesson> pageInfo = new Page<>(query.getPageNo(), query.getPageSize());LambdaQueryWrapper<LearningLesson> wrapper= Wrappers.<LearningLesson>lambdaQuery().eq(LearningLesson::getUserId, userId).orderByDesc(LearningLesson::getCreateTime);Page<LearningLesson> resultPage = learningLessonMapper.selectPage(pageInfo,wrapper);
为了用方式一,可以在service里注入别的service,但是注意要防止循环依赖,可以用mapper
public LearningLessonDTO queryLearningRecordByCourse(Long courseId){// 1.获取登录用户Long userId = UserContext.getUser();// 2.查询课表 注入别的ServiceLearningLesson lesson = lessonService.queryByUserAndCourseId(userId, courseId);// 3.查询学习记录// select * from xx where lesson_id = #{lessonId}List<LearningRecord> records = lambdaQuery().eq(LearningRecord::getLessonId, lesson.getId()).list();// 4.封装结果LearningLessonDTO dto = new LearningLessonDTO();dto.setId(lesson.getId());dto.setLatestSectionId(lesson.getLatestSectionId());dto.setRecords(BeanUtils.copyList(records, LearningRecordDTO.class));return dto;}
获取自己的mapper,getBaseMapper()
二、其它
时间
// 获取本周开始和结束时间LocalDateTime begin = DateUtils.getWeekBeginTime(LocalDate.now());LocalDateTime end = DateUtils.getWeekEndTime(LocalDate.now());//2. 查询 当前用户 本周学习积分// TODO//3. 查询当前用户本周完成总数量// select count(*) from learning_record where userid=? and finished=true and finishs_time between (start,end);LambdaQueryWrapper<LearningRecord> wrapper = new LambdaQueryWrapper();wrapper.eq(LearningRecord::getUserId,userId);wrapper.eq(LearningRecord::getFinished,true);wrapper.ge(LearningRecord::getFinishTime,begin);wrapper.le(LearningRecord::getFinishTime,end);Integer weekFinished = recordMapper.selectCount(wrapper);