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
58e55f1f
Commit
58e55f1f
authored
Apr 26, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Wyszukiwarka w pełnej wersji i duzo "magicznych metod" Spring Data
parent
930cdedf
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
4 deletions
+51
-4
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+25
-4
ProductRepository.java
...ing/src/main/java/sklep/repository/ProductRepository.java
+26
-0
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
58e55f1f
...
@@ -5,6 +5,7 @@ import java.util.List;
...
@@ -5,6 +5,7 @@ import java.util.List;
import
java.util.Optional
;
import
java.util.Optional
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Sort
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.BindingResult
;
...
@@ -15,7 +16,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
...
@@ -15,7 +16,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.SessionAttribute
;
import
org.springframework.web.bind.annotation.SessionAttribute
;
import
jakarta.servlet.http.HttpSession
;
import
jakarta.validation.Valid
;
import
jakarta.validation.Valid
;
import
sklep.basket.Basket
;
import
sklep.basket.Basket
;
import
sklep.model.Product
;
import
sklep.model.Product
;
...
@@ -34,7 +34,7 @@ public class ProductController {
...
@@ -34,7 +34,7 @@ public class ProductController {
@GetMapping
@GetMapping
public
String
wszystkieProdukty
(
Model
model
)
{
public
String
wszystkieProdukty
(
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findAll
();
List
<
Product
>
products
=
productRepository
.
findAll
(
Sort
.
by
(
"productId"
)
);
model
.
addAttribute
(
"products"
,
products
);
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
return
"products"
;
}
}
...
@@ -118,8 +118,29 @@ public class ProductController {
...
@@ -118,8 +118,29 @@ public class ProductController {
@GetMapping
(
"/szukaj"
)
@GetMapping
(
"/szukaj"
)
public
String
szukajProduktow
(
Model
model
,
public
String
szukajProduktow
(
Model
model
,
String
name
)
{
String
name
,
List
<
Product
>
products
=
productRepository
.
findByProductName
(
name
);
BigDecimal
min
,
BigDecimal
max
)
{
List
<
Product
>
products
=
List
.
of
();
if
(
name
!=
null
&&
!
name
.
isEmpty
()
&&
min
==
null
&&
max
==
null
)
{
products
=
productRepository
.
findByProductNameContainingIgnoringCase
(
name
);
}
else
if
((
name
==
null
||
name
.
isEmpty
())
&&
(
min
!=
null
||
max
!=
null
))
{
if
(
min
==
null
)
{
min
=
BigDecimal
.
ZERO
;
}
if
(
max
==
null
)
{
max
=
BigDecimal
.
valueOf
(
1000_000_000
);
}
products
=
productRepository
.
findByPriceBetween
(
min
,
max
);
}
else
if
(
name
!=
null
&&
!
name
.
isEmpty
()
&&
(
min
!=
null
||
max
!=
null
))
{
if
(
min
==
null
)
{
min
=
BigDecimal
.
ZERO
;
}
if
(
max
==
null
)
{
max
=
BigDecimal
.
valueOf
(
1000_000_000
);
}
products
=
productRepository
.
findByProductNameContainingIgnoringCaseAndPriceBetween
(
name
,
min
,
max
);
}
model
.
addAttribute
(
"products"
,
products
);
model
.
addAttribute
(
"products"
,
products
);
return
"wyszukiwarka"
;
return
"wyszukiwarka"
;
}
}
...
...
PC30-SklepSpring/src/main/java/sklep/repository/ProductRepository.java
View file @
58e55f1f
package
sklep
.
repository
;
package
sklep
.
repository
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
java.util.List
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.data.jpa.repository.JpaRepository
;
...
@@ -7,9 +8,34 @@ import org.springframework.stereotype.Repository;
...
@@ -7,9 +8,34 @@ import org.springframework.stereotype.Repository;
import
sklep.model.Product
;
import
sklep.model.Product
;
/* Gdy w projekcie umieścimy interfejs rozszerzający interfejs JpaRepository (albo podobny)
* i oznaczymy go adnotacją @Repository (albo skonfigurujemy w inny sposób...),
* to Spring AUTOMATYCZNIE UTWORZY IMPLEMENTACJĘ tego interfejsu.
* Dzięki temu "za darmo" mamy metody dający podstawowy dostęp do tabel.
* Dodatkowo w interfejsie można dopisać własne metody, w których nazwie kryje się zasada działania.
* Np. findByPriceBetween szuka produktów o cenie między min i max.
*
* findByEmail - szuka rekordów z polem email równym parametrowi.
*
* przejście do innych tabel / encji:
* List<Employee> findByDepartment_Location_City(String city)
* jakby w Javie sprawdzić: employee.getDepartment().getLocation.getCity().equals(city)
*
* https://www.baeldung.com/spring-data-derived-queries
*/
@Repository
@Repository
public
interface
ProductRepository
extends
JpaRepository
<
Product
,
Integer
>
{
public
interface
ProductRepository
extends
JpaRepository
<
Product
,
Integer
>
{
List
<
Product
>
findByProductName
(
String
name
);
List
<
Product
>
findByProductName
(
String
name
);
List
<
Product
>
findByProductNameContaining
(
String
name
);
List
<
Product
>
findByProductNameContainingIgnoringCase
(
String
name
);
List
<
Product
>
findByPriceBetween
(
BigDecimal
min
,
BigDecimal
max
);
List
<
Product
>
findByProductNameContainingIgnoringCaseAndPriceBetween
(
String
name
,
BigDecimal
min
,
BigDecimal
max
);
}
}
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