Spring Data REST 的 QueryDSL 集成可以用来执行更复杂的查询吗? [英] Can Spring Data REST's QueryDSL integration be used to perform more complex queries?

查看:15
本文介绍了Spring Data REST 的 QueryDSL 集成可以用来执行更复杂的查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个 REST API,我希望客户端可以在其中轻松过滤特定实体的大多数属性.结合使用 QueryDSLSpring Data REST(Oliver Gierke 的示例)通过允许客户端通过组合引用属性的查询参数(例如 /users?firstName=Dennis&lastName=Laumen)进行过滤,我可以轻松地达到我想要的 90%.

I'm currently building a REST API in which I want clients to easily filter on most properties of a specific entity. Using QueryDSL in combination with Spring Data REST (an example by Oliver Gierke) allows me to easily get to 90% of what I want by allowing clients to filter by combining query parameters which refer to properties (e.g. /users?firstName=Dennis&lastName=Laumen).

我什至可以通过实现 QuerydslBinderCustomizer 接口自定义查询参数和实体属性之间的映射(例如,用于不区分大小写的搜索或部分字符串匹配).这一切都很好,但是我也希望客户端能够使用范围过滤某些类型.例如,对于像出生日期这样的属性,我想执行以下操作,/users?dateOfBirthFrom=1981-1-1&dateOfBirthTo=1981-12-31.基于数字的属性 /users?idFrom=100&idTo=200 也是如此.我觉得这应该可以使用 QuerydslBinderCustomizer 接口来实现,但是这两个库之间的集成并没有被广泛记录.

I can even customize the mapping between the query parameters and an entity's properties by implementing the QuerydslBinderCustomizer interface (e.g. for case insensitive searches or partial string matches). This is all great, however I also want the clients to be able to filter some types using ranges. For example with regards to a property like date of birth I'd like to do something like the following, /users?dateOfBirthFrom=1981-1-1&dateOfBirthTo=1981-12-31. The same goes for number based properties, /users?idFrom=100&idTo=200. I have the feeling this should be possible using the QuerydslBinderCustomizer interface but the integration between these two libraries isn't documented very extensively.

最后,这可能使用 Spring Data REST 和 QueryDSL 吗?如果有,怎么做?

推荐答案

我认为您应该能够使用以下自定义使其工作:

I think you should be able to get this to work using the following customization:

bindings.bind(user.dateOfBirth).all((path, value) -> {

  Iterator<? extends LocalDate> it = value.iterator();
  return path.between(it.next(), it.next());
});

这里的关键是使用 ?dateOfBirth=…&dateOfBirth= (使用该属性两次)和 ….all(…) 绑定,这将为您提供访问所有提供的值.

The key here is to use ?dateOfBirth=…&dateOfBirth= (use the property twice) and the ….all(…) binding which will give you access to all values provided.

确保将 @DateTimeFormat 注释添加到 UserdateOfBirth-property 中,以便 Spring 能够转换传入的 Strings 正确地插入到 LocalDate 实例中.

Make sure you add the @DateTimeFormat annotation to the dateOfBirth-property of User so that Spring is able to convert the incoming Strings into LocalDate instances correctly.

lambda 当前得到一个 Collection 这使得解开单个元素变得更加痛苦,但我认为我们可以在未来的版本中改变这一点,而是公开一个 List.

The lambda currently gets a Collection<? extends T> which makes untangling the individual elements a bit more pain that it needs to be, but I think we can change this in a future release to rather expose a List.

这篇关于Spring Data REST 的 QueryDSL 集成可以用来执行更复杂的查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆