多表jpa
@Override
public PageVo<PageUserVo> user(Integer page1, Integer size1, String name, Integer roleId) {
Integer page = page1;
Integer size = size1;
page = VerificationUtils.checkPaging(page, size);
Sort sort = Sort.by(Sort.Direction.DESC, "id");
PageRequest pageRequest = PageRequest.of(page, size, sort);
Specification<UserEntity> userEntitySpecification=(root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (StringUtils.isNotBlank(name)) {
Path<String> name1 = root.get("username");
Path<String> realName = root.get("realName");
Predicate one = cb.like(name1, "%" + name + "%");
Predicate two = cb.like(realName, "%" + name + "%");
Predicate or = cb.or(one, two);
predicates.add(or);
}
if (roleId != null) {
Join<Object, Object> roles = root.join("roles",JoinType.LEFT);
Path<Integer> id = roles.get("id");
Predicate one = cb.equal(id, roleId);
predicates.add(one);
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
Page<UserEntity> pageUsers = productDao.findAll(userEntitySpecification,pageRequest);
List<PageUserVo> vos = pageUsers.getContent().stream().map(PageUserVo::mapping).collect(Collectors.toList());
return new PageVo<>(++page, size, pageUsers.getTotalElements(), vos);
}