Я хочу получить все продукты в моей таблице продуктов по категориям.Но я не хочу выписывать строку запроса для всех моих категорий.Вместо этого я хочу создать переменную, которую можно передать через мое отображение.Единственный способ сделать это - через JPLQ. Я использую MySQL и хочу знать, как превратить синтаксис JPLQ в собственные запросы.
хранилище продукта
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import tts.backend.dashboardapi.model.Product;
import java.awt.print.Pageable;
import java.util.List;
@Repository
public interface ProductRepository extends JpaRepository<Product,Integer> {
@Query("select u from products u where u.category = :category")
List<Product> findByCategory(@Param("category") Integer category);
@Query("select u from products u where u.category = :category AND u.availability = :availability")
List<Product> findByCategoryAndAvailability(@Param("category") Integer category,
@Param("availability")Boolean availability);
контроллер продукта
@GetMapping("/product/{category}")
public List<Product> findByCategory(@RequestParam (value= "category")Integer category){
return productrepository.findByCategory(category);
}
модель продукта
package tts.backend.dashboardapi.model;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "products")
@EntityListeners(AuditingEntityListener.class)
public class Product implements Serializable{
public Product() {
}
public Product(Integer id, String productName,Category category, double fullPrice, double salePrice, boolean availability, Supplier supplier){
this.id = id;
this.productName = productName;
this.category = category;
this.fullPrice = fullPrice;
this.availablity = availability;
this.supplier = supplier;
}
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "product_name")
private String productName;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name= "category")
private Category category;
@Column(name = "full_price")
private double fullPrice;
@Column(name = "sale_price")
private double salePrice;
@Column(name = "availability")
private boolean availablity;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "supplier")
private Supplier supplier;
У меня было только сопоставление для одного из моих запросов, потому чтоЯ получил кучу исключений при запуске моей программы.