通过示例 Spring 数据查询 [英] Query by Example Spring Data

查看:15
本文介绍了通过示例 Spring 数据查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带日期字段的域对象人:

I have domain object Person with date fields:

public class Person {
@Id
private Long id;
private Date date

像这样构建示例:

Person person = new Person();
person.setSomeOtherFields("some fields");
Example<Person> example = Example.of(person);

如何创建具有日期范围的示例查询(搜索实体包含大于或等于某个日期且小于或等于另一个日期的日期)?

How i can create example query with date range (search entity contains date greater or equal from some date and less or equal some another date)?

推荐答案

Spring Data JPA 实例查询技术使用 Examples 和 ExampleMatchers 转换实体实例到基础查询中.当前官方文档 阐明只有完全匹配可用于非字符串属性.由于您的要求涉及 java.util.Date 字段,因此您只能使用示例查询技术进行精确匹配.

The Spring Data JPA query-by-example technique uses Examples and ExampleMatchers to convert entity instances into the underlying query. The current official documentation clarifies that only exact matching is available for non-string attributes. Since your requirement involves a java.util.Date field, you can only have exact matching with the query-by-example technique.

您可以编写自己的 ExampleMatcher,根据您的需要返回查询子句.

You could write your own ExampleMatcher that returns query clauses according to your needs.

这篇关于通过示例 Spring 数据查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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