Нужна помощь со Spring. Не знаю, как добавить findById

Не могу найти информацию, как вывести запись из таблицы mysql по определенному id. Я новичок в Spring JPA, помогите, пожалуйста

Вот модели продуктов и корзины:


@Entity
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name, item_size, img;
    private int price, quantity;

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getItem_size() {
        return item_size;
    }

    public void setItem_size(String item_size) {
        this.item_size = item_size;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Product() {
    }

    public Product(String name, int price, String item_size, int quantity, String img) {
        this.name = name;
        this.price = price;
        this.item_size = item_size;
        this.quantity = quantity;
        this.img = img;
    }
}




@Entity
public class Cart {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name, item_size, img;
    private int price, quantity;


    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getItem_size() {
        return item_size;
    }

    public void setItem_size(String item_size) {
        this.item_size = item_size;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Cart() {
    }

    public Cart(String name, int price, String item_size, int quantity, String img) {
        this.name = name;
        this.price = price;
        this.item_size = item_size;
        this.quantity = quantity;
        this.img = img;
    }

}

public interface CartRepository extends CrudRepository<Cart, Long> {

}

public interface ProductRepository extends CrudRepository<Product, Long> {

}

Контроллер:

@Controller
public class MainController {

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private CartRepository cartRepository;

@GetMapping("/")
    public String home(Model model) {
        model.addAttribute("title", "Главная");
        return "home";
    }

    @GetMapping("/hoody")
    public String hoody(Model model) {
        Iterable<Product> products = productRepository.findAll();
        model.addAttribute("products", products);
        return "hoody";
    }

    @PostMapping("/hoody")
    public String addfromProduct(@RequestParam String img, @RequestParam String name, @RequestParam Integer price, @RequestParam String item_size, @RequestParam Integer quantity, Model model) {
        Cart cart = new Cart(name, price, item_size, quantity, img);
        cartRepository.save(cart);
        return "redirect:/basket";
    }

    @GetMapping("/basket")
    public String basket(Model model) {
        Iterable<Cart> carts = cartRepository.findAll();
        model.addAttribute("carts", carts);
        return "basket";
    }

<title>Hoody<title/>

</head>

<body>

<form action="/hoody" method="post">
    <div th:each="el : ${products}">
      <input type="text" name="name" th:value="${el.name}" class="form" readonly/>
      <input type="text" name="price"  class="total-price" th:value="${el.price}"/>
      <input type="text" name="item_size" class="item" th:value="${el.item_size}"/>
      <input class="quantity_range" name="quantity" type="number" min="1" max="5" th:value="${el.quantity}">
      <input type="text" name="img" th:value="${el.img}"/>
      <button type="submit" class="btn btn-success">Добавление товара в корзину</button>
    </div>
  </form>

</body>

<title>basket<title/>

</head>

<body translate="no">

<div th:each="el : ${carts}">
            <input type="text" name="name" th:value="${el.name}" class="form" readonly/>
            <input type="text" name="price"  class="total-price" th:value="${el.price}"/>
            <input type="text" name="item_size" class="item" th:value="${el.item_size}"/>
            <input class="quantity_range" name="quantity" type="number" min="1" max="5" th:value="${el.quantity}">
        </div>
        </form>
<div th:each="el : ${carts}">
<img th:src="${el.img}" alt=""/>
</div>

</body>

При productRepository.findAll() выводятся все записи из таблицы Product. Нужно, чтобы при нажатии на Добавить в корзину, запись из Product по id выводилась в формочку корзины, а там данные добавлялись в табличку Cart


Ответы (1 шт):

Автор решения: Nowhere Man

В соответствующий репозиторий следует добавить метод findById:

public interface ProductRepository extends CrudRepository<Product, Long> {
    Optional<Product> findById(Long id);
}
→ Ссылка