Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
5929b9fc
Commit
5929b9fc
authored
Apr 26, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Jeszcze jedna wersja z EntityManagerem
parent
3d3b3236
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
97 additions
and
0 deletions
+97
-0
ProductController_v6.java
...alternatywne_wersje_bazy_danych/ProductController_v6.java
+47
-0
ProductRepository_v6.java
...alternatywne_wersje_bazy_danych/ProductRepository_v6.java
+17
-0
ProductRepository_v6_Impl.java
...natywne_wersje_bazy_danych/ProductRepository_v6_Impl.java
+33
-0
No files found.
PC30-SklepSpring/src/main/java/sklep/alternatywne_wersje_bazy_danych/ProductController_v6.java
0 → 100644
View file @
5929b9fc
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
org.springframework.web.bind.annotation.RequestParam
;
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"
;
}
}
@GetMapping
(
"/szukaj"
)
public
String
wyszukajProdukty
(
@RequestParam
String
name
,
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findByProductName
(
name
);
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
}
}
PC30-SklepSpring/src/main/java/sklep/alternatywne_wersje_bazy_danych/ProductRepository_v6.java
0 → 100644
View file @
5929b9fc
package
sklep
.
alternatywne_wersje_bazy_danych
;
import
java.util.List
;
import
java.util.Optional
;
import
sklep.model.Product
;
public
interface
ProductRepository_v6
{
List
<
Product
>
findAll
();
Optional
<
Product
>
findById
(
int
productId
);
List
<
Product
>
findByProductName
(
String
name
);
}
\ No newline at end of file
PC30-SklepSpring/src/main/java/sklep/alternatywne_wersje_bazy_danych/ProductRepository_v6_Impl.java
0 → 100644
View file @
5929b9fc
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.Repository
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.TypedQuery
;
import
sklep.model.Product
;
@Repository
public
class
ProductRepository_v6_Impl
implements
ProductRepository_v6
{
@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
();
}
}
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