[英]Jpa repository findBy method signature with multiple or operators?
I have Entity class for Product like this: 我有这样的产品的实体类:
@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;
I want to make the controller look like this: 我想使控制器看起来像这样:
List<Product> productList = productRepository.findByProdNameOrProdBrandContains(search);
model.addAttribute("products" , productList);
How do I wrote findBy methods in my Repository if i want to search the keyword of prodName OR prodBrand? 如果要搜索prodName或prodBrand的关键字,如何在存储库中编写findBy方法? I'm imagining something like this: 我在想像这样的事情:
List<Product> findByProdNameOrProdBrand(String prodName || String prodBrand);
Do I have to create different function/ model or something, and if so, how can i use both fucntion/ model in my thymeleaf? 我是否必须创建不同的功能/模型或其他东西,如果是的话,如何在百里香叶中同时使用两种功能/模型? Any help is appreciated. 任何帮助表示赞赏。 Thank you in advance. 先感谢您。
Just create the following method in repository: 只需在存储库中创建以下方法:
List<Product> findByProdNameOrProdBrand(String prodName, String prodBrand);
But I suggest you to switch to Sepcification<T>
when query with more than 3 conditions. 但是我建议您在查询3个以上条件时切换到Sepcification<T>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.