java Spring Data JPA - 如何仅使用月和日从日期列中获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47719439/
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 - How can I fetch data from a Date column using only month and day?
提问by Matias Diez
I have a simple Spring Data JPA project working with mysql and I need to fetch all registers that match the day and month. The column I need to filter is type Datetime.
我有一个使用 mysql 的简单 Spring Data JPA 项目,我需要获取与日期和月份匹配的所有寄存器。我需要过滤的列是日期时间类型。
1935-12-08 00:00:00
If I want to do this in a db level it works fine:
如果我想在 db 级别执行此操作,它可以正常工作:
SELECT * FROM my_database.event where event_date LIKE '%-12-08%';
It treats the date as a string. Now I need to do this in my repository yet nothing seems to work. I tried the basic one:
它将日期视为字符串。现在我需要在我的存储库中执行此操作,但似乎没有任何效果。我尝试了基本的:
List<Event> findByEventDateLike(
@Param("eventDate") Date eventDate);
but it says it returns an illegal argument exception since it is a Date object. I tried other combinations but apparently spring data jpa can't compare dates with partial information.
但它说它返回一个非法参数异常,因为它是一个 Date 对象。我尝试了其他组合,但显然 spring 数据 jpa 无法将日期与部分信息进行比较。
NOTE:To keep it clean I'm trying to avoid @Query sentences but if it is the only way to go, it is a valid answer.
注意:为了保持干净,我试图避免使用 @Query 语句,但如果这是唯一的方法,那么它是一个有效的答案。
How can I do it?
我该怎么做?
采纳答案by Matias Diez
Use nativeQuery to emulate the query in the Database.
使用 nativeQuery 模拟数据库中的查询。
@Query(value = "SELECT * FROM events WHERE event_date Like %?1%", nativeQuery = true)
List<Match> findByMatchMonthAndMatchDay(@Param ("eventDate") String eventDate);
The DB takes care of the conversion between Date/String for the LIKE clause.
DB 负责 LIKE 子句的日期/字符串之间的转换。
Passing the param as "-month-day" (e.g.: -12-08) will return a collection of events with that string in the date field.
将参数作为“-month-day”(例如:-12-08)传递将在日期字段中返回具有该字符串的事件集合。
回答by Cepr0
If your JPA provider is Hibernate then you can use year
and month
(and other funcs) in your JPQL (HQL) query, for example like this:
如果您的 JPA 提供程序是 Hibernate,那么您可以在 JPQL (HQL) 查询中使用year
and month
(和其他 funcs),例如:
@Query("select e from Event e where year(e.eventDate) = ?1 and month(e.eventDate) = ?2")
List<Entity> getByYearAndMonth(int year, int month);
回答by dimirsen
The code below works for ZonedDateTime (startDate), don't have an opportunity to test against DateTime right now:
下面的代码适用于 ZonedDateTime (startDate),现在没有机会针对 DateTime 进行测试:
@Query("SELECT b FROM Box b " +
"WHERE EXTRACT (day FROM b.startDate) = :dayOfMonth AND EXTRACT (month FROM b.startDate) = :month")
List<Box> findBoxWithParticularDayAndMonth(@Param("dayOfMonth") Integer dayOfMonth, @Param("month") Integer month);
Usage:
用法:
List<Box> boxes = boxRepo.findBoxWithParticularDayAndMonth(22, 5);
回答by marti_
Java: Date to String Convert java.util.Date to String
Java:日期到字符串 将 java.util.Date 转换为字符串
@Query("select e from Event e where e.eventDate like %:eventDate%")
List<Entity> findByEventDateLike(@Param("eventDate")String eventDate);
for me the easy way is parse Date to String but I found another solution you could be try
对我来说,最简单的方法是将日期解析为字符串,但我找到了另一个您可以尝试的解决方案
%Like% Query in spring JpaRepositoryhttps://docs.spring.io/spring-data/jpa/docs/current/reference/html/
spring JpaRepository 中的 %Like% 查询https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
Containing
含
findByFirstnameContaining
findByFirstnameContaining
… where x.firstname like ?1 (parameter bound wrapped in %)
... where x.firstname like ?1(参数绑定在 % 中)