Use JPA查询方法来搜索特定日期(日和月)。

huangapple go评论98阅读模式
英文:

Use JPA query methods to seach records for specific day (day and month)

问题

我正在尝试为一个具有电影记录和发布日期的数据库实现一个RESTful API的搜索操作。数据库的格式类似于这样:

标题                 播放时长      语言       发布日期
				
Traffic shop	       104	       en	      12/15/1995
Nocturnal men	       101	       en	      12/22/1995
Exit front	       127	       en	      12/22/1996
The last paradise     106	       en	      02/10/1995

我需要获取所有在特定日期,比如说"12/22"发布的电影,所以它需要返回:

Nocturnal men	       101	       en	      12/22/1995
Exit front	       127	       en	      12/22/1996

即使它们发布在不同的年份也可以。我不确定是否可以使用JPA中的查询方法来实现这个,但我仍在学习中。

英文:

I'm trying to implement a search operation for a restful API on a databases with records of movies with release dates. The format of the database is something like this:

title              running time    language    release date      
				   
Traffic shop	   104	           en	       12/15/1995
Nocturnal men	   101	           en	       12/22/1995
Exit front	       127	           en	       12/22/1996
The last paradise  106	           en	       02/10/1995

, i need to get all movies released during lets say "12/22", so it needs to return:

Nocturnal men	   101	           en	       12/22/1995
Exit front	       127	           en	       12/22/1996

, even if they have been released in two different years. I'm not sure if this can be done using query methods in JPA but i'm still learning.

答案1

得分: 1

You can use day() and month() functions to extract data from the date for comparison.

@Query("SELECT m from Movie m WHERE day(m.releaseDate) = ?1 and month(m.releaseDate) = ?2")
List<Movie> getMovieByDayAndMonth(int day, int month);
英文:

You can use day() and month() function to extract data from date to compare

@Query(&quot;SELECT m from Movie m WHERE day(m.releaseDate) = ?1 and month(m.releaseDate) = ?2&quot;)
List&lt;Movie&gt; getMovieByDayAndMonth(int day, int month);

huangapple
  • 本文由 发表于 2020年8月13日 10:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63387165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定