[英]How to handle date ranges with QueryDSL Predicate in Spring?
I have a db with various events, and a corresponding entity:我有一个包含各种事件的数据库和一个相应的实体:
@NoArgsConstructor
@Data
@Table(name="Event")
public class Event {
@Id
private String id;
private String source;
private String action;
private Date timestamp;
private String data;
}
And a controller that takes a QueryDSLPredicate and parameters:还有一个采用 QueryDSLPredicate 和参数的 controller:
@GetMapping("/events")
public List<Event> getEvents(@QuerydslPredicate(root = Event.class) Predicate predicate,
@RequestParam(name = "id", required = false) String id,
@RequestParam(name = "source", required = false) String source,
@RequestParam(name = "action", required = false) String action,
@RequestParam(name = "startTimestamp", required = false) String startTimestamp,
@RequestParam(name = "endTimestamp", required = false) String endTimestamp,
@RequestHeader(required = false) String requestId) {
return StreamSupport.stream(eventRepository.findAll(predicate).spliterator(), false).collect(Collectors.toList());
This works great for sending specified parameters like this http://localhost:8080/events?source=testSource&action=testAction
这非常适合发送像这样的指定参数http://localhost:8080/events?source=testSource&action=testAction
But how can I handle the date ranges by sending in a starttime and endtime, and pulling all events between those times?但是如何通过发送开始时间和结束时间并在这些时间之间提取所有事件来处理日期范围? I'm new to querydsl/spring (in fact I haven't written java since college) and can't seem to find a great example on how to do this.我是 querydsl/spring 的新手(事实上我从大学开始就没有写过 java)并且似乎找不到一个很好的例子来说明如何做到这一点。
Got this working with help from this answer https://stackoverflow.com/a/35158320/2930025在这个答案https://stackoverflow.com/a/35158320/2930025的帮助下得到了这个工作
Changed my controller like so:像这样改变了我的 controller:
@GetMapping("/events")
public List<Event> getEvents(@QuerydslPredicate(root = Event.class) Predicate predicate,
@RequestHeader(required = false) String requestId) {
return StreamSupport.stream(eventRepository.findAll(predicate).spliterator(), false).collect(Collectors.toList());
And then added an overloaded customize method to the repository class:然后将重载的自定义方法添加到存储库 class:
@Repository
public interface EventRepository extends JpaRepository<Event,String>, QuerydslPredicateExecutor<Event>, QuerydslBinderCustomizer<QAuditEvent> {
@Override
default void customize(QuerydslBindings bindings, QEvent root) {
bindings.bind(root.timestamp).all((path, value) ->
{
Iterator<? extends Date> it = value.iterator();
return Optional.ofNullable(path.between(it.next(), it.next()));
});
}
}
And then to call simply send in two timestamps, one for the startTime and one for the endTime:然后调用只需发送两个时间戳,一个用于 startTime,一个用于 endTime:
http://localhost:8080/events?timestamp=2022-07-05T22:16:18Z×tamp=2022-08-27T22:16:18Z
Looks like the second answer on the linked question would also work, however I couldn't get the imports working correctly so went with the accepted answer's method.看起来链接问题的第二个答案也可以,但是我无法让导入正常工作,所以使用接受的答案的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.