java - 发送空搜索规范

标签 java spring jpa spring-data-jpa spring-data

@GetMapping("find")
    public Page<PaymentTransactionsDTO> getAllBySpecification(
            @And({
                    @Spec(path = "unique_id", spec = LikeIgnoreCase.class),
                    @Spec(path = "merchant_id", spec = In.class),                   
                    @Spec(path = "createdAt", params = "from", spec = GreaterThanOrEqual.class, config="uuuu-MM-dd'T'HH:mm:ss.SSSX"),
                    @Spec(path = "createdAt", params = "to", spec = LessThanOrEqual.class, config="uuuu-MM-dd'T'HH:mm:ss.SSSX")
            }) Specification<PaymentTransactions> specification,
            @SortDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable,
            Authentication authentication) {

        return transactionService.getAllBySpecificationByTerminalId(specification, pageable)
                  .map(g -> PaymentTransactionsDTO.builder()                      
                            .id(g.getId()) 

                            .build()
                    );       
    }

实现:

    @Override
    public Page<PaymentTransactions> getAllBySpecificationByTerminalId(Specification<PaymentTransactions> specification,
            Pageable pageable ) {           
        Specification<PaymentTransactions> finalSpec = specification
                .and(typeIn( ));
        return dao.findAll(finalSpec, pageable);
    }

    private Specification<PaymentTransactions> typeIn( ) {

        List<Integer> terminal_idsdd = new ArrayList<>();
        terminal_idsdd.add(45);
        terminal_idsdd.add(35); 

        return (root, query, cb) -> {
            if (terminal_idsdd != null && !terminal_idsdd.isEmpty()) {
               return root.get(PaymentTransactions_.terminalId).in(terminal_idsdd);
            } else {
               // always-true predicate, means that no filtering would be applied
               return cb.and(); 
            }
        };
    }

当我设置默认值时,它工作正常,但搜索不正确。

@Spec(path = "unique_id", spec = LikeIgnoreCase.class, defaultVal="123"),

默认情况下,不会从 UI 发送任何搜索参数,因此我正在考虑创建类似空规范对象的内容,如下所示:

@Override
        public Page<PaymentTransactions> getAllBySpecificationByTerminalId(Specification<PaymentTransactions> specification,
                Pageable pageable ) {

            if (specification == null) {
                Specification<PaymentTransactions> specification = new Specification<PaymentTransactions>();
            }

            Specification<PaymentTransactions> finalSpec = specification
                    .and(typeIn( ));
            return dao.findAll(finalSpec, pageable);
        }

您能指导我如何解决这个问题吗?

最佳答案

我相信您使用的所有 http 参数都是可选的(默认情况下所有 http 参数都是可选的)。所以很明显,当没有传递参数时,规范将为空。所以你需要手动处理这个 null 情况。我认为你的方法是正确的,但在下面发布一个简单的建议。希望您会发现它有帮助。

您可以简单地执行此操作,而不是产生空对象的开销。

@Override
        public Page<PaymentTransactions> getAllBySpecificationByTerminalId(Specification<PaymentTransactions> specification,
                Pageable pageable ) {

            Specification<PaymentTransactions> finalSpec = null;

            if (specification == null) {
                finalSpec = typeIn();
            } else {

                finalSpec = specification
                                 .and(typeIn( ));
            }
            return dao.findAll(finalSpec, pageable);
        }

其他

您可以将某些参数设置为非可选,以完全消除规范变为空的可能性,因为客户端将被迫传递这些参数。

关于java - 发送空搜索规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57322787/

相关文章:

java - 如何在浏览器中过滤Java Applet Console的输出?

java - 在 Spring 项目中禁用 Apache Commons 日志记录

java - 在 Spring Boot 中将 @Bean 放在哪里?

spring - spring-boot v 1.3 的最低 spring 版本

java - 如何使用 Spring Data REST 在 OneToMany 关系中添加对象

java - LocalDate 没有构造函数...我们如何创建没有构造函数的类?

mysql - 列表中有条件的 Hibernate SQLQuery

java - JPA无法更新实体

java - JPA - JpaRepository 子记录具有父 ID 而不是实体记录,如果父记录已获取

java - Spring @Entity 类中的注释顺序