Java Jpa存储库通过具有多个or运算符的方法签名查找?
我有如下产品的实体类:Java Jpa存储库通过具有多个or运算符的方法签名查找?,java,spring,spring-data-jpa,thymeleaf,Java,Spring,Spring Data Jpa,Thymeleaf,我有如下产品的实体类: @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; @N
@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;
List<Product> productList = productRepository.findByProdNameOrProdBrandContains(search);
model.addAttribute("products" , productList);
List<Product> findByProdNameOrProdBrand(String prodName || String prodBrand);
我想让控制器看起来像这样:
@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;
List<Product> productList = productRepository.findByProdNameOrProdBrandContains(search);
model.addAttribute("products" , productList);
List<Product> findByProdNameOrProdBrand(String prodName || String prodBrand);
List productList=productRepository.findByProdNameOrProdBrandContains(搜索);
model.addAttribute(“产品”,productList);
如果要搜索prodName或prodBrand的关键字,如何在存储库中编写findBy方法?我在想象这样的事情:
@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;
List<Product> productList = productRepository.findByProdNameOrProdBrandContains(search);
model.addAttribute("products" , productList);
List<Product> findByProdNameOrProdBrand(String prodName || String prodBrand);
列出findByProdNameOrProdBrand(字符串prodName | |字符串prodBrand);
我是否必须创建不同的功能/模型或其他东西,如果是,如何在我的thymeleaf中同时使用这两个功能/模型?感谢您的帮助。提前感谢。只需在存储库中创建以下方法:
List<Product> findByProdNameOrProdBrand(String prodName, String prodBrand);
列出findByProdNameOrProdBrand(字符串prodName,字符串prodBrand);
但我建议您在使用3个以上条件进行查询时切换到
sespecification
。但这不需要用户提供2个输入吗?所以您的问题是:如何通过动态条件进行查找
,对吗?对于3个以上的条件,您应该阅读查询DSL
和规范
。根据您的输入,您可以在一个函数中使用多个条件