java - Jpa 存储库 findBy 方法签名具有多个 or 运算符?

标签 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;

  @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/

相关文章:

java 。忽略/禁用按键功能但仍捕获按键事件

java - Maven - Windows 安装程序插件

java - 当组件对齐为 "RIGHT_TO_LEFT"时,字符串连接中的错误行为

java - 无法找到 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' 类型的 Bean。 - Spring 安全

java - 找不到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration 或@SpringBootTest(classes=...)

java - 如何获取传递给 HTTP POST 的参数

java - RESTful服务设计: Calling get all resource endpoint

java - 我们能否保护域模型免受 JSON 请求的影响而不创建 DTO 对象?

java - Spring Data JPA 无法返回复杂表达式的正确结果

spring - Jpa规范执行器: complex queries with specifications