【发布时间】:2021-09-02 16:55:05
【问题描述】:
我正在尝试使用 @GetMapping 进行搜索查询 使用 JPA 和 SpringBoot
这里是控制器使用的方法:
@GetMapping("/search{min_price}{max_price}{text}")
public Optional<Product> searchProducts(@PathVariable(required = false) Integer min_price,
@PathVariable(required = false) Integer max_price,
@PathVariable(required = false) String text
) {
Optional<Product> products = productRepository.SearchProduct(min_price, max_price, text);
return products;
}
这里是我在存储库中创建的@Query:(我将@Param 名称更改为 ?X 并且问题仍然存在)
@Query("SELECT P FROM Product P " +
"where (P.name like CONCAT('%',lower(?3),'%') or P.description like CONCAT('%',lower(?3),'%')) " +
"and (?2 is null or P.price <= ?2) " +
"and (?1 is null or P.price >= ?1)")
Optional<Product> SearchProduct(@Param("max_price") Integer min_price,
@Param("min_price") Integer max_price,
@Param("text") String text);
我是第一次使用 H2 数据库,不知道 ?X 是否是使用其中变量的正确方法。 (在 PL-SQL 中,我使用 :X 来实现此目的)。
当我得到一个 get 时,Postman 中的返回总是“null”
这是我所做的获取示例“search?min_price=1&max_price=200&text=prod” 该数据库有 5 个条目,如果我直接在 DB 2 中使用此值进行搜索,结果符合条件。
【问题讨论】: