java - 使用 JPA 查询获取上个月内的所有列?
问题描述
我当前的查询统计了每个活动售出的所有门票,并列出了门票数量最多的每个活动。现在的问题是,我还需要在上个月内获得所有票,而我根本无法从 JParepo Doc 中获得所需的结果。只要。当前查询如下所示:
@Query("select t.event.id, count(t.id) from Ticket t where t.event.seat = True group by t.event.id order by count(id) desc")
我在想在“真”之后和分组之前需要实施的地方,但我只是不知道如何。如果你们中的某个人能设法帮助我,我将非常感激!
编辑:忘记提及当前事件日期简称为“日期”,因此访问它需要做的只是 t.event.date。
解决方案
将您的查询更改为以下内容:
@Query("select t.event.id, count(t.id) from Ticket t where t.event.seat = ?1 and t.event.date >= ?2 group by t.event.id order by count(t.id) desc")
List<?> getEventCount(Boolean seatTaken, Date eventDate);
我修改了查询以使座位成为布尔参数(如果您想查找未占用的座位,则可重用)并添加到Date
参数中。
然后计算一个月前的日期:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
Date date = cal.getTime();
最后实现新方法:
List<?> results = repo.getEventCount(true, date);
要在 spring 中创建一个 Service 类并将它们放在一起,请尝试以下操作:
@Service
public class TicketService {
@Autowired
private TicketRepository repo;
public List<?> getTicketEventCount() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
Date date = cal.getTime();
List<?> results = repo.getEventCount(true, date);
return results;
}
}
推荐阅读
- javascript - 计算键盘上特定键的按下次数并在javascript中按下其他键时减少计数
- java - 如何为自定义 ParameterResolver 实现创建测试?
- r - R 省略变量标签不正确
- javascript - 具有此行为的箭头函数
- c# - 在运行时向 App.config 添加新配置
- python - 无论如何在功能块之外使用'return'从'else'返回到我的变量?
- typeclass - 重写子证明中的“未能合成类型类实例”
- python - 如何更改 jinja loop.with bootstrap 生成的按钮样式
- java - 如何测试返回 Mono 的函数
其中有另一个 Mono 在使用 Reactor 的 StepVerifier - r - 数据框中的字长操作