JDK8stream()流简写根据条件查询并将id转化为集合方法
根据条件查询符合条件的list集合时,最后再将查询到的id转化为List集合,下面是jdk8的stream流写法。List<Long> roleIdList = Optional.ofNullable(userRoleService.findList(userRoleDTO.builder().roleId(id).build())).orElseGet(() -> new Ar
·
根据条件查询符合条件的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);
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)