它是否自动构造查询
Does it auto construct queries
我的端到端流程如下:
控制器 -> 适配器 -> persistence/repository -> DB
在我的控制器中:
@RequestMapping(value = "/userOrder/{orderID}", method = RequestMethod.DELETE)
public Set<?> deleteOrder(@PathVariable("orderID") String orderID) {
/*
* orderHandlerAdapter cancels the order and passed list of orders created + cancelled so * far.
*/
Set<Orders> orderList = orderHandlerAdapter.cancelOrder(orderID);
return orderList;
}
在适配器中:
public Set<Order> cancelOrder(String orderID) {
/*
* Cancel order first.
*/
userOrderRepository.saveOrder(orderID,"CANCELLED");
/*
* Return list of orders created and cancelled so far.
*/
Set<Order> orderList = userOrderRepository.getFirstByOrderIdAndStatusCdIsIn(orderID, new Set<String> {"CREATED","CANCELLED"});
return Set<Order>;
}
坚持:
public interface orderRespository extends CrudRepository<UserOrder,String>() {
/*
* Save status = CANCELLED into the DB.
*/
void saveOrder(String orderID);
/*
* Get orders that are created and cancelled.
*/
Set<Order> getFirstByOrderIdAndStatusCdIsIn(String orderID, new Set<String> orderStatusSet);
}
我发现调用saveOrder/getFirstByOrderIdAndStatusCdIsInis时,查询没有在任何地方定义,接口也没有实现。但是结果返回正确。
这是如何工作的? JPA 是否足够智能,可以根据方法名称创建查询?
Spring 数据具有产品中存储库中方法的默认实现(CrudRepository
、PagingAndSortingRepository
、JpaRepository
、QueryByExampleExecutor
) .您可以在 SimpleJpaRepository
.
中找到很多
但这不仅仅是 SimpleJpaRepository
的一个实例被添加到 ApplicationContext
。相反,使用代理。代理检查被调用的方法并决定如何提供实现:
- 是否为预定义方法 -> 调用其实现
- 它是否有
@Query
注释或它的名称是否与命名查询相匹配 -> 调用那个
- 能否将其名称转换为查询 -> 这样做。
除此之外 Spring 数据负责查询结果与方法所需 return 类型之间的转换。
我的端到端流程如下: 控制器 -> 适配器 -> persistence/repository -> DB
在我的控制器中:
@RequestMapping(value = "/userOrder/{orderID}", method = RequestMethod.DELETE)
public Set<?> deleteOrder(@PathVariable("orderID") String orderID) {
/*
* orderHandlerAdapter cancels the order and passed list of orders created + cancelled so * far.
*/
Set<Orders> orderList = orderHandlerAdapter.cancelOrder(orderID);
return orderList;
}
在适配器中:
public Set<Order> cancelOrder(String orderID) {
/*
* Cancel order first.
*/
userOrderRepository.saveOrder(orderID,"CANCELLED");
/*
* Return list of orders created and cancelled so far.
*/
Set<Order> orderList = userOrderRepository.getFirstByOrderIdAndStatusCdIsIn(orderID, new Set<String> {"CREATED","CANCELLED"});
return Set<Order>;
}
坚持:
public interface orderRespository extends CrudRepository<UserOrder,String>() {
/*
* Save status = CANCELLED into the DB.
*/
void saveOrder(String orderID);
/*
* Get orders that are created and cancelled.
*/
Set<Order> getFirstByOrderIdAndStatusCdIsIn(String orderID, new Set<String> orderStatusSet);
}
我发现调用saveOrder/getFirstByOrderIdAndStatusCdIsInis时,查询没有在任何地方定义,接口也没有实现。但是结果返回正确。
这是如何工作的? JPA 是否足够智能,可以根据方法名称创建查询?
Spring 数据具有产品中存储库中方法的默认实现(CrudRepository
、PagingAndSortingRepository
、JpaRepository
、QueryByExampleExecutor
) .您可以在 SimpleJpaRepository
.
但这不仅仅是 SimpleJpaRepository
的一个实例被添加到 ApplicationContext
。相反,使用代理。代理检查被调用的方法并决定如何提供实现:
- 是否为预定义方法 -> 调用其实现
- 它是否有
@Query
注释或它的名称是否与命名查询相匹配 -> 调用那个 - 能否将其名称转换为查询 -> 这样做。
除此之外 Spring 数据负责查询结果与方法所需 return 类型之间的转换。