[英]Specific query in java spring called from rest controller
我正在嘗試通過其字段customerId過濾名為Measure的表。 這是該路徑的控制器開始處的樣子:
@RequestMapping(method = GET, path = "/nodes/{id}/ports/{portid}/measures")
@ResponseBody
public ResponseEntity<?> getPortMeasures(@PathVariable long id, @PathVariable long portid,
@RequestParam Optional<Long> from,
@RequestParam Optional<String> order,
@RequestParam Optional<String> countername,
@RequestParam Optional<Long> to) {
其次是調用查詢底層的方法
if (order.isPresent() && order.get().equals("asc")) {
return ResponseRestBuilder.createSuccessResponse(
measureRepository.
searchAsc
(networkElementList.get(0).ip, portList.get(0).rack, portList.get(0).frame, portList.get(0).slot,
portList.get(0).portSerial, countername.get(), from.orElse(0L), to.orElse(99999999999999999L)));
}
else{
return ResponseRestBuilder.createSuccessResponse(
measureRepository.
searchDesc
(networkElementList.get(0).ip, portList.get(0).rack, portList.get(0).frame, portList.get(0).slot,
portList.get(0).portSerial, countername.get(), from.orElse(0L), to.orElse(99999999999999999L)));
}
這是查詢的樣子:
@Query("SELECT mes FROM Measure mes WHERE " +
"mes.nodeIp = (:nodeIp) AND " +
"mes.rack = (:rack) AND " +
"mes.frame = (:frame) AND " +
"mes.slot = (:slot) AND " +
"mes.portSerial = (:portSerial) AND " +
"lower(mes.counterName) LIKE concat('%', lower(:countername), '%') AND"+
"mes.timestamp > (:timestamp1) AND " +
"mes.timestamp < (:timestamp2) "+
"ORDER BY mes.timestamp DESC")
List<Measure> searchDesc(@Param("nodeIp") String nodeIp, @Param("rack") String rack, @Param("frame") String frame,
@Param("slot") String slot, @Param("portSerial") String portSerial, @Param("countername") String countername,
@Param("timestamp1") Long timestamp1, @Param("timestamp2") Long timestamp2);
@Query("SELECT mes FROM Measure mes WHERE " +
"mes.nodeIp = :nodeIp AND " +
"mes.rack = :rack AND " +
"mes.frame = :frame AND " +
"mes.slot = :slot AND " +
"mes.portSerial = :portSerial AND " +
"lower(mes.counterName) LIKE concat('%', lower(:countername), '%') AND " +
"mes.timestamp > :timestamp1 AND " +
"mes.timestamp < :timestamp2 "+
"ORDER BY mes.timestamp ASC")
List<Measure> searchAsc(@Param("nodeIp") String nodeIp, @Param("rack") String rack, @Param("frame") String frame,
@Param("slot") String slot, @Param("portSerial") String portSerial, @Param("countername") String countername,
@Param("timestamp1") Long timestamp1, @Param("timestamp2") Long timestamp2);
它不會過濾任何內容,因為控制器會回復0行。 我100%確信有實際的行,因為我已經檢查了其他的休息電話。 我究竟做錯了什么?
問題很可能與timestamp字段有關,從代碼中似乎您傳遞了很長時間,但實際上它正在等待timestamp文字,jpa中的timestamp文字格式為{ts '2009-11-05 12-45-52.325'}
...只是檢查一下,嘗試從查詢中刪除時間戳,然后再次放入並手動提供文字...如果{ts '2009-11-05 12-45-52.325'}
,則需要找到一種方法來解析傳遞的long到相應的文字
問題在於字段portList.get(0).rack為“ null”。 顯然,這使整個查詢無法正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.