首页 > 解决方案 > 是什么创建了准确的实现:自定义存储库或为多个参数扩展 JpaSpecificationExecutor

问题描述

我正在尝试获取具有多个参数的实体的所有元素,例如“汽车”,例如工厂、品牌、供应商、类别(例如 SUV)等......(列表稍长)。我要做的是创建一个自定义存储库并添加我对自定义功能的描述:

public interface CarRepositoryCustom {
    List<CarEntity> getVehicleSegment(Map<String, Object> params);
}

我的存储库就像:

@Repository
public interface CarRepository extends JpaRepository<CarEntity, Long>, CarRepositoryCustom {

}

问题是我查看了 spring data 的官方文档,并通过扩展 JpaSpecificationExecutor 找到了另一种方法:

public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {

}

附加接口具有允许您以多种方式运行规范的方法。例如,findAll 方法返回所有符合规范的实体,如下例所示:

List<T> findAll(Specification<T> spec);

规范接口定义如下:

public interface Specification<T> {
  Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
            CriteriaBuilder builder);
}

我的问题是:准确的解决方案是什么?

标签: springspring-bootspring-data-jpaspring-data

解决方案


推荐阅读