使用Spring Data JPA本地查询在两个日期之间搜索记录?

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

Search records between two dates using Spring Data JPA native query?

问题

我正在尝试使用Spring Data Jpa创建一个端点,以使用户/客户能够使用我的表中的日期列查找记录的详细信息,然而被获取的记录只有截止日期之前的记录。

例如,我想要在18日至20日之间的记录,但被获取的记录只有18日和19日的记录,尽管该表中存在20日的记录。我需要一种方法来检索18日和19日的记录,以及与截止日期(在这种情况下为20日)相关的记录。非常感谢任何帮助。

下面是我在JPA存储库中尝试过的使用JPA本机查询的方法。

在第一种方法中,我使用了本机查询BETWEEN,但未获取到截止日期的记录。以下是代码片段:

@Query(value = "SELECT * FROM PAYMENT_MASTER WHERE LAST_UPDATED BETWEEN :startDate AND :endDate", nativeQuery = true)
List<PaymentMaster> getAllBetweenDates(@Param("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate startDate,
                                   @Param("endDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate endDate);

下面是我尝试的另一种方法,结果与上述描述以及前一种方法相同。在这里,我使用了本机查询GREATER THAN/EQUAL TO和LESS THAN/EQUAL TO,但仍未获得所需的结果。以下是存储库中的代码片段:

@Query(value = "SELECT * FROM PAYMENT_MASTER WHERE LAST_UPDATED >= :startDate AND LAST_UPDATED <= :endDate", nativeQuery = true)
List<PaymentMaster> getAllBetweenDates(@Param("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate startDate,
                                   @Param("endDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate endDate);

这是我控制器中的代码片段:

@PostMapping("/find/date-between")
public ResponseEntity<Object> findPaymentByDate(@RequestBody DateSearcherDto searcherDto) {
    List<PaymentMaster> paymentMasterList = paymentMasterRepository.getAllBetweenDates(searcherDto.getStartDate(), searcherDto.getEndDate());
    List<PaymentMasterDto> resultsDto = new ArrayList<>();
}

更新

请查看我下面创建的DateSearcher DTO。

package com.oasis.firsbacklogbackend.dto;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;

@Data
public class DateSearcherDto {

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd")
    private LocalDateTime startDate;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = "yyyy-MM-dd")
    private LocalDateTime endDate;

}

非常感谢您的帮助。谢谢。

英文:

I am trying to create an endpoint with Spring Data Jpa to enable users/client to find details of records using the date column from my table, however the records that are being fetched are only records from before the end date.

For example, I want records from between 18th - 20th, but the records that are fetched are records from 18th & 19th leaving out the records for 20th even though records for that date (20th) exists in the table. I need a way to retrieve the records of 18th and 19th along with that pertaining to the end date which is 20th in this case. Any help is appreciated.

Below are the approaches I have tried in my JPA repository using the JPA native query.

In the first approach, I used the native query BETWEEN but the records from the end date are not being fetched. This is the code snippet below:

@Query(value = &quot;SELECT * FROM PAYMENT_MASTER WHERE LAST_UPDATED BETWEEN :startDate AND :endDate&quot;, nativeQuery = true)
List&lt;PaymentMaster&gt; getAllBetweenDates(@Param(&quot;startDate&quot;) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate startDate,
                                       @Param(&quot;endDate&quot;) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate endDate);

Below is another approach I tried that gave me the same results as described above and as the previous approach. Here I used the native query GREATER THAN/EQUAL TO AND LESS THAN/EQUAL TO but still didn't get the desired outcome. Here's the code snippet from the repository:

@Query(value = &quot;SELECT * FROM PAYMENT_MASTER WHERE LAST_UPDATED &gt;= :startDate AND LAST_UPDATED &lt;= :endDate&quot;, nativeQuery = true)
List&lt;PaymentMaster&gt; getAllBetweenDates(@Param(&quot;startDate&quot;) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate startDate,
                                       @Param(&quot;endDate&quot;) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate endDate);

Here is a code snippet from my controller:

@PostMapping(&quot;/find/date-between&quot;)
public ResponseEntity&lt;Object&gt; findPaymentByDate(@RequestBody DateSearcherDto searcherDto) {
    List&lt;PaymentMaster&gt; paymentMasterList = paymentMasterRepository.getAllBetweenDates(searcherDto.getStartDate(), searcherDto.getEndDate());
    List&lt;PaymentMasterDto&gt; resultsDto = new ArrayList&lt;&gt;();
}

UPDATE

Please check out my DateSearcher DTO whicd I created below.


package com.oasis.firsbacklogbackend.dto;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;

@Data
public class DateSearcherDto {

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = &quot;yyyy-MM-dd&quot;)
    private LocalDateTime startDate;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME, pattern = &quot;yyyy-MM-dd&quot;)
    private LocalDateTime endDate;

}


Please your help would be appreciated. Thanks.

答案1

得分: 4

根据 @gtiwari333 的回答,我修改了我的代码,并成功地获得了获取包括结束日期在内的所有记录的期望结果。以下是为了未来读者而进行的修改。

我修改后的代码库:

@Query(value = "SELECT * FROM PAYMENT_MASTER WHERE LAST_UPDATED >= :startDate AND LAST_UPDATED <= :endDate", nativeQuery = true)
List<PaymentMaster> getAllBetweenDates(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);

我修改后的控制器:

@PostMapping("/find/date-between")
public ResponseEntity<Object> findPaymentByDate(@RequestBody DateSearcherDto searcherDto) {

    LocalDateTime start = LocalDateTime.of(LocalDate.from(searcherDto.getStartDate()), LocalTime.of(0, 0, 0));
    LocalDateTime end = LocalDateTime.of(LocalDate.from(searcherDto.getEndDate()), LocalTime.of(23, 59, 59));

    List<PaymentMaster> paymentMasterList =
            paymentMasterRepository.getAllBetweenDates(start, end);

我的 DateSearcher Dto 修改:

package com.oasis.firsbacklogbackend.dto;

import lombok.Data;

import java.time.LocalDate;

@Data
public class DateSearcherDto {

    private LocalDate startDate;

    private LocalDate endDate;

}

这就是我解决问题的方法。通过这些修改,它像魔法一样奏效…… 谢谢。

英文:

Based on the answers of @gtiwari333 I modified my code and was able to get the desired outcome of fetching all the records including that of the end date. Below are the modifications for the sake of future readers.

My modified repository

    @Query(value = &quot;SELECT * FROM PAYMENT_MASTER WHERE LAST_UPDATED &gt;= :startDate AND LAST_UPDATED &lt;= :endDate&quot;, nativeQuery = true)
    List&lt;PaymentMaster&gt; getAllBetweenDates(@Param(&quot;startDate&quot;) LocalDateTime startDate, @Param(&quot;endDate&quot;) LocalDateTime endDate);


My modified controller

@PostMapping(&quot;/find/date-between&quot;)
    public ResponseEntity&lt;Object&gt; findPaymentByDate(@RequestBody DateSearcherDto searcherDto) {

        LocalDateTime start = LocalDateTime.of(LocalDate.from(searcherDto.getStartDate()), LocalTime.of(0, 0, 0));
        LocalDateTime end = LocalDateTime.of(LocalDate.from(searcherDto.getEndDate()), LocalTime.of(23, 59, 59));

        List&lt;PaymentMaster&gt; paymentMasterList =
                paymentMasterRepository.getAllBetweenDates(start, end);


My DateSearcher Dto modifications:

package com.oasis.firsbacklogbackend.dto;

import lombok.Data;

import java.time.LocalDate;

@Data
public class DateSearcherDto {

    private LocalDate startDate;

    private LocalDate endDate;

}



This was how I solved the problem. With these modifications, it worked like magic... Thanks.

答案2

得分: 3

如果您正在进行日期/时间比较,您需要将下限设置为清晨早些时候,将上限设置为深夜晚些时候。此外,更新您的查询以使用 LocalDateTime

@Query(value = "SELECT * FROM PAYMENT_MASTER WHERE LAST_UPDATED >= :startDate AND LAST_UPDATED <= :endDate", nativeQuery = true)
List<PaymentMaster> getAllBetweenDates(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);

在您的服务中:

LocalDateTime start = LocalDateTime.of(searcherDto.getStartDate(), LocalTime.of(0, 0, 0));
LocalDateTime end = LocalDateTime.of(searcherDto.getEndDate(), LocalTime.of(23, 59, 59));

paymentMasterRepository.getAllBetweenDates(start, end);
英文:

If you are doing date/time comparison, you have to put the lower bound to early morning and upper bound to late night. Also, update your query to take LocalDateTime

@Query(value = &quot;SELECT * FROM PAYMENT_MASTER WHERE LAST_UPDATED &gt;= :startDate AND LAST_UPDATED &lt;= :endDate&quot;, nativeQuery = true)
List&lt;PaymentMaster&gt; getAllBetweenDates(@Param(&quot;startDate&quot;) LocalDateTime startDate, @Param(&quot;endDate&quot;) LocalDateTime endDate);

In your service:

LocalDateTime start = LocalDateTime.of(searcherDto.getStartDate(), LocalTime.of(0, 0, 0));
LocalDateTime end = LocalDateTime.of(searcherDto.getEndDate(), LocalTime.of(23, 59, 59));

paymentMasterRepository.getAllBetweenDates(start, end);

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

发表评论

匿名网友

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

确定