MyBatis条件构造器

我目前比较中意的写法:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19

        // 查询思路:table.id = fieldId and (table.orgId = orgId or table.orgId = 超管orgId)
        LambdaQueryWrapper<FieldPo> queryWrapper = new LambdaQueryWrapper<FieldPo>()
                .eq(FieldPo::getId, fieldId)
                .and(queryWrapperInner -> queryWrapperInner
                        .eq(FieldPo::getOrgId, orgId)
                        .or()
                        .eq(FieldPo::getOrgId, SystemAdmin.ORG_ID.getOrgId()));

        FieldPo fieldPo = fieldDao.selectOne(queryWrapper);

        if (fieldPo == null) {
            throw new ApiException(
                    ExceptionCode.FIELD_NOT_FOUND.getCode(),
                    ExceptionCode.FIELD_NOT_FOUND.getMsg());
        }

        return fieldPo;

参考资料

  1. Mybatis Plus中的lambdaQueryWrapper条件构造图介绍