Java 自定义查询Spring数据JPA中的参数处理
使用Spring数据JPA在Spring Boot中创建自定义本机SQL查询。想要搜索带有Java 自定义查询Spring数据JPA中的参数处理,java,sql,spring-boot,spring-data-jpa,Java,Sql,Spring Boot,Spring Data Jpa,使用Spring数据JPA在Spring Boot中创建自定义本机SQL查询。想要搜索带有order\u num的office\u products表,并返回相关的数据行(在现实世界中,多个订单具有相同的订单号是没有意义的,但在本例中,让我们说“是”,以便返回列表) 使用此控制器: @Controller public class OrderController { @Autowired private OrderRepository orderRepository; @GetMapping
order\u num
的office\u products
表,并返回相关的数据行(在现实世界中,多个订单具有相同的订单号是没有意义的,但在本例中,让我们说“是”,以便返回列表)
使用此控制器:
@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";
}
}
但是,当试图通过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();
}
在控制台中:
[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”)字符串orderNum
传递给方法List 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
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 ()
你知道我在这里遗漏了什么吗
更新:
在深入挖掘之后,我意识到我需要将以下代码添加到我的控制器中,并使用Shabbir建议的JPQL示例,代码现在可以工作了:
@GetMapping("/test")
public String getOrderListByNum(Model model, OrderEntity orderEntity) {
List<OrderEntity> foundByOrderNo = orderRepository.getOrderByOrderNum(orderEntity.getOrderNo());
model.addAttribute("foundByOrderNo", foundByOrderNo);
return "test";
}
@GetMapping(“/test”)
公共字符串getOrderListByNum(模型模型,OrderEntity OrderEntity){
List foundByOrderNo=orderRepository.getOrderByOrderNum(orderEntity.getOrderNo());
addAttribute(“foundByOrderNo”,foundByOrderNo);
返回“测试”;
}
更新:
当然还有派生查询解决方案:
@Repository
public interface OrderRepository extends JpaRepository<OrderEntity, OrderID> {
List<OrderEntity> findByOrderNo(String orderNo);
}
@存储库
公共接口OrderRepository扩展了JpaRepository{
列出findByOrderNo(字符串orderNo);
}
试试这个
@Query(value ="SELECT * FROM order_entity WHERE order_num = :orderNum", nativeQuery = true)
List<OrderEntity> getOrderByOrderNum(@Param(value="orderNum") String orderNum);
@Query(value=“SELECT*FROM order\u entity,其中order\u num=:orderNum”,nativeQuery=true)
列出getOrderByOrderNum(@Param(value=“orderNum”)字符串orderNum);
最好使用JPQL而不是本机查询,如下所示:
@Query("SELECT op FROM OrderEntity op WHERE orderNum = :orderNum")
List<OrderEntity> getOrderByOrderNum(@Param(value="orderNum") String orderNum);
@Query(“从OrderEntity op中选择op,其中orderNum=:orderNum”)
列出getOrderByOrderNum(@Param(value=“orderNum”)字符串orderNum);
您的方法没有任何参数。如果调用方不能将任何参数传递给该方法,它怎么可能要求42或54号令呢?还有,为什么要对这么简单的东西使用SQL查询?使用JPQL。或者甚至是从方法名自动派生的查询。@JB Nizet您能给我一个SQL本机查询或JPQL的代码示例吗?谢谢。。。在本机查询中,我试图查询的表的名称是office\u products
,但是您有office\u实体
?另外,您能否解释一下OrderEntity op
在JPQL中代表什么?尝试SQL示例,我在路径[]的上下文中为Servlet[dispatcherServlet]获取Servlet.service(),抛出异常[Handler dispatch failed;嵌套异常为java.lang。错误:未解决的编译问题:方法getOrderByOrderNum(String)在类型中,OrderRepository不适用于参数()
如果您想查询office_products表,那么为什么要将其结果存储到列表中?并且紧跟实体或表名称之后的任何名称都称为别名。尝试JPQL示例我得到:[ERROR]~2019-10-14-13.46.09.241CDT~~~o.a.c.c.c.[dispatcherServlet]路径为[]的上下文中Servlet[dispatcherServlet]的Servlet.service()引发异常[Handler dispatch失败;嵌套异常为java.lang。错误:未解决的编译问题:CheckNumRepository类型中的getCheckByCheckNum(String)方法不适用于参数()]with root-cause java.lang.Error:未解决的编译问题:类型OrderRepository中的方法getOrderByOrderNum(String)不适用于参数()
@wallwalker您试图运行的代码甚至没有编译。您的代码尝试调用该方法而不传递任何参数,尽管该方法需要一个参数。您应该真正练习编程的基础知识(定义方法、调用方法、编译代码、读取错误消息、修复错误)在使用像Spring和JPA这样复杂的野兽之前。
@Query("SELECT op FROM OrderEntity op WHERE orderNum = :orderNum")
List<OrderEntity> getOrderByOrderNum(@Param(value="orderNum") String orderNum);