簡體   English   中英

如何修改org.springframework.data.jpa.domain.Specification對象?

[英]How to modify a org.springframework.data.jpa.domain.Specification object?

我已經在@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助手類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM