我已经在@RestController中实现了Specification,以在Specificaton对象中设置值。

public ResponseEntity<ServiceResponse> searchUnexpectedLikelyToChurn(@RequestHeader HttpHeaders headers,
            @PageableDefault(page = 0, size = 10, sort = "buyer", direction = Direction.ASC) Pageable pageable,
            @Conjunction(value = {
                    @Or({
                        @Spec(path="buyer", params="search", spec=LikeIgnoreCase.class),
                        @Spec(path = "email", params = "search", spec = LikeIgnoreCase.class),
                        @Spec(path = "shipCity", params = "search", spec = LikeIgnoreCase.class),
                        @Spec(path = "shipState", params = "search", spec = LikeIgnoreCase.class),
                        @Spec(path = "country", params = "search", spec = LikeIgnoreCase.class)
                    })}, and = @Spec(path = "company",params = "company", spec = Equal.class)) Specification<CustomerEntity> customerSpec


而且我已经使用了这个库[https://github.com/tkaczmarzyk/specification-arg-resolver#enabling-spec-annotations-in-your-spring-app]

现在,我需要在此Specification对象上添加另一个条件,它是一个java.util.Date对象,它是根据代码而不是用户输入计算的。如何添加呢?

我当前的DAO实现是:

 @Repository
public interface CustomerDao extends JpaRepository<CustomerEntity, Integer>, JpaSpecificationExecutor<CustomerEntity> {
}

最佳答案

您可以使用org.springframework.data.jpa.domain.Specification界面,如下所示:

Specification combinedSpec - Specification.where(customerSpec)
                                          .and(calculateAdditionalSpec());


如果使用旧版本的Spring Data JPA,请使用Specifications帮助器类。

08-03 17:18