将WHERE IN子句添加到JPA规范中

时间:2019-07-09 20:03:49

标签: spring-boot jpa spring-data-jpa jpa-2.0 jpa-2.1

我正在尝试实现受IN子句限制的搜索功能:

我想实施具有过滤器限制的搜索实现:

    @GetMapping("find")
    public Page<MerchantUserDTO> getAllBySpecification(
            @And({
                @Spec(path = "name", spec = LikeIgnoreCase.class),
                @Spec(path = "login", spec = LikeIgnoreCase.class),
                @Spec(path = "email", spec = LikeIgnoreCase.class),
            }) Specification<Users> specification,
            @SortDefault(sort = "login", direction = Sort.Direction.DESC) Pageable pageable
    ) {        
        return merchantUserService.getAllBySpecification(specification, pageable)
                .map(g -> MerchantUserDTO.builder()                   
                        .id(g.getId())
                        .login(g.getLogin())                        
                        .build()
                );
    }

    @Override
    public Page<Users> getAllBySpecification(Specification<Users> specification, Pageable pageable) {
        return dao.findAllByTypeIn(specification, pageable, "MerchantUser");
    }

存储库:

@Repository
public interface MerchantUserRepository extends JpaRepository<Users, Integer>, JpaSpecificationExecutor<Users> {

    Page<Users> findAllByTypeIn(Pageable page, String... types);

    Page<Users> findAllByTypeIn(Specification<Users> specification, Pageable pageable, String... types);
}

用IN子句扩展规范的正确方法是什么?

specification.and(path.in(types))路径是一个属性,但是如何正确实现呢?

1 个答案:

答案 0 :(得分:1)

通常可以通过以下方式实现:

1)创建规范实现

public class MerchantUserSpecification implements Specification<Users> {

    private final List<String> types;

    public MerchantUserSpecification(List<String> types) {
        this.types = types;
    }

    @Override
    public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        if (types != null && !types.isEmpty()) {
            return root.get(Users_.type).in(types);
        } else {
            // always-true predicate, means that no filtering would be applied
            return cb.and(); 
        }
    }

2)使用从JpaSpecificationExecutor界面继承的方法Page findAll(@Nullable Specification spec, Pageable pageable);,而不是使用自定义的findAllByTypeIn(Specification<Users> specification....)

@Override
public Page<Users> getAllBySpecification(Specification<Users> specification, Pageable pageable) {
    // combine original specification (passed from outside) and filter-by-types specification
    Specification<Users> finalSpec = specification
           .and(new MerchantUserSpecification(Arrays.asList("MerchantUser")))
    return dao.findAll(finalSpec, pageable)
}

PS

在Java 8+和简单情况下(例如您的情况),代码可能会减少得更多。不必在单独的类中实现Specification<T>,而只需创建方法

private Specification<Users> typeIn(List<String> types) {
    return (root, query, cb) -> {
        if (types != null && !types.isEmpty()) {
           return root.get(Users_.type).in(types);
        } else {
           // always-true predicate, means that no filtering would be applied
           return cb.and(); 
        }
    }
}

@Override
public Page<Users> getAllBySpecification(Specification<Users> specification, Pageable pageable) {
    // combine original specification (passed from outside) and filter-by-types specification
    Specification<Users> finalSpec = specification
            .and(typeIn(Arrays.asList("MerchantUser")))
    return dao.findAll(finalSpec, pageable)
}

更新:最快捷的方法

@Override
public Page<Users> getAllBySpecification(Specification<Users> specification, Pageable pageable) {
    // combine original specification (passed from outside) and filter-by-types specification
    Specification<Users> finalSpec = specification
            .and((root, query, cb) -> root.get(Users_.type).in(Arrays.asList("MerchantUser"))
    return dao.findAll(finalSpec, pageable)
}