Spring Data JPA - 在日期之前和之后构建查询
Spring Data JPA - building a query before and after date
我是 Java JPA 的新手 - Spring Boot。我想创建一个 JPA 查找来计算两个日期之间的行数。
我需要一个数字来显示过去 28 天内加入的成员数量。所以像 10.
但也会得到一个值,表示与上个月的差异 - 所以 +4% - 或 -2% - 所以我想这将是 count1/count2 * 100 = 差异。您将如何检测极性 - 那么评估它是负极还是正极?
目前我有这样的东西
long countByRegisteredDateAfter(Date thresholdDate) throws Exception;
但可能需要更像这样的东西
long countByRegisteredBeforeDateAfter(Date thresholdDate1, Date thresholdDate2)
throws Exception;
也许还有像这样更精细的调整
long countByRegisteredBeforeAndDateAfterAndRole(Date thresholdDate1, Date thresholdDate2, String role)
throws Exception;
到目前为止的代码:
// 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);
我有点困惑,因为主题指出您想尝试查找之前和之后的日期,而稍后您想要将它们介于两者之间。不过要在尝试之间获取日期:
long countByRegisteredDateBetween(Date thresholdDate1, Date thresholdDate2)
或者在第二个例子中
long countByRegisteredDateBetweenAndRole(Date thresholdDate1, Date thresholdDate2, String role)
并尝试前后对比:
long countByRegisteredDateBeforeAndRegisteredDateAfter(Date thresholdDate1, Date thresholdDate2)
与角色案例类似
我是 Java JPA 的新手 - Spring Boot。我想创建一个 JPA 查找来计算两个日期之间的行数。
我需要一个数字来显示过去 28 天内加入的成员数量。所以像 10.
但也会得到一个值,表示与上个月的差异 - 所以 +4% - 或 -2% - 所以我想这将是 count1/count2 * 100 = 差异。您将如何检测极性 - 那么评估它是负极还是正极?
目前我有这样的东西
long countByRegisteredDateAfter(Date thresholdDate) throws Exception;
但可能需要更像这样的东西
long countByRegisteredBeforeDateAfter(Date thresholdDate1, Date thresholdDate2)
throws Exception;
也许还有像这样更精细的调整
long countByRegisteredBeforeAndDateAfterAndRole(Date thresholdDate1, Date thresholdDate2, String role)
throws Exception;
到目前为止的代码:
// 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);
我有点困惑,因为主题指出您想尝试查找之前和之后的日期,而稍后您想要将它们介于两者之间。不过要在尝试之间获取日期:
long countByRegisteredDateBetween(Date thresholdDate1, Date thresholdDate2)
或者在第二个例子中
long countByRegisteredDateBetweenAndRole(Date thresholdDate1, Date thresholdDate2, String role)
并尝试前后对比:
long countByRegisteredDateBeforeAndRegisteredDateAfter(Date thresholdDate1, Date thresholdDate2)
与角色案例类似