Spring DATA JPA 之间和 IsBetween 关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35319562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring DATA JPA Between and IsBetween keywords
提问by Mayank Porwal
How to get Spring DATA JPA method (like findBy
....) for the query below:
如何findBy
为以下查询获取 Spring DATA JPA 方法(如....):
Select * from USER where '2016-02-15' between VALID_FROM and VALID_TO;
回答by jpganz18
You should do it in this way
你应该这样做
SELECT * FROM users u WHERE e.REFERENCE_FIELD BETWEEN startDate AND endDate
Your code is not very clear, and I dont know what is 2016-02-15, if is the name of your field or is a value, if is a value and you are using between, you must specify a range, and a field to be compared.
你的代码不是很清楚,我不知道什么是2016-02-15,如果是你的字段名或者是一个值,如果是一个值并且你在两者之间使用,你必须指定一个范围,一个字段进行比较。
EDIT
编辑
If you want to use spring data jpa then do it like this
如果你想使用 spring 数据 jpa 然后这样做
findReferenceFieldBetween(value1,value2);
check the ref. here
检查参考。这里
回答by Saurabh Valsangkar
If you want to include both boundary dates in result you can use below in Spring data jpa
如果您想在结果中包含两个边界日期,您可以在 Spring 数据 jpa 中使用下面
@Query(value = "{'field' : {$gte : ?0, $lte : ?1}}", fields="{ 'field' : 1}")
List<DTO> findByFieldBetweenQuery(String first, String second);
Remove fields section if all fields are expected. Hope it helps someone.
如果需要所有字段,请删除字段部分。希望它可以帮助某人。