Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
javab_20230617
Commits
5e3856b0
Commit
5e3856b0
authored
Jul 29, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dodatkowe wersje dostępów do bazy danych
parent
3d440089
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
325 additions
and
5 deletions
+325
-5
ProductController_v1.java
...ep/alternatywne_dostepy_do_bazy/ProductController_v1.java
+53
-0
ProductController_v2.java
...ep/alternatywne_dostepy_do_bazy/ProductController_v2.java
+44
-5
ProductController_v4.java
...ep/alternatywne_dostepy_do_bazy/ProductController_v4.java
+47
-0
ProductController_v5.java
...ep/alternatywne_dostepy_do_bazy/ProductController_v5.java
+47
-0
ProductController_v6.java
...ep/alternatywne_dostepy_do_bazy/ProductController_v6.java
+40
-0
ProductRepository_v4.java
...ep/alternatywne_dostepy_do_bazy/ProductRepository_v4.java
+33
-0
ProductRepository_v5.java
...ep/alternatywne_dostepy_do_bazy/ProductRepository_v5.java
+17
-0
ProductRepository_v5_Impl.java
...ternatywne_dostepy_do_bazy/ProductRepository_v5_Impl.java
+33
-0
ProductRepository_v6.java
...ep/alternatywne_dostepy_do_bazy/ProductRepository_v6.java
+11
-0
No files found.
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductController_v1.java
View file @
5e3856b0
...
@@ -3,7 +3,9 @@ package sklep.alternatywne_dostepy_do_bazy;
...
@@ -3,7 +3,9 @@ package sklep.alternatywne_dostepy_do_bazy;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
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.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
javax.sql.DataSource
;
import
javax.sql.DataSource
;
...
@@ -37,4 +39,55 @@ public class ProductController_v1 {
...
@@ -37,4 +39,55 @@ public class ProductController_v1 {
}
}
return
sb
.
toString
();
return
sb
.
toString
();
}
}
@GetMapping
(
"/{id}"
)
@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
);
}
return
sb
.
toString
();
}
@GetMapping
(
"/szukaj"
)
@ResponseBody
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
);
}
return
sb
.
toString
();
}
}
}
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductController_v2.java
View file @
5e3856b0
package
sklep
.
alternatywne_dostepy_do_bazy
;
package
sklep
.
alternatywne_dostepy_do_bazy
;
import
jakarta.persistence.EntityManager
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
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.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
sklep.model.Product
;
import
java.util.List
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.TypedQuery
;
import
sklep.model.Product
;
@Controller
@Controller
@RequestMapping
(
"/alt2/products"
)
@RequestMapping
(
"/alt2/products"
)
...
@@ -22,7 +24,7 @@ public class ProductController_v2 {
...
@@ -22,7 +24,7 @@ public class ProductController_v2 {
public
String
readAll
()
{
public
String
readAll
()
{
List
<
Product
>
products
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
).
getResultList
();
List
<
Product
>
products
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
).
getResultList
();
StringBuilder
sb
=
new
StringBuilder
(
"Lista produktów:\n"
);
StringBuilder
sb
=
new
StringBuilder
(
"Lista produktów:\n"
);
for
(
Product
product
:
products
)
{
for
(
Product
product
:
products
)
{
sb
.
append
(
" * "
)
sb
.
append
(
" * "
)
.
append
(
product
.
getProductName
())
.
append
(
product
.
getProductName
())
.
append
(
" za cenę "
)
.
append
(
" za cenę "
)
...
@@ -31,4 +33,41 @@ public class ProductController_v2 {
...
@@ -31,4 +33,41 @@ public class ProductController_v2 {
}
}
return
sb
.
toString
();
return
sb
.
toString
();
}
}
@GetMapping
(
path
=
"/{id}"
,
produces
=
"text/plain;charset=UTF-8"
)
@ResponseBody
public
String
readOne
(
@PathVariable
(
"id"
)
Integer
productId
)
{
Product
product
=
em
.
find
(
Product
.
class
,
productId
);
StringBuilder
sb
=
new
StringBuilder
();
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
(
productId
);
}
return
sb
.
toString
();
}
@GetMapping
(
path
=
"/szukaj"
,
produces
=
"text/plain;charset=UTF-8"
)
@ResponseBody
public
String
szukaj
(
String
name
)
{
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
"SELECT p FROM Product p WHERE productName = :nazwa"
,
Product
.
class
);
query
.
setParameter
(
"nazwa"
,
name
);
Product
product
=
query
.
getSingleResult
();
StringBuilder
sb
=
new
StringBuilder
();
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 nazwie "
).
append
(
name
);
}
return
sb
.
toString
();
}
}
}
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductController_v4.java
0 → 100644
View file @
5e3856b0
package
sklep
.
alternatywne_dostepy_do_bazy
;
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
org.springframework.web.bind.annotation.RequestParam
;
import
sklep.model.Product
;
@Controller
@RequestMapping
(
"/alt4/products"
)
public
class
ProductController_v4
{
@Autowired
private
ProductRepository_v4
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"
;
}
}
@GetMapping
(
"/szukaj"
)
public
String
wyszukajProdukty
(
@RequestParam
String
name
,
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findByProductName
(
name
);
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
}
}
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductController_v5.java
0 → 100644
View file @
5e3856b0
package
sklep
.
alternatywne_dostepy_do_bazy
;
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
org.springframework.web.bind.annotation.RequestParam
;
import
sklep.model.Product
;
@Controller
@RequestMapping
(
"/alt5/products"
)
public
class
ProductController_v5
{
@Autowired
private
ProductRepository_v5
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"
;
}
}
@GetMapping
(
"/szukaj"
)
public
String
wyszukajProdukty
(
@RequestParam
String
name
,
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findByProductName
(
name
);
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
}
}
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductController_v6.java
0 → 100644
View file @
5e3856b0
package
sklep
.
alternatywne_dostepy_do_bazy
;
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
(
"/alt6/products"
)
public
class
ProductController_v6
{
@Autowired
private
ProductRepository_v6
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"
;
}
}
}
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductRepository_v4.java
0 → 100644
View file @
5e3856b0
package
sklep
.
alternatywne_dostepy_do_bazy
;
import
java.util.List
;
import
java.util.Optional
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Repository
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.TypedQuery
;
import
sklep.model.Product
;
@Repository
public
class
ProductRepository_v4
{
@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
();
}
}
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductRepository_v5.java
0 → 100644
View file @
5e3856b0
package
sklep
.
alternatywne_dostepy_do_bazy
;
import
java.util.List
;
import
java.util.Optional
;
import
sklep.model.Product
;
public
interface
ProductRepository_v5
{
List
<
Product
>
findAll
();
Optional
<
Product
>
findById
(
int
productId
);
List
<
Product
>
findByProductName
(
String
name
);
}
\ No newline at end of file
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductRepository_v5_Impl.java
0 → 100644
View file @
5e3856b0
package
sklep
.
alternatywne_dostepy_do_bazy
;
import
java.util.List
;
import
java.util.Optional
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Repository
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.TypedQuery
;
import
sklep.model.Product
;
@Repository
public
class
ProductRepository_v5_Impl
implements
ProductRepository_v5
{
@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
();
}
}
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductRepository_v6.java
0 → 100644
View file @
5e3856b0
package
sklep
.
alternatywne_dostepy_do_bazy
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
sklep.model.Product
;
// We wcześniejszych wersjach pisało się tu adnotację @Repository.
// @Repository
public
interface
ProductRepository_v6
extends
JpaRepository
<
Product
,
Integer
>
{
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment