繁体   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