我有这样的产品实体类:
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer ID_product;
@NotNull
private String prodName;
@NotNull
private String prodDesc;
@NotNull
private Integer price;
@NotNull
private String prodBrand;
@NotNull
private String prodImage;
@ManyToOne
private Category category;
我想让 Controller 看起来像这样:
List<Product> productList = productRepository.findByProdNameOrProdBrandContains(search);
model.addAttribute("products" , productList);
如果我想搜索 prodName 或 prodBrand 的关键字,如何在存储库中编写 findBy 方法?我正在想象这样的事情:
List<Product> findByProdNameOrProdBrand(String prodName || String prodBrand);
我是否必须创建不同的函数/模型或其他东西,如果是这样,我如何在我的 thymeleaf 中同时使用函数/模型?任何帮助表示赞赏。预先感谢您。
最佳答案
只需在存储库中创建以下方法:
List<Product> findByProdNameOrProdBrand(String prodName, String prodBrand);
但我建议您切换到Sepcification<T>
当查询条件超过3个时。
关于java - Jpa 存储库 findBy 方法签名具有多个 or 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540304/