根据条件查询符合条件的list集合时,最后再将查询到的id转化为List集合,下面是jdk8的stream流写法。

List<Long> roleIdList = Optional.ofNullable(userRoleService.findList(userRoleDTO.builder()
                .roleId(id).build())).orElseGet(() -> new ArrayList<UserRole>()).stream()
                .map(UserRole::getRoleId).collect(Collectors.toList());

如果集合不为空,则查询未关联的角色,如果集合为空,则查询全部

if (Objects.nonNull(roleIdList) && !roleIdList.isEmpty()) {
            return userService.findList(UserDTO.builder().excludeIdList(mainUserIdList).build());
        }
        return userService.findList(UserDTO.builder().build());;

JPA排除id查询条件编写

List<Long> excludeIdList = roleDTO.getExcludeIdList();
        if (null != excludeIdList && !excludeIdList.isEmpty()) {
            Path<Long> idSearchPath = root.get(Role_.id);
            CriteriaBuilder.In<Long> in = criteriaBuilder.in(idSearchPath);
            excludeIdList.stream().forEach(id -> in.value(id));
            if (null != where) {
                where = criteriaBuilder.and(where, criteriaBuilder.not(in));
            } else {
                where = criteriaBuilder.not(in);
            }
        }
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐