为了账号安全,请及时绑定邮箱和手机立即绑定

使用条件实现搜索过滤器

使用条件实现搜索过滤器

慕标5832272 2022-11-02 10:06:03
我想用几个子条件实现搜索功能。我试过这个:    @GetMapping("find")    public Page<PaymentTransactionsDTO> getAllBySpecification(            @And({                    @Spec(path = "name", spec = LikeIgnoreCase.class),                    @Spec(path = "unique_id", spec = LikeIgnoreCase.class),                    @Spec(path = "createdAt", params = "from", spec = GreaterThanOrEqual.class),                    @Spec(path = "createdAt", params = "to", spec = LessThanOrEqual.class)            }) Specification<PaymentTransactions> specification,            Pageable pageable    ) {                return transactionService.getAllBySpecification(specification, pageable));           }存储库:      @Override      public Page<PaymentTransactions> getAllBySpecification(final Specification<PaymentTransactions> specification, final Pageable pageable) {          return dao.findAll(specification, pageable);      }目前此请求正在运行:GET /api/transactions/find?unique_id=22&page=0&size=10 但我也想实现这些额外的搜索条件,不仅发送基本搜索unique_id:start with =end with contains使用https://github.com/tkaczmarzyk/specification-arg-resolver有没有办法发送额外的子条件?我一般找不到这个问题的解决方案,发送这些值的最佳做法是什么?
查看完整描述

1 回答

?
森栏

TA贡献1810条经验 获得超5个赞

如果您想创建非常特殊的过滤器,我相信您应该从发明搜索界面开始。例如像这样:


GET /models?name=eq(john smith)&createdAt=between(2019-01-01,2019-01-31)

GET /models?name=like(sm)&createdAt=from(2019-01-01)

GET /models?name=sw(john)&createdAt=to(2019-01-31)

之后,您将能够尝试实施它。


IMO 解决此类任务的最佳方法是使用 Spring Data JPA Specifications(和 JPA Criteria API)。例如:


1)让我们创建一个为我们的实体Filter实现的类:SpecificationModel


@Value

public class ModelFilter implements Specification<Model> {


    private String name;

    private String createdAt;


    @Override

    public Predicate toPredicate(Root<Model> root, CriteriaQuery<?> query, CriteriaBuilder builder) {


        List<Predicate> predicates = new ArrayList<>();


        // Prepare predicates and fill the list with them...


        return builder.and(predicates.toArray(new Predicate[0]));

    }

}

2)然后创建一个控制器方法:


@GetMapping

public List<Model> getAllByFilter(ModelFilter filter) {

    return repo.findAll(filter); 

}

剩下要做的就是准备我们的谓词))


为此,我们可以先创建一个方便的“谓词生成器”界面:


@FunctionalInterface

interface PredicateBuilder<T> {


    Optional<Predicate> get(String fieldName, String value, Root<T> root, CriteriaBuilder builder);


    static Matcher getMatcher(String op, String value) {

        return getMatcher(op, value, "(.+)");

    }


    static Matcher getMatcher(String op, String value, String pattern) {

        return Pattern.compile(op + "\\(" + pattern + "\\)").matcher(value);

    }

}

然后尝试制作我们的谓词:


平等的


PredicateBuilder<Model> eq = (fieldName, value, root, cb) -> {

    Matcher m = getMatcher("eq", value);

    if (m.matches()) {

        return Optional.of(cb.equal(cb.upper(root.get(fieldName)), m.group(1).toUpperCase()));

    } else {

        return Optional.empty();

    }

};

喜欢


PredicateBuilder<Model> like = (fn, value, root, cb) -> {

    Matcher m = getMatcher("like", value);

    if (m.matches()) {

        return Optional.of(cb.like(cb.upper(root.get(fn)), "%" + m.group(1).toUpperCase() + "%"));

    } else {

        return Optional.empty();

    }

};

从...开始


PredicateBuilder<Model> sw = (fn, value, root, cb) -> {

    Matcher m = getMatcher("sw", value);

    if (m.matches()) {

        return Optional.of(cb.like(cb.upper(root.get(fn)), m.group(1).toUpperCase() + "%"));

    } else {

        return Optional.empty();

    }

};

之间


PredicateBuilder<Model> between = (fn, value, root, cb) -> {

    Matcher m = getMatcher("between", value, "(.+)\\s*,\\s*(.+)");

    if (m.matches()) {

        LocalDate from = LocalDate.parse(m.group(1));

        LocalDate to = LocalDate.parse(m.group(2));

        return Optional.of(cb.between(root.get(fn), from, to));

    } else {

        return Optional.empty();

    }

};


PredicateBuilder<Model> from = (fn, value, root, cb) -> {

    Matcher m = getMatcher("from", value);

    if (m.matches()) {

        LocalDate from = LocalDate.parse(m.group(1));

        return Optional.of(cb.greaterThanOrEqualTo(root.get(fn), from));

    } else {

        return Optional.empty();

    }

};


PredicateBuilder<Model> to = (fn, value, root, cb) -> {

    Matcher m = getMatcher("to", value);

    if (m.matches()) {

        LocalDate to = LocalDate.parse(m.group(1));

        return Optional.of(cb.lessThanOrEqualTo(root.get(fn), to));

    } else {

        return Optional.empty();

    }

};

剩下的只是完成Filter课程:


@Value

public class ModelFilter implements Specification<Model> {


    private String name;

    private String createdAt;


    PredicateBuilder<Model> eq = ... ;

    PredicateBuilder<Model> like = ... ;

    PredicateBuilder<Model> sw = ... ;

    PredicateBuilder<Model> between = ... ;

    PredicateBuilder<Model> from = ... ;

    PredicateBuilder<Model> to = ... ;


    @Override

    public Predicate toPredicate(Root<Model> root, CriteriaQuery<?> query, CriteriaBuilder builder) {


        List<Predicate> predicates = new ArrayList<>();


        if (name != null) {

            eq.get("name", name, root, builder).ifPresent(predicates::add);

            like.get("name", name, root, builder).ifPresent(predicates::add);

            sw.get("name", name, root, builder).ifPresent(predicates::add);

        }


        if (createdAt != null) {

            between.get("createdAt", createdAt, root, builder).ifPresent(predicates::add);

            from.get("createdAt", createdAt, root, builder).ifPresent(predicates::add);

            to.get("createdAt", createdAt, root, builder).ifPresent(predicates::add);

        }


        return builder.and(predicates.toArray(new Predicate[0]));

    }

}

当然,这只是实现的一个例子。您可以创建自己需要的规范和谓词的实现。这里的主要内容是:

  • 想出你的搜索界面

  • 制定您的“过滤器”规范

  • 准备好你需要的所有谓词

  • 在控制器方法中使用过滤器规范


查看完整回答
反对 回复 2022-11-02
  • 1 回答
  • 0 关注
  • 69 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信