简体   繁体   English

Spring Data JPA - 如何仅使用月份和日期从Date列中获取数据?

[英]Spring Data JPA - How can I fetch data from a Date column using only month and day?

I have a simple Spring Data JPA project working with mysql and I need to fetch all registers that match the day and month. 我有一个简单的Spring Data JPA项目使用mysql,我需要获取所有匹配日期和月份的寄存器。 The column I need to filter is type Datetime. 我需要过滤的列是Datetime类型。

1935-12-08 00:00:00

If I want to do this in a db level it works fine: 如果我想在数据库级别执行此操作,它可以正常工作:

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. 但它说它返回一个非法的参数异常,因为它是一个Date对象。 I tried other combinations but apparently spring data jpa can't compare dates with partial information. 我尝试了其他组合,但显然弹簧数据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? 我该怎么做?

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)查询中使用yearmonth (以及其他函数 ),例如:

@Query("select e from Event e where year(e.eventDate) = ?1 and month(e.eventDate) = ?2")
List<Entity> getByYearAndMonth(int year, int month);

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);

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子句的Date / String之间的转换。

Passing the param as "-month-day" (eg: -12-08) will return a collection of events with that string in the date field. 将参数传递为“-month-day”(例如:-12-08)将在日期字段中返回带有该字符串的事件集合。

Java: Date to String Convert java.util.Date to String Java:Date to String 将java.util.Date转换为String

@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 JpaRepository https://docs.spring.io/spring-data/jpa/docs/current/reference/html/ %Like%JaseRepository中的%Query https://docs.spring.io/spring-data/jpa/docs/current/reference/html/

Containing

findByFirstnameContaining findByFirstnameContaining

… where x.firstname like ?1 (parameter bound wrapped in %) ...其中x.firstname喜欢?1(参数绑定包装在%)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 spring 数据 jpa 返回该日期的日期和记录数? - How can I return date and count of records for that date using spring data jpa? 如何从 SQLite 数据库中获取特定月份的数据 - How can I fetch the data of specific month from SQLite database 如何在Java中将日期格式化为仅月,日和小时? - How can I format a Date in Java as only month, day and hour? 如何使用Spring Data JPA从列中获取链接和值? - How can I get both links and values from a column with Spring Data JPA? 如何使用 Spring JPA DATA 仅获取特定的子实体及其具有特定值的父实体 - How to fetch only the specific child entity along with its Parent entities with specific value using Spring JPA DATA 如何使用 Spring Boot 的 JPA 存储库中的映射从 spring 引导中的多个表中获取数据 - How to fetch data from multiple tables in spring boot using mapping in Spring Boot's JPA repository 当输入查询只是该数据的一部分时,如何使用 Room 从我的数据库中获取数据? - How can I fetch data from my Database using Room when the input query is only part of that data? 如何使用带有“从方法名创建查询”策略的Spring数据JPA来实现这两个简单查询? - How can I implement these 2 simple queries using Spring data JPA with the “query creation from method names” strategy? 如何使用Spring Data MongoDB或Spring Data Jpa - How can I use Spring Data MongoDB or Spring Data Jpa 如果数据库(oracle)中没有记录与使用 spring jpa 的日期列结合使用,如何将零显示为计数? - How to display zero as count if there is no record in data base(oracle) in combination with Date column using spring jpa?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM