spring - 是什么创建了准确的实现:自定义存储库或为多个参数扩展 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);
}
我的问题是:准确的解决方案是什么?
解决方案
推荐阅读
- ethereum - 我们有两种方式来启动一个 web3 实例,我们选择哪一种更好呢?
- python - 如何在 Python 3.8 的 dbf 文件中写入/附加列?
- algorithm - 在锦标赛排序算法中排序八个数字的最少比较时间是多少?(考虑坏情况)
- amazon-web-services - 适用于 USER_PASSWORD_AUTH 流的 AWS Cognito 开发工具包
- amazon-web-services - EC2 最便宜的地区
- api - Shopware 6 使用 API 的产品变体图像上传问题
- javascript - 使用 getStaticPaths 和 getStaticProps 在 nextjs 中使用 Airtable 中的表中的数据进行分页
- go - 如何将日期字符串绑定到结构?
- android - 我什么时候需要在 Android Studio Compose 中添加 @Composable?
- npm - Why cypress module is not detecting while running sorry-cypress run command?