java Spring Data JPA - 在日期之前和之后构建查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46575797/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:16:53  来源:igfitidea点击:

Spring Data JPA - building a query before and after date

javaspringspring-data-jpajpql

提问by The Old County

I am new to Java JPA - Spring Boot. I want to create a JPA lookup that will count the number of rows between two dates.

我是 Java JPA - Spring Boot 的新手。我想创建一个 JPA 查找来计算两个日期之间的行数。

I need to get a number that shows the number of members that have joined in the last 28 days. So like 10.

我需要得到一个数字,显示过去 28 天内加入的成员数量。所以像10。

But also get a value that indicates the difference from last month -- so +4% -- or -2% -- so I suppose it will be a count1/count2 * 100 = difference. How would you detect the polarity -- so assess if its negative or positive?

但也要得到一个值,表明与上个月的差异——所以 +4%——或 -2%——所以我想这将是一个 count1/count2 * 100 = 差异。你将如何检测极性——所以评估它是负极还是正极?

Currently I have something like this

目前我有这样的事情

long countByRegisteredDateAfter(Date thresholdDate) throws Exception;

but need something maybe more like this

但需要更像这样的东西

long countByRegisteredBeforeDateAfter(Date thresholdDate1, Date thresholdDate2)
    throws Exception;

and maybe something more fine tuned like this

也许像这样更精细的调整

long countByRegisteredBeforeAndDateAfterAndRole(Date thresholdDate1, Date thresholdDate2, String role)
    throws Exception;

Code so far:

到目前为止的代码:

            // 28 days ago
            Calendar thresholdPast28 = Calendar.getInstance();
            thresholdPast28.set(Calendar.HOUR_OF_DAY,0);
            thresholdPast28.set(Calendar.MINUTE,0);
            thresholdPast28.set(Calendar.SECOND,0);
            thresholdPast28.add(Calendar.DATE,-28);

            java.util.Date thresholdPast28Date = thresholdPast28.getTime();
            Long countLast28Days = (Long) tblLoginRepository.countByRegisteredDateAfter(thresholdPast28Date);

            System.out.println("countLast28Days " + countLast28Days);


            // 56 days ago
            Calendar thresholdPast56 = Calendar.getInstance();
            thresholdPast56.set(Calendar.HOUR_OF_DAY,0);
            thresholdPast56.set(Calendar.MINUTE,0);
            thresholdPast56.set(Calendar.SECOND,0);
            thresholdPast56.add(Calendar.DATE,-28);

            java.util.Date thresholdPast56Date = thresholdPast56.getTime();
            Long countLast56Days = (Long) tblLoginRepository.countByRegisteredDateAfter(thresholdPast56Date);

            System.out.println("countLast56Days " + countLast56Days);

回答by pezetem

I'm a bit confused as the topic states that you want to try to find dates before and after while in later you want to have them between. Nevertheless to get dates between try:

我有点困惑,因为该主题指出您想尝试查找之前和之后的日期,而稍后您希望在它们之间找到日期。不过要在尝试之间获取日期:

long countByRegisteredDateBetween(Date thresholdDate1, Date thresholdDate2)

or in the second example

或者在第二个例子中

long countByRegisteredDateBetweenAndRole(Date thresholdDate1, Date thresholdDate2, String role)

and to get before and after try something like:

并在尝试之前和之后尝试:

long countByRegisteredDateBeforeAndRegisteredDateAfter(Date thresholdDate1, Date thresholdDate2)

similar do with the Role case

与 Role 案例类似