我正在撰寫一個 Springboot JPA REST API,它與 DB2 資料庫對話并且應該查詢包含一個TIMESTAMP欄位的表。
使用 SQL,在兩個 TIMESTAMP 之間過濾行的 DB2 查詢如下所示,它將從我的測驗資料中回傳 1 條記錄:
SELECT * FROM CARS WHERE SOLD_DATE BETWEEN '2020-01-01' AND '2022-01-01'
由于我使用的是 Spring Data JPA,因此我定義了CarEntity哪個java.sql.Timestamp欄位
@Entity
public class CarEntity {
....
Timestamp soldDate;
...
//getters and setters
}
我正在嘗試像上面的 SQL 查詢一樣檢索資料。
為此,我將 Postman 中的開始和結束資料作為Long表示開始和結束日期的值通過 URL 傳遞,例如
http://localhost:8080/cars/sold/1420070400/1640995200
此端點命中我的控制器方法,該方法將轉換Long為java.sql.Date并將其傳遞到存盤庫和存盤庫中,我使用@Query如下注釋:
@Repository
public interface CarRepository extends JpaRepository<CarEntity, Timestamp>{
@Query("select c from CarEntity c where c.carModel = 'Toyota' and c.soldDate between :startDate and :endDate")
List<CarEntity> getCarsSoldBetween(Date startDate, Date endDate);
}
但是,這不起作用并且它不回傳任何資料,盡管我知道它應該回傳 1 條記錄。
但是如果我像下面這樣對開始和結束日期進行硬編碼,我會得到 1 條記錄:
@Query("select c from CarEntity c where c.carModel = 'Toyota' and c.soldDate between '2020-01-01' and '2022-01-01'")
List<CarEntity> getCarsSoldBetween(Date startDate, Date endDate);
當然,問題在于我對 startDate 和 endDate 進行了硬編碼,而不是使用傳遞給getCarsSoldBetween()方法的那些。
更新-1
感謝@HYUNJUN,我添加了一些更改:
- 我
java.sql.Timestamp像以前一樣在我的物體中使用,但我的控制器、服務和存盤庫使用java.util.Date而不是java.sql.Date我最初使用的。 - 在我下面添加的 application.properties 中,可以查看 SQL 中傳遞的引數(請注意,這會導致顯著的減速,因此僅用于除錯目的):
logging.level.org.hibernate.sql=DEBUGlogging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
現在,當我轉到 DB2 Bench 并發出以下查詢時,我將回傳 2 行,這是正確的:
SELECT * FROM MYSCHEMA.CARS WHERE SOLD_TIMESTAMP BETWEEN '2021-10-04 15:00:00' AND '2021-10-20 00:00:00';
// RETURNS 2 ROWS
但是,我的存盤庫查詢如下所示:
@Repository
public interface CarRepository extends JpaRepository<CarEntity, Timestamp>{
@Query("select c from CarEntity c where c.carModel = 'Toyota' and c.soldDate between :startDate and :endDate")
List<CarEntity> getCarsSoldBetween(Date startDate, Date endDate);
}
, 不回傳任何內容,我希望回傳 2 行,因為開始日期和結束日期與日志輸出相同:
type.descriptor.sql.BasicBinder binding parameter [1] as [TIMESTAMP] - [Mon Oct 04 15:00:00 PDT 2021]
type.descriptor.sql.BasicBinder binding parameter [2] as [TIMESTAMP] - [Wed Oct 20 00:00:00 PDT 2021]
所以,我正在傳遞相同的日期范圍并期望得到相同的結果,但這并沒有發生
uj5u.com熱心網友回復:
我寫的代碼和你上面的代碼幾乎一樣。但我沒有遇到你提到的問題。我在下面上傳我的代碼。你為什么不比較我的代碼和你的代碼?
package com.springboot.springbootinternals.db2;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
public class Db2Controller {
private final CarRepository carRepository;
@GetMapping("db2/{start-date}/{end-date}")
public String db2(
@PathVariable("start-date") Long startDate,
@PathVariable("end-date") Long endDate
) {
carRepository.save(Car.builder().date(new Timestamp(1420070401L * 1000)).build());
Date start = new Date(TimeUnit.SECONDS.toMillis(startDate));
Date end = new Date(TimeUnit.SECONDS.toMillis(endDate));
List<Car> cars = carRepository.findAll(start, end);
System.out.println(">>> " cars);
return "success";
}
}
@Entity
@Builder
@NoArgsConstructor
@ToString
@AllArgsConstructor
class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Timestamp date;
}
@Repository
interface CarRepository extends JpaRepository<Car, Long> {
@Query("select c from Car c where c.date between :startDate and :endDate")
List<Car> findAll(Date startDate, Date endDate);
}
此外,將此選項放在 applicion.yml 中以檢查傳遞給 SQL 的正確值。
logging:
level:
org:
hibernate:
SQL: DEBUG
type:
descriptor:
sql:
BasicBinder: TRACE # show_parameter_value
像這樣。

uj5u.com熱心網友回復:
您可以使用以下代碼來檢索所需的結果
@Repository
public interface CarEntityRepository extends JpaRepository<CarEntity,Long> {
@Query("SELECT c FROM CarEntity c WHERE c.carModel like %:model% and c.soldDate > :start and c.soldDate < :end ")
List<CarEntity> getCarsSoldBetween(@Param("model") String model, @Param("start") Date start, @Param("end") Date end);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/344078.html
