【发布时间】:2019-10-16 23:40:04
【问题描述】:
我需要统计当月的注册用户数。 我使用原始方法
我使用 H2 数据库。
-
在模型(阅读器)中,我有一个字段:
private String LocalDate因为我将数据库中的日期保存为String:@Override public String getCurrentDate() { LocalDate localDate = LocalDate.now(); DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String formatDate = localDate.format(format); return formatDate; } -
在 ReaderRepository 我有这个:
Integer getCountDatesCurrentMonth(); List<String> allDates();
在 List 中我从表中获取所有日期。
-
在
ReaderServiceImpl我有这个方法:@Override public Integer getCurrentMonthRegisteredReaders() { List<String> dates = readerRepository.allDates(); Integer date = LocalDate.now().getMonthValue(); String s; int count=0; for(int i =0;i<dates.size();i++){ s = dates.get(i).charAt(5)+""+dates.get(i).charAt(6); if(s.equals(date.toString())) count++; } return count;
我在哪里获得当月注册的用户数
代码运行完美,并在当月注册的读者的网页上显示我, 但我想用 JPA 或 JPQL 在一两行中做到这一点,但我做不到。
我尝试过使用 JPA,但不起作用:
Integer countByDateMonthValue(int currentMonth);
我得到错误:
Caused by: java.lang.IllegalStateException: Illegal attempt to dereference path source [null.date] of basic type
或使用 JPQL:
@Query("select count(date) from Reader where... ")
但我不知道该怎么办......
【问题讨论】:
标签: jpa spring-data-jpa jpql