Commit 27822aae by Patryk Czarnik

Automatyczne tworzenie JpaRepository z interfejsu

parent 1fe4907e
package sklep.alternatywne_wersje_bazy_danych;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import sklep.model.Product;
@Controller
@RequestMapping("/alt7")
public class ProductController_v7 {
@Autowired
private ProductRepository_v7 productRepository;
@GetMapping
public String wszystkieProdukty(Model model) {
List<Product> products = productRepository.findAll();
model.addAttribute("products", products);
return "products";
}
@GetMapping("/{id}")
public String jedenProdukt(@PathVariable int id, Model model) {
Optional<Product> product = productRepository.findById(id);
if(product.isPresent()) {
model.addAttribute("product", product.get());
return "product";
} else {
model.addAttribute("product_id", id);
return "missing_product";
}
}
}
package sklep.alternatywne_wersje_bazy_danych;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import sklep.model.Product;
@Repository
public interface ProductRepository_v7 extends JpaRepository<Product, Integer> {
}
...@@ -38,11 +38,5 @@ public class ProductController { ...@@ -38,11 +38,5 @@ public class ProductController {
return "missing_product"; return "missing_product";
} }
} }
@GetMapping("/szukaj")
public String wyszukajProdukty(@RequestParam String name, Model model) {
List<Product> products = productRepository.findByProductName(name);
model.addAttribute("products", products);
return "products";
}
} }
package sklep.repository; package sklep.repository;
import java.util.List; import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import sklep.model.Product; import sklep.model.Product;
@Repository @Repository
public class ProductRepository { public interface ProductRepository extends JpaRepository<Product, Integer> {
@Autowired
private EntityManager em;
public List<Product> findAll() {
TypedQuery<Product> query = em.createNamedQuery("Product.findAll", Product.class);
return query.getResultList();
}
public Optional<Product> findById(int productId) {
return Optional.ofNullable(em.find(Product.class, productId));
}
public List<Product> findByProductName(String name) {
final String sql = "SELECT p FROM Product p WHERE p.productName = :name";
TypedQuery<Product> query = em.createQuery(sql, Product.class);
query.setParameter("name", name);
return query.getResultList();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment