Commit 87327dbc by Patryk Czarnik

Cytanie produktów za pomocą JPA/Hibernate

(wersja tekstowa w kontrolerze)
parent cb1b901b
package sklep.alternatywne_wersje_bazy_danych;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import sklep.model.Product;
@Controller
@RequestMapping(path="/alt3", produces="text/plain")
public class ProductController_v3 {
@Autowired
private EntityManager em;
@GetMapping
@ResponseBody
public String wszystkieProdukty() {
StringBuilder sb = new StringBuilder();
TypedQuery<Product> query = em.createNamedQuery("Product.findAll", Product.class);
List<Product> products = query.getResultList();
for(Product product : products) {
sb.append("* produkt ")
.append(product.getProductName())
.append(" za cenę ")
.append(product.getPrice())
.append('\n');
}
return sb.toString();
}
@GetMapping("/{id}")
@ResponseBody
public String jedenProdukt(@PathVariable int id) {
StringBuilder sb = new StringBuilder();
Product product = em.find(Product.class, id);
if(product != null) {
sb.append("Znaleziony produkt:\n")
.append(product.getProductName())
.append(" za cenę ")
.append(product.getPrice())
.append('\n');
} else {
sb.append("Nie ma produktu o numerze ").append(id);
}
return sb.toString();
}
@GetMapping("/szukaj")
@ResponseBody
public String wyszukajProdukty(@RequestParam String name) {
StringBuilder sb = new StringBuilder();
final String sql = "SELECT p FROM Product p WHERE p.productName = :name";
TypedQuery<Product> query = em.createQuery(sql, Product.class);
query.setParameter("name", name);
List<Product> products = query.getResultList();
for(Product product : products) {
sb.append("* produkt ")
.append(product.getProductName())
.append(" za cenę ")
.append(product.getPrice())
.append('\n');
}
return sb.toString();
}
}
package sklep.controller;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
......@@ -15,32 +10,28 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import sklep.model.Product;
@Controller
@RequestMapping(path="/products", produces="text/plain")
public class ProductController {
@Autowired
private DataSource dataSource;
private EntityManager em;
@GetMapping
@ResponseBody
public String wszystkieProdukty() {
StringBuilder sb = new StringBuilder();
final String sql = "SELECT * FROM products ORDER BY product_id";
try(Connection c = dataSource.getConnection();
PreparedStatement stmt = c.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
sb.append("* produkt ")
.append(rs.getString("product_name"))
.append(" za cenę ")
.append(rs.getBigDecimal("price"))
.append('\n');
}
} catch (SQLException e) {
e.printStackTrace();
sb.append("Błąd: " + e);
TypedQuery<Product> query = em.createNamedQuery("Product.findAll", Product.class);
List<Product> products = query.getResultList();
for(Product product : products) {
sb.append("* produkt ")
.append(product.getProductName())
.append(" za cenę ")
.append(product.getPrice())
.append('\n');
}
return sb.toString();
}
......@@ -49,25 +40,15 @@ public class ProductController {
@ResponseBody
public String jedenProdukt(@PathVariable int id) {
StringBuilder sb = new StringBuilder();
final String sql = "SELECT * FROM products WHERE product_id = ?";
try(Connection c = dataSource.getConnection();
PreparedStatement stmt = c.prepareStatement(sql)) {
stmt.setInt(1, id);
try(ResultSet rs = stmt.executeQuery()) {
if(rs.next()) {
sb.append("Znaleziony produkt:\n")
.append(rs.getString("product_name"))
.append(" za cenę ")
.append(rs.getBigDecimal("price"))
.append('\n');
} else {
sb.append("Nie ma produktu o numerze ").append(id);
}
}
} catch (SQLException e) {
e.printStackTrace();
sb.append("Błąd: " + e);
Product product = em.find(Product.class, id);
if(product != null) {
sb.append("Znaleziony produkt:\n")
.append(product.getProductName())
.append(" za cenę ")
.append(product.getPrice())
.append('\n');
} else {
sb.append("Nie ma produktu o numerze ").append(id);
}
return sb.toString();
}
......@@ -77,24 +58,16 @@ public class ProductController {
public String wyszukajProdukty(@RequestParam String name) {
StringBuilder sb = new StringBuilder();
final String sql = "SELECT * FROM products WHERE product_name = ? ORDER BY product_id";
try(Connection c = dataSource.getConnection();
PreparedStatement stmt = c.prepareStatement(sql)) {
stmt.setString(1, name);
try(ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
sb.append("* produkt ")
.append(rs.getString("product_name"))
.append(" za cenę ")
.append(rs.getBigDecimal("price"))
.append('\n');
}
}
} catch (SQLException e) {
e.printStackTrace();
sb.append("Błąd: " + e);
final String sql = "SELECT p FROM Product p WHERE p.productName = :name";
TypedQuery<Product> query = em.createQuery(sql, Product.class);
query.setParameter("name", name);
List<Product> products = query.getResultList();
for(Product product : products) {
sb.append("* produkt ")
.append(product.getProductName())
.append(" za cenę ")
.append(product.getPrice())
.append('\n');
}
return sb.toString();
}
......
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