mybatisplus的lambdaQuery()使用案例

一、常用

查询

// 方式一 条件是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.查询课表 注入别的Service
        LearningLesson 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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值