Search in sources :

Example 1 with LambdaQueryChainWrapper

use of com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper in project sduoj-server by SDUOJ.

the class ProblemManageService method queryDescriptionList.

public List<ProblemDescriptionListDTO> queryDescriptionList(String problemCode, UserSessionDTO userSessionDTO) {
    ProblemDO problemDO = problemDao.lambdaQuery().select(ProblemDO::getProblemId, ProblemDO::getDefaultDescriptionId, ProblemDO::getUserId).eq(ProblemDO::getProblemCode, problemCode).one();
    AssertUtils.notNull(problemDO, ApiExceptionEnum.PROBLEM_NOT_FOUND);
    Long defaultDescriptionId = problemDO.getDefaultDescriptionId();
    LambdaQueryChainWrapper<ProblemDescriptionDO> query = problemDescriptionDao.lambdaQuery().select(ProblemDescriptionDO::getId, ProblemDescriptionDO::getIsPublic, ProblemDescriptionDO::getProblemId, ProblemDescriptionDO::getVoteNum, ProblemDescriptionDO::getUserId, ProblemDescriptionDO::getTitle);
    // 题目管理员能看到所有,admin 只能看到公开/默认/自己的题面
    if (!problemCommonService.isProblemManager(problemDO, userSessionDTO)) {
        Long userId = Optional.ofNullable(userSessionDTO).map(UserSessionDTO::getUserId).orElse(null);
        query.and(o1 -> o1.eq(ProblemDescriptionDO::getIsPublic, 1).or(o2 -> o2.eq(ProblemDescriptionDO::getIsPublic, 0).eq(ProblemDescriptionDO::getUserId, userId)).or(o3 -> o3.eq(ProblemDescriptionDO::getId, defaultDescriptionId)));
    }
    List<ProblemDescriptionDO> problemDescriptionDOList = query.eq(ProblemDescriptionDO::getProblemId, problemDO.getProblemId()).list();
    List<ProblemDescriptionListDTO> problemDescriptionDTOList = problemDescriptionListConverter.to(problemDescriptionDOList);
    problemDescriptionDTOList.forEach(o -> {
        o.setProblemCode(problemCode);
        o.setUsername(userClient.userIdToUsername(o.getUserId()));
    });
    return problemDescriptionDTOList;
}
Also used : ProblemDescriptionDO(cn.edu.sdu.qd.oj.problem.entity.ProblemDescriptionDO) LambdaQueryChainWrapper(com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper) java.util(java.util) LambdaUpdateChainWrapper(com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper) Autowired(org.springframework.beans.factory.annotation.Autowired) PermissionEnum(cn.edu.sdu.qd.oj.auth.enums.PermissionEnum) ApiExceptionEnum(cn.edu.sdu.qd.oj.common.enums.ApiExceptionEnum) PageResult(cn.edu.sdu.qd.oj.common.entity.PageResult) JudgeTemplateListDTO(cn.edu.sdu.qd.oj.judgetemplate.dto.JudgeTemplateListDTO) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) cn.edu.sdu.qd.oj.common.util(cn.edu.sdu.qd.oj.common.util) Service(org.springframework.stereotype.Service) cn.edu.sdu.qd.oj.problem.dto(cn.edu.sdu.qd.oj.problem.dto) CheckpointManageService(cn.edu.sdu.qd.oj.checkpoint.service.CheckpointManageService) JudgeTemplateService(cn.edu.sdu.qd.oj.judgetemplate.service.JudgeTemplateService) ProblemDO(cn.edu.sdu.qd.oj.problem.entity.ProblemDO) ProblemManageListDO(cn.edu.sdu.qd.oj.problem.entity.ProblemManageListDO) ProblemDescriptionDao(cn.edu.sdu.qd.oj.problem.dao.ProblemDescriptionDao) Collectors(java.util.stream.Collectors) UserClient(cn.edu.sdu.qd.oj.problem.client.UserClient) UserSessionDTO(cn.edu.sdu.qd.oj.common.entity.UserSessionDTO) ProblemDao(cn.edu.sdu.qd.oj.problem.dao.ProblemDao) ProblemManageListDao(cn.edu.sdu.qd.oj.problem.dao.ProblemManageListDao) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) Slf4j(lombok.extern.slf4j.Slf4j) ApiException(cn.edu.sdu.qd.oj.common.exception.ApiException) cn.edu.sdu.qd.oj.problem.converter(cn.edu.sdu.qd.oj.problem.converter) Lists(org.assertj.core.util.Lists) TagDTO(cn.edu.sdu.qd.oj.tag.dto.TagDTO) CheckpointDTO(cn.edu.sdu.qd.oj.checkpoint.dto.CheckpointDTO) Transactional(org.springframework.transaction.annotation.Transactional) ProblemDO(cn.edu.sdu.qd.oj.problem.entity.ProblemDO) ProblemDescriptionDO(cn.edu.sdu.qd.oj.problem.entity.ProblemDescriptionDO)

Example 2 with LambdaQueryChainWrapper

use of com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper in project demo-SpringBoot by Max-Qiu.

the class TestLambdaQueryWrapper method testLambdaQueryChainWrapper.

/**
 * 链式写法,
 */
@Test
void testLambdaQueryChainWrapper() {
    // SELECT id,username,age,email FROM smp_user WHERE (age = ?)
    List<User> list = new LambdaQueryChainWrapper<>(userMapper).eq(User::getAge, 18).list();
    list.forEach(System.out::println);
    // SELECT COUNT(*) FROM smp_user WHERE (age = ?)
    // SELECT id,username,age,email,create_time FROM smp_user WHERE (age = ?) LIMIT ?
    Page<User> page = new LambdaQueryChainWrapper<>(userMapper).eq(User::getAge, 18).page(new Page<>(1, 10));
    System.out.println(page.getTotal());
    // SELECT COUNT( * ) FROM smp_user WHERE (age = ?)
    long count = new LambdaQueryChainWrapper<>(userMapper).eq(User::getAge, 18).count();
    System.out.println(count);
    // SELECT id,username,age,email,create_time FROM smp_user WHERE (age = ?)
    User one = new LambdaQueryChainWrapper<>(userMapper).eq(User::getAge, 18).one();
    System.out.println(one);
}
Also used : User(com.maxqiu.demo.entity.User) LambdaQueryChainWrapper(com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with LambdaQueryChainWrapper

use of com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper in project demo-SpringBoot by Max-Qiu.

the class TestWrappers method testWrapper.

@Test
void testWrapper() {
    // 创建一个空条件的查询构造器(其实用不到,条件为空的话可以直接传null,比如 userMapper.selectList(null))
    QueryWrapper<User> emptyWrapper = Wrappers.emptyWrapper();
    // 创建一个查询构造器(推荐用Lambda,毕竟queryWrapper.eq("username", "max")这种写String的方式看起来不咋地)
    QueryWrapper<User> queryWrapper1 = Wrappers.query();
    QueryWrapper<User> queryWrapper2 = Wrappers.query(new User());
    // 创建一个支持Lambda语法查询构造器(推荐,详细区别见下方代码)
    LambdaQueryWrapper<User> lambdaQueryWrapper1 = Wrappers.lambdaQuery();
    // 如果不使用对象接收直接链式...写法,可以使用下面这种写法设置泛型
    Wrappers.<User>lambdaQuery();
    LambdaQueryWrapper<User> lambdaQueryWrapper3 = Wrappers.lambdaQuery(User.class);
    LambdaQueryWrapper<User> lambdaQueryWrapper4 = Wrappers.lambdaQuery(new User());
    // 创建一个支持Lambda语法且支持链式写法的查询构造器
    LambdaQueryChainWrapper<User> lambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(userMapper);
    UpdateWrapper<User> updateWrapper1 = Wrappers.update();
    UpdateWrapper<User> updateWrapper2 = Wrappers.update(new User());
    LambdaUpdateWrapper<User> lambdaUpdateWrapper1 = Wrappers.lambdaUpdate();
    LambdaUpdateWrapper<User> lambdaUpdateWrapper2 = Wrappers.lambdaUpdate(new User());
    LambdaUpdateWrapper<User> lambdaUpdateWrapper3 = Wrappers.lambdaUpdate(User.class);
    LambdaUpdateChainWrapper<User> lambdaUpdateChainWrapper = new LambdaUpdateChainWrapper<>(userMapper);
}
Also used : User(com.maxqiu.demo.entity.User) LambdaUpdateChainWrapper(com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper) LambdaQueryChainWrapper(com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

LambdaQueryChainWrapper (com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper)3 LambdaUpdateChainWrapper (com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper)2 User (com.maxqiu.demo.entity.User)2 Test (org.junit.jupiter.api.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 PermissionEnum (cn.edu.sdu.qd.oj.auth.enums.PermissionEnum)1 CheckpointDTO (cn.edu.sdu.qd.oj.checkpoint.dto.CheckpointDTO)1 CheckpointManageService (cn.edu.sdu.qd.oj.checkpoint.service.CheckpointManageService)1 PageResult (cn.edu.sdu.qd.oj.common.entity.PageResult)1 UserSessionDTO (cn.edu.sdu.qd.oj.common.entity.UserSessionDTO)1 ApiExceptionEnum (cn.edu.sdu.qd.oj.common.enums.ApiExceptionEnum)1 ApiException (cn.edu.sdu.qd.oj.common.exception.ApiException)1 cn.edu.sdu.qd.oj.common.util (cn.edu.sdu.qd.oj.common.util)1 JudgeTemplateListDTO (cn.edu.sdu.qd.oj.judgetemplate.dto.JudgeTemplateListDTO)1 JudgeTemplateService (cn.edu.sdu.qd.oj.judgetemplate.service.JudgeTemplateService)1 UserClient (cn.edu.sdu.qd.oj.problem.client.UserClient)1 cn.edu.sdu.qd.oj.problem.converter (cn.edu.sdu.qd.oj.problem.converter)1 ProblemDao (cn.edu.sdu.qd.oj.problem.dao.ProblemDao)1 ProblemDescriptionDao (cn.edu.sdu.qd.oj.problem.dao.ProblemDescriptionDao)1 ProblemManageListDao (cn.edu.sdu.qd.oj.problem.dao.ProblemManageListDao)1