簡體   English   中英

自定義查詢中的參數處理 Spring 數據 JPA

[英]Parameter handling in custom query Spring Data JPA

在 Spring 引導中使用 Spring 數據 Z9CE3D1BD8890F16A0C448Z09359 創建自定義本機 SQL 查詢想用order_num搜索office_products表並返回相關的數據行(在現實世界中,有多個具有相同訂單號的訂單是沒有意義的,但在這個例子中,我們只說“是”,所以我們可以返回一個列表)。

使用這個 Controller:

@Controller
public class OrderController {

@Autowired
private OrderRepository orderRepository;

@GetMapping("/test")
public String getOrderListByNum(Model model) {
     List<OrderEntity> foundByOrderNo = orderRepository.getOrderByOrderNum();
     model.addAttribute("foundByOrderNo", foundByOrderNo);
     return "test";

}

}

當查詢被硬編碼為12354order_num值時,我可以成功查詢數據庫並返回結果,如下所示:

@Repository
public interface OrderRepository extends JpaRepository<OrderEntity, OrderID> {

     @Query(value ="SELECT * FROM office_products WHERE order_num=12354", nativeQuery = true)
     List<OrderEntity> getOrderByOrderNum();
} 

但是,當嘗試通過http://localhost:8080/test?orderNum=12354傳遞值來使用參數時,如下代碼不起作用:

@Repository
public interface OrderRepository extends JpaRepository<OrderEntity, OrderID> {

     @Query(value ="SELECT * FROM office_products WHERE order_num = :orderNum", nativeQuery = true)
     List<OrderEntity> getOrderByOrderNum();
} 

嘗試使用參數時,我得到以下錯誤:

@ http://localhost:8080/test?orderNum=12354

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Oct 14 12:07:44 CDT 2019
There was an unexpected error (type=Internal Server Error, status=500).
Named parameter not bound : orderNum; nested exception is org.hibernate.QueryException: Named parameter not bound : orderNum
org.springframework.dao.InvalidDataAccessResourceUsageException: Named parameter not bound : orderNum; nested exception is org.hibernate.QueryException: Named parameter not bound : orderNum

在控制台中:

[ERROR]~2019-10-14-12.07.44.569CDT~~~~~~ o.a.c.c.C.[.[.[.[dispatcherServlet] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: Named parameter not bound : orderNum; nested exception is org.hibernate.QueryException: Named parameter not bound : orderNum] with root cause
org.hibernate.QueryException: Named parameter not bound : orderNum

最后,將@Param("orderNum") String orderNum傳遞給方法List<OrderEntity> getOrderByOrderNum(); 我收到一個新錯誤:

@ http://localhost:8080/test?orderNum=12354

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Oct 14 12:22:11 CDT 2019
There was an unexpected error (type=Internal Server Error, status=500).
Unresolved compilation problem: The method getOrderByOrderNum(String) in the type OrderRepository  is not applicable for the arguments ()
java.lang.Error: Unresolved compilation problem: 
    The method getOrderByOrderNum(String) in the type OrderRepository is not applicable for the arguments ()

知道我在這里缺少什么嗎?


更新:

在深入挖掘之后,我意識到我需要將以下代碼添加到我的 controller 並使用 Shabbir 建議的 JPQL 示例,代碼現在可以工作:

@GetMapping("/test")
public String getOrderListByNum(Model model, OrderEntity orderEntity) {
     List<OrderEntity> foundByOrderNo = orderRepository.getOrderByOrderNum(orderEntity.getOrderNo());
     model.addAttribute("foundByOrderNo", foundByOrderNo);
     return "test";

}

更新:

當然還有派生查詢解決方案:

@Repository
public interface OrderRepository extends JpaRepository<OrderEntity, OrderID> {

     List<OrderEntity> findByOrderNo(String orderNo);
} 

嘗試這個

@Query(value ="SELECT * FROM order_entity WHERE order_num = :orderNum", nativeQuery = true)

List<OrderEntity> getOrderByOrderNum(@Param(value="orderNum") String orderNum);

最好使用 JPQL 而不是本機查詢,例如:

@Query("SELECT op FROM OrderEntity op WHERE orderNum = :orderNum")

List<OrderEntity> getOrderByOrderNum(@Param(value="orderNum") String orderNum);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM