Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
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_20230928
Commits
44dc5a66
Commit
44dc5a66
authored
Nov 07, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SpringTechnicznie - gotowe źródła
parent
9f2ccd0a
Hide whitespace changes
Inline
Side-by-side
Showing
44 changed files
with
1757 additions
and
1 deletions
+1757
-1
HelloController.java
...cznie/src/main/java/com/example/demo/HelloController.java
+43
-0
Controller0.java
...znie/src/main/java/com/example/demo/data/Controller0.java
+24
-0
Controller1_EM.java
...e/src/main/java/com/example/demo/data/Controller1_EM.java
+42
-0
Controller2.java
...znie/src/main/java/com/example/demo/data/Controller2.java
+43
-0
Controller3.java
...znie/src/main/java/com/example/demo/data/Controller3.java
+49
-0
Controller4.java
...znie/src/main/java/com/example/demo/data/Controller4.java
+43
-0
Controller5.java
...znie/src/main/java/com/example/demo/data/Controller5.java
+144
-0
Repository0_Memory.java
...c/main/java/com/example/demo/data/Repository0_Memory.java
+50
-0
Repository2.java
...znie/src/main/java/com/example/demo/data/Repository2.java
+35
-0
Repository3_Impl.java
...src/main/java/com/example/demo/data/Repository3_Impl.java
+38
-0
Repository3_Interface.java
...ain/java/com/example/demo/data/Repository3_Interface.java
+16
-0
Repository4.java
...znie/src/main/java/com/example/demo/data/Repository4.java
+9
-0
Repository5.java
...znie/src/main/java/com/example/demo/data/Repository5.java
+52
-0
AController1.java
.../src/main/java/com/example/demo/licznik/AController1.java
+35
-0
AController4.java
.../src/main/java/com/example/demo/licznik/AController4.java
+27
-0
AController5.java
.../src/main/java/com/example/demo/licznik/AController5.java
+24
-0
AController6.java
.../src/main/java/com/example/demo/licznik/AController6.java
+19
-0
AController7.java
.../src/main/java/com/example/demo/licznik/AController7.java
+19
-0
AController8.java
.../src/main/java/com/example/demo/licznik/AController8.java
+19
-0
Configuration8.java
...rc/main/java/com/example/demo/licznik/Configuration8.java
+22
-0
Licznik.java
...cznie/src/main/java/com/example/demo/licznik/Licznik.java
+33
-0
LicznikBean8.java
.../src/main/java/com/example/demo/licznik/LicznikBean8.java
+14
-0
LicznikComponent6.java
...main/java/com/example/demo/licznik/LicznikComponent6.java
+17
-0
LicznikComponent7.java
...main/java/com/example/demo/licznik/LicznikComponent7.java
+20
-0
Country.java
...nicznie/src/main/java/com/example/demo/model/Country.java
+57
-0
Department.java
...znie/src/main/java/com/example/demo/model/Department.java
+98
-0
Employee.java
...icznie/src/main/java/com/example/demo/model/Employee.java
+153
-0
Job.java
...Technicznie/src/main/java/com/example/demo/model/Job.java
+95
-0
Location.java
...icznie/src/main/java/com/example/demo/model/Location.java
+92
-0
Region.java
...hnicznie/src/main/java/com/example/demo/model/Region.java
+72
-0
FabrykaKomponentow.java
...va/com/example/demo/wstrzykiwanie/FabrykaKomponentow.java
+16
-0
Inject1.java
...src/main/java/com/example/demo/wstrzykiwanie/Inject1.java
+36
-0
Inject2.java
...src/main/java/com/example/demo/wstrzykiwanie/Inject2.java
+41
-0
Inject3.java
...src/main/java/com/example/demo/wstrzykiwanie/Inject3.java
+31
-0
Komponent.java
...c/main/java/com/example/demo/wstrzykiwanie/Komponent.java
+14
-0
Repo.java
...ie/src/main/java/com/example/demo/wstrzykiwanie/Repo.java
+13
-0
application.properties
...ringTechnicznie/src/main/resources/application.properties
+4
-1
styl.css
PC31-SpringTechnicznie/src/main/resources/static/styl.css
+36
-0
employees.html
...ngTechnicznie/src/main/resources/templates/employees.html
+35
-0
hello.html
...SpringTechnicznie/src/main/resources/templates/hello.html
+13
-0
index.html
...SpringTechnicznie/src/main/resources/templates/index.html
+69
-0
licznik.html
...ringTechnicznie/src/main/resources/templates/licznik.html
+14
-0
names.html
...SpringTechnicznie/src/main/resources/templates/names.html
+14
-0
ping.html
...-SpringTechnicznie/src/main/resources/templates/ping.html
+17
-0
No files found.
PC31-SpringTechnicznie/src/main/java/com/example/demo/HelloController.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
;
import
java.time.LocalDateTime
;
import
java.time.LocalTime
;
import
java.util.Map
;
import
jakarta.servlet.http.HttpServletRequest
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.RequestMapping
;
@Controller
public
class
HelloController
{
@RequestMapping
(
"/"
)
public
String
root
()
{
return
"index.html"
;
}
@RequestMapping
(
"/hello"
)
public
String
hello
(
Model
model
)
{
model
.
addAttribute
(
"txt"
,
"Hello world!"
);
model
.
addAttribute
(
"time"
,
LocalTime
.
now
());
return
"hello.html"
;
}
@RequestMapping
(
"/ping"
)
public
String
ping
(
HttpServletRequest
request
,
Model
model
)
{
// Do modelu można też dodać słownik / mapę.
// Odczyt wartości wygląda później np. tak ${clientInfo.userAgent}
String
ip
=
request
.
getRemoteAddr
();
System
.
out
.
println
(
"Zapytanie z adresu "
+
ip
);
model
.
addAttribute
(
"clientInfo"
,
Map
.
of
(
"userAgent"
,
request
.
getHeader
(
"User-Agent"
),
"ip"
,
ip
,
"data"
,
LocalDateTime
.
now
()
));
return
"ping.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Controller0.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
java.util.List
;
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
com.example.demo.model.Employee
;
@Controller
public
class
Controller0
{
@Autowired
private
Repository0_Memory
repository
;
@GetMapping
(
"/emps0"
)
public
String
showExampleEmployee
(
Model
model
)
{
List
<
Employee
>
emps
=
repository
.
findAll
();
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Controller1_EM.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
java.util.List
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.TypedQuery
;
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
com.example.demo.model.Employee
;
@Controller
@RequestMapping
(
"/emps1"
)
public
class
Controller1_EM
{
/* W tej wersji Controller bezpośrednio korzysta z technologii JPA/Hibernate.
* Ułatwieniem jest to, że Spring automatycznie "wstryknie" nam obiekt EntityManager.
*/
@Autowired
private
EntityManager
em
;
@GetMapping
public
String
showAll
(
Model
model
)
{
TypedQuery
<
Employee
>
query
=
em
.
createNamedQuery
(
"Employee.findAll"
,
Employee
.
class
);
List
<
Employee
>
emps
=
query
.
getResultList
();
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
@GetMapping
(
"/{id}"
)
public
String
showOne
(
Model
model
,
@PathVariable
(
"id"
)
Integer
employeeId
)
{
Employee
emp
=
em
.
find
(
Employee
.
class
,
employeeId
);
model
.
addAttribute
(
"emps"
,
List
.
of
(
emp
));
return
"employees.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Controller2.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
java.util.List
;
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
com.example.demo.model.Employee
;
@Controller
@RequestMapping
(
"/emps2"
)
public
class
Controller2
{
@Autowired
private
Repository2
repository
;
@GetMapping
public
String
showAll
(
Model
model
)
{
List
<
Employee
>
emps
=
repository
.
findAll
();
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
@GetMapping
(
"/{id}"
)
public
String
showOne
(
Model
model
,
@PathVariable
(
"id"
)
Integer
employeeId
)
{
Employee
emp
=
repository
.
findOne
(
employeeId
);
model
.
addAttribute
(
"emps"
,
List
.
of
(
emp
));
return
"employees.html"
;
}
@GetMapping
(
"/by_name"
)
public
String
showByName
(
Model
model
,
@RequestParam
(
"name"
)
String
name
)
{
List
<
Employee
>
emps
=
repository
.
findByLastName
(
name
);
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Controller3.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
java.util.List
;
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
com.example.demo.model.Employee
;
@Controller
@RequestMapping
(
"/emps3"
)
public
class
Controller3
{
/* Aby bezpośrednio w Controllerze nie używać kodów dot. Hibernate, SQL itd...
* wydzielam operacje bazodanowe do oddzielnej klasy, opisanej jako Repository
* i wstrzykuję referencję do repozytorium tutaj w controllerze.
* W doatku robię to porzez interfejs (zgodne z najlepszymi praktykami Springa),
* aby ułatwić podmianę implementacji na inną.
*/
@Autowired
private
Repository3_Interface
repository
;
@GetMapping
public
String
showAll
(
Model
model
)
{
List
<
Employee
>
emps
=
repository
.
findAll
();
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
@GetMapping
(
"/{id}"
)
public
String
showOne
(
Model
model
,
@PathVariable
(
"id"
)
Integer
employeeId
)
{
Employee
emp
=
repository
.
findOne
(
employeeId
);
model
.
addAttribute
(
"emps"
,
List
.
of
(
emp
));
return
"employees.html"
;
}
@GetMapping
(
"/by_name"
)
public
String
showByName
(
Model
model
,
@RequestParam
(
"name"
)
String
name
)
{
List
<
Employee
>
emps
=
repository
.
findByLastName
(
name
);
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Controller4.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
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
com.example.demo.model.Employee
;
@Controller
@RequestMapping
(
"/emps4"
)
public
class
Controller4
{
@Autowired
private
Repository4
repository
;
@GetMapping
public
String
showAll
(
Model
model
)
{
List
<
Employee
>
emps
=
repository
.
findAll
();
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
@GetMapping
(
"/{id}"
)
public
String
showOne
(
Model
model
,
@PathVariable
(
"id"
)
Integer
employeeId
)
{
Optional
<
Employee
>
emp
=
repository
.
findById
(
employeeId
);
List
<
Employee
>
emps
;
if
(
emp
.
isPresent
())
{
emps
=
List
.
of
(
emp
.
get
());
}
else
{
emps
=
List
.
of
();
}
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Controller5.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
java.math.BigDecimal
;
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
org.springframework.web.bind.annotation.ResponseBody
;
import
com.example.demo.model.Employee
;
@Controller
@RequestMapping
(
"/emps5"
)
public
class
Controller5
{
@Autowired
private
Repository5
repository
;
@GetMapping
(
"/all"
)
public
String
showAll
(
Model
model
)
{
List
<
Employee
>
emps
=
repository
.
findAll
();
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
@GetMapping
(
"/by_id/{id}"
)
public
String
showOne
(
Model
model
,
@PathVariable
(
"id"
)
Integer
employeeId
)
{
Optional
<
Employee
>
emp
=
repository
.
findById
(
employeeId
);
List
<
Employee
>
emps
;
if
(
emp
.
isPresent
())
{
emps
=
List
.
of
(
emp
.
get
());
}
else
{
emps
=
List
.
of
();
}
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
@GetMapping
(
"/by_name"
)
public
String
showByName
(
Model
model
,
@RequestParam
(
"name"
)
String
name
)
{
List
<
Employee
>
emps
=
repository
.
findByLastName
(
name
);
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
// http://localhost:8080/emps5/by_job/IT_PROG
@GetMapping
(
"/by_job/{job}"
)
public
String
getEmployeesByJobId
(
Model
model
,
@PathVariable
(
"job"
)
String
jobId
)
{
List
<
Employee
>
emps
=
repository
.
findByJob_JobId
(
jobId
);
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
// http://localhost:8080/emps5/by_city/Oxford
@GetMapping
(
"/by_city/{city}"
)
public
String
getEmployeesByCity
(
Model
model
,
@PathVariable
(
"city"
)
String
city
)
{
List
<
Employee
>
emps
=
repository
.
findByDepartment_Location_City
(
city
);
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
// http://localhost:8080/emps5/by_salary?min=5000&max=10000
@GetMapping
(
"/by_salary"
)
public
String
getEmployeesBySalary
(
Model
model
,
@RequestParam
(
name
=
"min"
,
defaultValue
=
"0"
)
BigDecimal
min
,
@RequestParam
(
name
=
"max"
,
defaultValue
=
"1000000000"
)
BigDecimal
max
)
{
List
<
Employee
>
employees
=
repository
.
findBySalary
(
min
,
max
);
model
.
addAttribute
(
"emps"
,
employees
);
return
"employees.html"
;
}
@GetMapping
(
"/by_year/{year}"
)
public
String
getEmployeesByYear
(
Model
model
,
@PathVariable
(
"year"
)
int
year
)
{
List
<
String
>
names
=
repository
.
namesByYear
(
year
);
model
.
addAttribute
(
"names"
,
names
);
return
"names.html"
;
}
@GetMapping
(
"/zara"
)
public
String
zarabiajacy
(
Model
model
,
@RequestParam
(
"id"
)
int
id
)
{
List
<
Employee
>
emps
=
repository
.
zarabiajacyWiecejNiz
(
id
);
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
@GetMapping
(
"/szef/{id}"
)
@ResponseBody
public
String
szef
(
@PathVariable
(
"id"
)
Integer
idPracownika
)
{
return
repository
.
nazwiskoSzefa
(
idPracownika
);
}
@GetMapping
(
"/save/{id}"
)
public
String
modify
(
Model
model
,
@PathVariable
(
"id"
)
Integer
idPracownika
,
@RequestParam
(
value
=
"salary"
,
required
=
false
)
BigDecimal
newSalary
,
@RequestParam
(
value
=
"first_name"
,
required
=
false
)
String
newFirstName
,
@RequestParam
(
value
=
"last_name"
,
required
=
false
)
String
newLastName
)
{
Optional
<
Employee
>
found
=
repository
.
findById
(
idPracownika
);
List
<
Employee
>
emps
;
if
(
found
.
isPresent
())
{
Employee
emp
=
found
.
get
();
if
(
newSalary
!=
null
)
emp
.
setSalary
(
newSalary
);
if
(
newFirstName
!=
null
)
emp
.
setFirstName
(
newFirstName
);
if
(
newLastName
!=
null
)
emp
.
setLastName
(
newLastName
);
repository
.
save
(
emp
);
emps
=
List
.
of
(
emp
);
}
else
{
emps
=
List
.
of
();
}
model
.
addAttribute
(
"emps"
,
emps
);
return
"employees.html"
;
}
@GetMapping
(
"/move/{id}"
)
@ResponseBody
public
String
modify
(
Model
model
,
@PathVariable
(
"id"
)
Integer
idPracownika
,
@RequestParam
(
value
=
"dep"
,
required
=
false
)
int
newDep
,
@RequestParam
(
value
=
"job"
,
required
=
false
)
String
newJob
)
{
repository
.
moveEmployee
(
idPracownika
,
newDep
,
newJob
);
return
"Przeniesiono"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Repository0_Memory.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
org.springframework.stereotype.Repository
;
import
com.example.demo.model.Department
;
import
com.example.demo.model.Employee
;
import
com.example.demo.model.Job
;
import
com.example.demo.model.Location
;
/* Klasa pełniąca rolę repozytoium, czyli dostraczanie danych,
* ale zaimplementowana bez bazy danych - po prostu zwraca przykładowe obiekty.
*/
@Repository
public
class
Repository0_Memory
{
public
List
<
Employee
>
findAll
()
{
Employee
emp
=
exampleEmployee1
();
return
List
.
of
(
emp
);
}
private
Employee
exampleEmployee1
()
{
Employee
emp
=
new
Employee
();
emp
.
setFirstName
(
"Jan"
);
emp
.
setLastName
(
"Kowalski"
);
emp
.
setSalary
(
BigDecimal
.
valueOf
(
12345
));
emp
.
setJob
(
exampleJob
());
emp
.
setDepartment
(
exampleDepartment
());
return
emp
;
}
private
Department
exampleDepartment
()
{
Location
loc
=
new
Location
();
loc
.
setCity
(
"Warszawa"
);
Department
dep
=
new
Department
();
dep
.
setDepartmentName
(
"Szkolenia"
);
dep
.
setLocation
(
loc
);
return
dep
;
}
private
Job
exampleJob
()
{
Job
job
=
new
Job
();
job
.
setJobTitle
(
"Trainer"
);
return
job
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Repository2.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
java.util.List
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.TypedQuery
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Repository
;
import
com.example.demo.model.Employee
;
@Repository
public
class
Repository2
{
@Autowired
private
EntityManager
em
;
public
List
<
Employee
>
findAll
()
{
TypedQuery
<
Employee
>
query
=
em
.
createNamedQuery
(
"Employee.findAll"
,
Employee
.
class
);
List
<
Employee
>
emps
=
query
.
getResultList
();
return
emps
;
}
public
Employee
findOne
(
int
employeeId
)
{
return
em
.
find
(
Employee
.
class
,
employeeId
);
}
public
List
<
Employee
>
findByLastName
(
String
lastName
)
{
TypedQuery
<
Employee
>
query
=
em
.
createQuery
(
"SELECT emp FROM Employee emp WHERE emp.lastName = :name"
,
Employee
.
class
);
query
.
setParameter
(
"name"
,
lastName
);
List
<
Employee
>
emps
=
query
.
getResultList
();
return
emps
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Repository3_Impl.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
java.util.List
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.TypedQuery
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Repository
;
import
com.example.demo.model.Employee
;
@Repository
public
class
Repository3_Impl
implements
Repository3_Interface
{
@Autowired
private
EntityManager
em
;
@Override
public
List
<
Employee
>
findAll
()
{
TypedQuery
<
Employee
>
query
=
em
.
createNamedQuery
(
"Employee.findAll"
,
Employee
.
class
);
List
<
Employee
>
emps
=
query
.
getResultList
();
return
emps
;
}
@Override
public
Employee
findOne
(
int
employeeId
)
{
return
em
.
find
(
Employee
.
class
,
employeeId
);
}
@Override
public
List
<
Employee
>
findByLastName
(
String
lastName
)
{
TypedQuery
<
Employee
>
query
=
em
.
createQuery
(
"SELECT emp FROM Employee emp WHERE emp.lastName = :name"
,
Employee
.
class
);
query
.
setParameter
(
"name"
,
lastName
);
List
<
Employee
>
emps
=
query
.
getResultList
();
return
emps
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Repository3_Interface.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
java.util.List
;
import
com.example.demo.model.Employee
;
public
interface
Repository3_Interface
{
List
<
Employee
>
findAll
();
Employee
findOne
(
int
employeeId
);
List
<
Employee
>
findByLastName
(
String
lastName
);
}
\ No newline at end of file
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Repository4.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
com.example.demo.model.Employee
;
public
interface
Repository4
extends
JpaRepository
<
Employee
,
Integer
>
{
// Spring sam tworzy instancję tego interfejsu, która zawiera implementację wszystkich standardowych metod
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/data/Repository5.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
data
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
java.util.concurrent.ThreadLocalRandom
;
import
java.util.stream.Collectors
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.data.jpa.repository.Query
;
import
com.example.demo.model.Employee
;
public
interface
Repository5
extends
JpaRepository
<
Employee
,
Integer
>
{
List
<
Employee
>
findByLastName
(
String
name
);
List
<
Employee
>
findByLastNameContainingIgnoringCase
(
String
name
);
List
<
Employee
>
findByJob_JobId
(
String
jobId
);
// employee.getDepartment().getLocation().getCity()
// ale w zapytaniu zostanie to zamienione na SQL, który JOIN-em dołącza tabele departments i locations i w WHERE sprawdza city
List
<
Employee
>
findByDepartment_Location_City
(
String
city
);
// Zapytanie w składni JPQL
@Query
(
"SELECT emp FROM Employee emp WHERE emp.salary BETWEEN :min AND :max ORDER BY emp.salary"
)
List
<
Employee
>
findBySalary
(
BigDecimal
min
,
BigDecimal
max
);
@Query
(
nativeQuery
=
true
,
value
=
"SELECT first_name || ' ' || last_name"
+
" FROM employees"
+
" WHERE extract(year FROM hire_date) = :year"
+
" ORDER BY last_name"
)
List
<
String
>
namesByYear
(
int
year
);
// W ramach takiego interfejsu można też pisać metody z własną implementacją.
// Tu przykład już nie bazodanowy...
default
List
<
Integer
>
losowe
(
int
limit
,
int
ilosc
)
{
return
ThreadLocalRandom
.
current
().
ints
(
ilosc
,
0
,
limit
).
boxed
().
collect
(
Collectors
.
toList
());
}
// podobno tak wygląda wywołanie procedury składowanej, ale nam nie działa
@Query
(
nativeQuery
=
true
,
value
=
"CALL przenies_pracownika(:idPracownika, :newDep, :newJob)"
)
void
moveEmployee
(
Integer
idPracownika
,
int
newDep
,
String
newJob
);
@Query
(
nativeQuery
=
true
,
value
=
"SELECT zarabiajacy_wiecej_niz(:id)"
)
List
<
Employee
>
zarabiajacyWiecejNiz
(
int
id
);
@Query
(
nativeQuery
=
true
,
value
=
"SELECT nazwisko_szefa(:idPracownika)"
)
String
nazwiskoSzefa
(
Integer
idPracownika
);
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/AController1.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.ModelAttribute
;
@Controller
public
class
AController1
{
@GetMapping
(
"/test0"
)
public
String
test0
()
{
return
"licznik.html"
;
}
@GetMapping
(
"/test1"
)
public
String
test1
(
Model
model
)
{
model
.
addAttribute
(
"licznik"
,
"Ala ma kota, a nie żaden licznik"
);
return
"licznik.html"
;
}
@GetMapping
(
"/test2"
)
public
String
test2
(
Model
model
,
Licznik
licznik
)
{
// Nawet gdy sami nie dodamy obiektu do modelu, to Spring i tak to zrobi
// model.addAttribute("licznik", licznik);
return
"licznik.html"
;
}
@GetMapping
(
"/test3"
)
public
String
test3
(
@ModelAttribute
Licznik
licznik
)
{
// Ta adnotacja mówi jednoznacznie, że ten parametr jest elementem modelu
return
"licznik.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/AController4.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.ModelAttribute
;
@Controller
public
class
AController4
{
@ModelAttribute
public
Licznik
getLicznik
()
{
// Gdy w klasie istynieje taka metoda oznaczona @ModelAttribute, to ona zostanie użyta do utworzenia obiektu.
// Jeśli nie ma takiej metody (jak w Controller1), to konstruktor domyślny.
return
new
Licznik
(
"ModelAttribute z metody"
,
400
);
}
@GetMapping
(
"/test4"
)
public
String
test4
(
@ModelAttribute
Licznik
licznik
)
{
return
"licznik.html"
;
}
@GetMapping
(
"/test4a"
)
public
String
test4a
(
Licznik
licznik
)
{
return
"licznik.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/AController5.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.ModelAttribute
;
import
org.springframework.web.bind.annotation.SessionAttributes
;
@Controller
@SessionAttributes
({
"licznik"
})
public
class
AController5
{
// W tej wersji atrybut modelu żyje w zakresie sesji.
@ModelAttribute
public
Licznik
getLicznik
()
{
return
new
Licznik
(
"v5 z metody"
,
500
);
}
@GetMapping
(
"/test5"
)
public
String
test5
(
@ModelAttribute
Licznik
licznik
)
{
return
"licznik.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/AController6.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
@Controller
public
class
AController6
{
@Autowired
private
LicznikComponent6
licznikComponent6
;
@GetMapping
(
"/test6"
)
public
String
test6
(
Model
model
)
{
model
.
addAttribute
(
"licznik"
,
licznikComponent6
.
getLicznik
());
return
"licznik.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/AController7.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
@Controller
public
class
AController7
{
@Autowired
private
LicznikComponent7
licznikComponent7
;
@GetMapping
(
"/test7"
)
public
String
test7
(
Model
model
)
{
model
.
addAttribute
(
"licznik"
,
licznikComponent7
.
getLicznik
());
return
"licznik.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/AController8.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
@Controller
public
class
AController8
{
@Autowired
private
LicznikBean8
licznikBean8
;
@GetMapping
(
"/test8"
)
public
String
test8
(
Model
model
)
{
model
.
addAttribute
(
"licznik"
,
licznikBean8
.
getLicznik
());
return
"licznik.html"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/Configuration8.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
public
class
Configuration8
{
// Adnotacja @Bean jest używana na poziomie metody.
// Wynik wywołania tej metody zostanie zapamiętany przez Springa i będzie traktowany jak "bean",
// czyli zostanie wstrzyknięty tam, gdzie będzie go ktoś potrzebował.
// Taka metoda musi być umieszczona w klasie, którą "przeczyta" Spring i wykona takie metody.
// Najczęściej stosuje się klasy oznaczone @Configuration
@Bean
public
LicznikBean8
generuj
()
{
LicznikBean8
wynik
=
new
LicznikBean8
();
wynik
.
setLicznik
(
new
Licznik
(
"bean z konfiguracji"
,
800
));
return
wynik
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/Licznik.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
public
class
Licznik
{
private
String
napis
;
private
int
value
;
public
Licznik
(
String
napis
,
int
value
)
{
this
.
napis
=
napis
;
this
.
value
=
value
;
}
public
Licznik
()
{
this
(
"default"
,
0
);
}
public
synchronized
String
getNapis
()
{
return
napis
;
}
public
synchronized
void
setNapis
(
String
napis
)
{
this
.
napis
=
napis
;
}
public
synchronized
int
getValue
()
{
return
++
value
;
}
@Override
public
String
toString
()
{
return
"Licznik [napis: "
+
getNapis
()
+
", value: "
+
getValue
()
+
"]"
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/LicznikBean8.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
public
class
LicznikBean8
{
private
Licznik
licznik
;
public
Licznik
getLicznik
()
{
return
licznik
;
}
public
void
setLicznik
(
Licznik
licznik
)
{
this
.
licznik
=
licznik
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/LicznikComponent6.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
import
org.springframework.stereotype.Component
;
@Component
// albo @Service, @Repository
public
class
LicznikComponent6
{
private
Licznik
licznik
=
new
Licznik
();
public
Licznik
getLicznik
()
{
return
licznik
;
}
public
void
setLicznik
(
Licznik
licznik
)
{
this
.
licznik
=
licznik
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/licznik/LicznikComponent7.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
licznik
;
import
org.springframework.context.annotation.Scope
;
import
org.springframework.context.annotation.ScopedProxyMode
;
import
org.springframework.stereotype.Component
;
@Component
@Scope
(
scopeName
=
"session"
,
proxyMode
=
ScopedProxyMode
.
TARGET_CLASS
)
public
class
LicznikComponent7
{
private
Licznik
licznik
=
new
Licznik
();
public
Licznik
getLicznik
()
{
return
licznik
;
}
public
void
setLicznik
(
Licznik
licznik
)
{
this
.
licznik
=
licznik
;
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/model/Country.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
model
;
import
java.io.Serializable
;
import
jakarta.persistence.*
;
/**
* The persistent class for the countries database table.
*
*/
@Entity
@Table
(
name
=
"countries"
)
@NamedQuery
(
name
=
"Country.findAll"
,
query
=
"SELECT c FROM Country c"
)
public
class
Country
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@Column
(
name
=
"country_id"
)
private
String
countryId
;
@Column
(
name
=
"country_name"
)
private
String
countryName
;
//bi-directional many-to-one association to Region
@ManyToOne
@JoinColumn
(
name
=
"region_id"
)
private
Region
region
;
public
Country
()
{
}
public
String
getCountryId
()
{
return
this
.
countryId
;
}
public
void
setCountryId
(
String
countryId
)
{
this
.
countryId
=
countryId
;
}
public
String
getCountryName
()
{
return
this
.
countryName
;
}
public
void
setCountryName
(
String
countryName
)
{
this
.
countryName
=
countryName
;
}
public
Region
getRegion
()
{
return
this
.
region
;
}
public
void
setRegion
(
Region
region
)
{
this
.
region
=
region
;
}
}
\ No newline at end of file
PC31-SpringTechnicznie/src/main/java/com/example/demo/model/Department.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
model
;
import
java.io.Serializable
;
import
java.util.List
;
import
jakarta.persistence.*
;
/**
* The persistent class for the departments database table.
*
*/
@Entity
@Table
(
name
=
"departments"
)
@NamedQuery
(
name
=
"Department.findAll"
,
query
=
"SELECT d FROM Department d"
)
public
class
Department
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@SequenceGenerator
(
name
=
"DEPARTMENTS_DEPARTMENTID_GENERATOR"
,
sequenceName
=
"DEPARTMENTS_SEQ"
,
allocationSize
=
10
)
@GeneratedValue
(
strategy
=
GenerationType
.
SEQUENCE
,
generator
=
"DEPARTMENTS_DEPARTMENTID_GENERATOR"
)
@Column
(
name
=
"department_id"
)
private
Integer
departmentId
;
@Column
(
name
=
"department_name"
)
private
String
departmentName
;
@Column
(
name
=
"manager_id"
)
private
Integer
managerId
;
//uni-directional many-to-one association to Location
@ManyToOne
@JoinColumn
(
name
=
"location_id"
)
private
Location
location
;
//bi-directional many-to-one association to Employee
@OneToMany
(
mappedBy
=
"department"
)
private
List
<
Employee
>
employees
;
public
Department
()
{
}
public
Integer
getDepartmentId
()
{
return
this
.
departmentId
;
}
public
void
setDepartmentId
(
Integer
departmentId
)
{
this
.
departmentId
=
departmentId
;
}
public
String
getDepartmentName
()
{
return
this
.
departmentName
;
}
public
void
setDepartmentName
(
String
departmentName
)
{
this
.
departmentName
=
departmentName
;
}
public
Integer
getManagerId
()
{
return
this
.
managerId
;
}
public
void
setManagerId
(
Integer
managerId
)
{
this
.
managerId
=
managerId
;
}
public
Location
getLocation
()
{
return
this
.
location
;
}
public
void
setLocation
(
Location
location
)
{
this
.
location
=
location
;
}
public
List
<
Employee
>
getEmployees
()
{
return
this
.
employees
;
}
public
void
setEmployees
(
List
<
Employee
>
employees
)
{
this
.
employees
=
employees
;
}
public
Employee
addEmployee
(
Employee
employee
)
{
getEmployees
().
add
(
employee
);
employee
.
setDepartment
(
this
);
return
employee
;
}
public
Employee
removeEmployee
(
Employee
employee
)
{
getEmployees
().
remove
(
employee
);
employee
.
setDepartment
(
null
);
return
employee
;
}
}
\ No newline at end of file
PC31-SpringTechnicznie/src/main/java/com/example/demo/model/Employee.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
model
;
import
java.io.Serializable
;
import
java.math.BigDecimal
;
import
java.util.Date
;
import
jakarta.persistence.*
;
/**
* The persistent class for the employees database table.
*
*/
@Entity
@Table
(
name
=
"employees"
)
@NamedQuery
(
name
=
"Employee.findAll"
,
query
=
"SELECT e FROM Employee e"
)
public
class
Employee
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@SequenceGenerator
(
name
=
"EMPLOYEES_EMPLOYEEID_GENERATOR"
,
sequenceName
=
"EMPLOYEES_SEQ"
,
allocationSize
=
1
)
@GeneratedValue
(
strategy
=
GenerationType
.
SEQUENCE
,
generator
=
"EMPLOYEES_EMPLOYEEID_GENERATOR"
)
@Column
(
name
=
"employee_id"
)
private
Integer
employeeId
;
@Column
(
name
=
"commission_pct"
)
private
BigDecimal
commissionPct
;
private
String
email
;
@Column
(
name
=
"first_name"
)
private
String
firstName
;
@Temporal
(
TemporalType
.
DATE
)
@Column
(
name
=
"hire_date"
)
private
Date
hireDate
;
@Column
(
name
=
"last_name"
)
private
String
lastName
;
@Column
(
name
=
"phone_number"
)
private
String
phoneNumber
;
private
BigDecimal
salary
;
//bi-directional many-to-one association to Department
@ManyToOne
@JoinColumn
(
name
=
"department_id"
)
private
Department
department
;
//uni-directional many-to-one association to Employee
@ManyToOne
@JoinColumn
(
name
=
"manager_id"
)
private
Employee
manager
;
//bi-directional many-to-one association to Job
@ManyToOne
@JoinColumn
(
name
=
"job_id"
)
private
Job
job
;
public
Employee
()
{
}
public
Integer
getEmployeeId
()
{
return
this
.
employeeId
;
}
public
void
setEmployeeId
(
Integer
employeeId
)
{
this
.
employeeId
=
employeeId
;
}
public
BigDecimal
getCommissionPct
()
{
return
this
.
commissionPct
;
}
public
void
setCommissionPct
(
BigDecimal
commissionPct
)
{
this
.
commissionPct
=
commissionPct
;
}
public
String
getEmail
()
{
return
this
.
email
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
String
getFirstName
()
{
return
this
.
firstName
;
}
public
void
setFirstName
(
String
firstName
)
{
this
.
firstName
=
firstName
;
}
public
Date
getHireDate
()
{
return
this
.
hireDate
;
}
public
void
setHireDate
(
Date
hireDate
)
{
this
.
hireDate
=
hireDate
;
}
public
String
getLastName
()
{
return
this
.
lastName
;
}
public
void
setLastName
(
String
lastName
)
{
this
.
lastName
=
lastName
;
}
public
String
getPhoneNumber
()
{
return
this
.
phoneNumber
;
}
public
void
setPhoneNumber
(
String
phoneNumber
)
{
this
.
phoneNumber
=
phoneNumber
;
}
public
BigDecimal
getSalary
()
{
return
this
.
salary
;
}
public
void
setSalary
(
BigDecimal
salary
)
{
this
.
salary
=
salary
;
}
public
Department
getDepartment
()
{
return
this
.
department
;
}
public
void
setDepartment
(
Department
department
)
{
this
.
department
=
department
;
}
public
Employee
getManager
()
{
return
this
.
manager
;
}
public
void
setManager
(
Employee
manager
)
{
this
.
manager
=
manager
;
}
public
Job
getJob
()
{
return
this
.
job
;
}
public
void
setJob
(
Job
job
)
{
this
.
job
=
job
;
}
}
\ No newline at end of file
PC31-SpringTechnicznie/src/main/java/com/example/demo/model/Job.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
model
;
import
java.io.Serializable
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
jakarta.persistence.*
;
/**
* The persistent class for the jobs database table.
*
*/
@Entity
@Table
(
name
=
"jobs"
)
@NamedQuery
(
name
=
"Job.findAll"
,
query
=
"SELECT j FROM Job j"
)
public
class
Job
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@Column
(
name
=
"job_id"
)
private
String
jobId
;
@Column
(
name
=
"job_title"
)
private
String
jobTitle
;
@Column
(
name
=
"max_salary"
)
private
BigDecimal
maxSalary
;
@Column
(
name
=
"min_salary"
)
private
BigDecimal
minSalary
;
//bi-directional many-to-one association to Employee
@OneToMany
(
mappedBy
=
"job"
)
private
List
<
Employee
>
employees
;
public
Job
()
{
}
public
String
getJobId
()
{
return
this
.
jobId
;
}
public
void
setJobId
(
String
jobId
)
{
this
.
jobId
=
jobId
;
}
public
String
getJobTitle
()
{
return
this
.
jobTitle
;
}
public
void
setJobTitle
(
String
jobTitle
)
{
this
.
jobTitle
=
jobTitle
;
}
public
BigDecimal
getMaxSalary
()
{
return
this
.
maxSalary
;
}
public
void
setMaxSalary
(
BigDecimal
maxSalary
)
{
this
.
maxSalary
=
maxSalary
;
}
public
BigDecimal
getMinSalary
()
{
return
this
.
minSalary
;
}
public
void
setMinSalary
(
BigDecimal
minSalary
)
{
this
.
minSalary
=
minSalary
;
}
public
List
<
Employee
>
getEmployees
()
{
return
this
.
employees
;
}
public
void
setEmployees
(
List
<
Employee
>
employees
)
{
this
.
employees
=
employees
;
}
public
Employee
addEmployee
(
Employee
employee
)
{
getEmployees
().
add
(
employee
);
employee
.
setJob
(
this
);
return
employee
;
}
public
Employee
removeEmployee
(
Employee
employee
)
{
getEmployees
().
remove
(
employee
);
employee
.
setJob
(
null
);
return
employee
;
}
}
\ No newline at end of file
PC31-SpringTechnicznie/src/main/java/com/example/demo/model/Location.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
model
;
import
java.io.Serializable
;
import
jakarta.persistence.*
;
/**
* The persistent class for the locations database table.
*
*/
@Entity
@Table
(
name
=
"locations"
)
@NamedQuery
(
name
=
"Location.findAll"
,
query
=
"SELECT l FROM Location l"
)
public
class
Location
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@SequenceGenerator
(
name
=
"LOCATIONS_LOCATIONID_GENERATOR"
,
sequenceName
=
"LOCATIONS_SEQ"
,
allocationSize
=
100
)
@GeneratedValue
(
strategy
=
GenerationType
.
SEQUENCE
,
generator
=
"LOCATIONS_LOCATIONID_GENERATOR"
)
@Column
(
name
=
"location_id"
)
private
Integer
locationId
;
private
String
city
;
@Column
(
name
=
"postal_code"
)
private
String
postalCode
;
@Column
(
name
=
"state_province"
)
private
String
stateProvince
;
@Column
(
name
=
"street_address"
)
private
String
streetAddress
;
//uni-directional many-to-one association to Country
@ManyToOne
@JoinColumn
(
name
=
"country_id"
)
private
Country
country
;
public
Location
()
{
}
public
Integer
getLocationId
()
{
return
this
.
locationId
;
}
public
void
setLocationId
(
Integer
locationId
)
{
this
.
locationId
=
locationId
;
}
public
String
getCity
()
{
return
this
.
city
;
}
public
void
setCity
(
String
city
)
{
this
.
city
=
city
;
}
public
String
getPostalCode
()
{
return
this
.
postalCode
;
}
public
void
setPostalCode
(
String
postalCode
)
{
this
.
postalCode
=
postalCode
;
}
public
String
getStateProvince
()
{
return
this
.
stateProvince
;
}
public
void
setStateProvince
(
String
stateProvince
)
{
this
.
stateProvince
=
stateProvince
;
}
public
String
getStreetAddress
()
{
return
this
.
streetAddress
;
}
public
void
setStreetAddress
(
String
streetAddress
)
{
this
.
streetAddress
=
streetAddress
;
}
public
Country
getCountry
()
{
return
this
.
country
;
}
public
void
setCountry
(
Country
country
)
{
this
.
country
=
country
;
}
}
\ No newline at end of file
PC31-SpringTechnicznie/src/main/java/com/example/demo/model/Region.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
model
;
import
java.io.Serializable
;
import
java.util.List
;
import
jakarta.persistence.*
;
/**
* The persistent class for the regions database table.
*
*/
@Entity
@Table
(
name
=
"regions"
)
@NamedQuery
(
name
=
"Region.findAll"
,
query
=
"SELECT r FROM Region r"
)
public
class
Region
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@Column
(
name
=
"region_id"
)
private
Integer
regionId
;
@Column
(
name
=
"region_name"
)
private
String
regionName
;
//bi-directional many-to-one association to Country
@OneToMany
(
mappedBy
=
"region"
)
private
List
<
Country
>
countries
;
public
Region
()
{
}
public
Integer
getRegionId
()
{
return
this
.
regionId
;
}
public
void
setRegionId
(
Integer
regionId
)
{
this
.
regionId
=
regionId
;
}
public
String
getRegionName
()
{
return
this
.
regionName
;
}
public
void
setRegionName
(
String
regionName
)
{
this
.
regionName
=
regionName
;
}
public
List
<
Country
>
getCountries
()
{
return
this
.
countries
;
}
public
void
setCountries
(
List
<
Country
>
countries
)
{
this
.
countries
=
countries
;
}
public
Country
addCountry
(
Country
country
)
{
getCountries
().
add
(
country
);
country
.
setRegion
(
this
);
return
country
;
}
public
Country
removeCountry
(
Country
country
)
{
getCountries
().
remove
(
country
);
country
.
setRegion
(
null
);
return
country
;
}
}
\ No newline at end of file
PC31-SpringTechnicznie/src/main/java/com/example/demo/wstrzykiwanie/FabrykaKomponentow.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
wstrzykiwanie
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
public
class
FabrykaKomponentow
{
{
System
.
out
.
println
(
"FabrykaKomponentow init"
);
}
@Bean
Komponent
dajKomponent
()
{
System
.
out
.
println
(
"Zaraz utworzę Komponent w klasie FabrykaKomponentow"
);
return
new
Komponent
();
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/wstrzykiwanie/Inject1.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
wstrzykiwanie
;
import
jakarta.annotation.PostConstruct
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
@Controller
public
class
Inject1
{
// 1. sposób wstrzykiwania: pole
@Autowired
private
Komponent
komponent
;
@Autowired
private
Repo
repo
;
{
System
.
out
.
println
(
"Inject1 init"
);
}
// initialization block
public
Inject1
()
{
System
.
out
.
println
(
"Inject1 constr, komponent = "
+
komponent
);
// null
}
@PostConstruct
public
void
pc
()
{
System
.
out
.
println
(
"inject1 @PostConstruct, komponent = "
+
komponent
);
}
@RequestMapping
(
"/inject1"
)
@ResponseBody
public
String
get
()
{
return
repo
.
getText
()
+
" "
+
komponent
.
getValue
();
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/wstrzykiwanie/Inject2.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
wstrzykiwanie
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
@Controller
public
class
Inject2
{
{
System
.
out
.
println
(
"Inject2 init"
);
}
// 2. sposób wstrzykiwania: setter
private
Komponent
komponent
;
private
Repo
repo
;
public
Komponent
getKomponent
()
{
return
komponent
;
}
@Autowired
public
void
setKomponent
(
Komponent
komponent
)
{
System
.
out
.
println
(
"SET KOMPONENT"
);
this
.
komponent
=
komponent
;
}
public
Repo
getRepo
()
{
return
repo
;
}
@Autowired
public
void
setRepo
(
Repo
repo
)
{
this
.
repo
=
repo
;
}
@RequestMapping
(
"/inject2"
)
@ResponseBody
public
String
get
()
{
return
repo
.
getText
()
+
" "
+
komponent
.
getValue
();
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/wstrzykiwanie/Inject3.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
wstrzykiwanie
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
@Controller
public
class
Inject3
{
{
System
.
out
.
println
(
"Inject3 init"
);
}
// 3. sposób wstrzykiwania: konstruktor
// To podejście zwykle jest podawane jako najlepsze, bo umożliwia łatwą podmianę komponentów na "mocki" podczas testów
// Przy tym podejściu problemem byłaby jednak cykliczna zależność między komponentami.
private
Komponent
komponent
;
private
Repo
repo
;
// Zauważmy, że ta klasa nie jest już poprawnym "JavaBean", bo nie posiada konstruktora domyślnego.
// Adnotacja Autowired tym razem nie jest potrzebna.
public
Inject3
(
Komponent
komponent
,
Repo
repo
)
{
System
.
out
.
println
(
"KONSTRUKTOR Inject3(komponent, repo)"
);
this
.
komponent
=
komponent
;
this
.
repo
=
repo
;
}
@RequestMapping
(
"/inject3"
)
@ResponseBody
public
String
get
()
{
return
repo
.
getText
()
+
" "
+
komponent
.
getValue
();
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/wstrzykiwanie/Komponent.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
wstrzykiwanie
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
class
Komponent
{
{
System
.
out
.
println
(
"Komponent init"
);
}
private
AtomicInteger
licznik
=
new
AtomicInteger
();
public
int
getValue
()
{
return
licznik
.
incrementAndGet
();
}
}
PC31-SpringTechnicznie/src/main/java/com/example/demo/wstrzykiwanie/Repo.java
0 → 100644
View file @
44dc5a66
package
com
.
example
.
demo
.
wstrzykiwanie
;
import
org.springframework.stereotype.Repository
;
@Repository
// na podobnych zasadach: @Component, @Service, @Repository - tworzony jest jeden obiekt "singleton"
public
class
Repo
{
{
System
.
out
.
println
(
"Repo init"
);
}
public
String
getText
()
{
return
"Ala ma kota"
;
}
}
PC31-SpringTechnicznie/src/main/resources/application.properties
View file @
44dc5a66
spring.datasource.url
=
jdbc:postgresql://localhost:5432/hr
spring.datasource.username
=
kurs
spring.datasource.password
=
abc123
spring.jpa.show-sql
=
true
PC31-SpringTechnicznie/src/main/resources/static/styl.css
0 → 100644
View file @
44dc5a66
body
{
background-color
:
#FFFFCC
;
font-family
:
'Arial'
,
sans-serif
;
}
h1
{
color
:
green
;
text-align
:
center
;
}
form
{
margin
:
30px
auto
;
padding
:
20px
;
width
:
800px
;
border
:
4px
solid
blue
;
background-color
:
#AAEEFF
;
}
.wynik
{
background-color
:
#FFFFFF
;
border
:
3px
solid
green
;
margin
:
20px
auto
;
width
:
800px
;
padding
:
10px
;
color
:
green
;
}
.error
{
background-color
:
#FFFFFF
;
border
:
6px
double
red
;
margin
:
20px
auto
;
padding
:
10px
;
width
:
800px
;
color
:
red
;
font-weight
:
bold
;
}
PC31-SpringTechnicznie/src/main/resources/templates/employees.html
0 → 100644
View file @
44dc5a66
<!DOCTYPE html>
<html
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Employees
</title>
<style
type=
"text/css"
>
table
{
border-collapse
:
collapse
;
}
th
,
td
{
border
:
1px
solid
#444444
;
padding
:
4px
;
}
</style>
</head>
<body>
<table>
<tr>
<th>
Imię
</th><th>
Nazwisko
</th><th>
Stanowisko
</th><th>
Pensja
</th><th>
Departament
</th><th>
Miasto
</th>
</tr>
<tr
th:each=
"emp : ${emps}"
>
<td
th:text=
"${emp.firstName}"
>
firstName
</td>
<td
th:text=
"${emp.lastName}"
>
lastName
</td>
<td
th:text=
"${emp.job.jobTitle}"
>
jobTitle
</td>
<td
th:text=
"${emp.salary}"
>
salary
</td>
<td
th:text=
"${emp.department.departmentName}"
th:if=
"${emp.department != null}"
>
departmentName
</td>
<td
th:text=
"${emp.department.location.city}"
th:if=
"${emp.department != null}"
>
city
</td>
</tr>
</table>
<p><a
th:href=
"@{/}"
>
wróć do spisu treści
</a></p>
</body>
</html>
PC31-SpringTechnicznie/src/main/resources/templates/hello.html
0 → 100644
View file @
44dc5a66
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"UTF-8"
>
<title>
Hello
</title>
</head>
<body>
<p>
[[${txt}]]
</p>
<p>
Teraz jest godzina: [[${time}]]
</p>
</body>
</html>
PC31-SpringTechnicznie/src/main/resources/templates/index.html
0 → 100644
View file @
44dc5a66
<!DOCTYPE html>
<html
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Spis treści
</title>
<link
rel=
"stylesheet"
type=
"text/css"
th:href=
"@{/styl.css}"
href=
"../static/styl.css"
>
</head>
<body>
<h1>
Spis treści
</h1>
<ul>
<li><a
href=
"/hello"
>
Hello
</a></li>
<li><a
href=
"/ping"
>
Ping
</a></li>
</ul>
<h2>
Spring Data
</h2>
<ul>
<li><a
href=
"/emps0"
>
InMemoryRepository
</a></li>
<li><a
href=
"/emps1"
>
emps1
</a>
- wersja z bespośrednio użytym JPA
</li>
<li><a
href=
"/emps1/100"
>
emps1/100
</a>
- odczyt jednego pracownika po id
</li>
<li><a
href=
"/emps2"
>
emps2
</a>
- wersja z użyciem JPA wyciągniętym do osobnej klasy @Repository
</li>
<li><a
href=
"/emps2/100"
>
emps2/100
</a>
- odczyt jednego pracownika po id
</li>
<li><a
href=
"/emps2/by_name?name=King"
>
emps2/by_name?last_name=King
</a>
- odczyt pracowników po nazwisku, przykład
<strong>
Query
</strong></li>
<li><a
href=
"/emps3"
>
emps3
</a>
- j.w. ale interfejs
</li>
<li><a
href=
"/emps3/100"
>
emps3/100
</a>
- odczyt jednego pracownika po id
</li>
<li><a
href=
"/emps3/by_name?name=King"
>
emps3/by_name?last_name=King
</a>
- odczyt pracowników po nazwisku, przykład
<strong>
Query
</strong></li>
<li><a
href=
"/emps4"
>
emps4
</a>
- Spring Data JpaRepository – domyślny interfejs
</li>
<li><a
href=
"/emps4/100"
>
emps4/100
</a>
- odczyt jednego pracownika po id
</li>
<li><a
href=
"/emps5/all"
>
emps5
</a>
- Spring Data JpaRepository – rozszerzony interfejs
</li>
<li><a
href=
"/emps5/by_id/101"
>
emps5/by_id/101
</a>
- wg id
</li>
<li><a
href=
"/emps5/by_name?name=King"
>
emps5/by_name?last_name=King
</a>
- odczyt pracowników po nazwisku, przykład
<strong>
Query
</strong></li>
<li><a
href=
"/emps5/by_job/ST_CLERK"
>
/by_job/ST_CLERK
</a>
- wg stanowiska
<li><a
href=
"/emps5/by_city/Seattle"
>
emps5/by_city/Seattle
</a>
- wg miasta
</li>
<li><a
href=
"/emps5/by_salary?min=5000&max=10000"
>
emps5/by_salary
</a>
- wg pensji
</li>
<li><a
href=
"/emps5/by_year/1999"
>
emps5/by_year
</a>
- wg roku zatrudneinia - przykład
<strong>
native query
</strong></li>
<li><a
href=
"/emps5/szef/105"
>
szef/105
</a>
- nazwisko sefa (przykład własnej funkcji)
<li><a
href=
"/emps5/zara?id=103"
>
zara?id=103
</a>
- zarabiający więcej niż (przykład własnej funkcji)
<li><a
href=
"/emps5/move/110?dep=90&job=AD_PRES"
>
move
</a>
- przenieś pracownika
</ul>
<h2>
Wstrzykiwanie na 3 sposoby
</h2>
<ul>
<li><a
href=
"/inject1"
>
Wstrzykiwanie 1
</a>
- pole
</li>
<li><a
href=
"/inject2"
>
Wstrzykiwanie 2
</a>
- setter
</li>
<li><a
href=
"/inject3"
>
Wstrzykiwanie 3
</a>
- konstruktor
</li>
</ul>
<h2>
Eksperymenty z beanami
</h2>
<ul>
<li><a
href=
"/test0"
>
Test 0
</a>
—
brak modelu
</li>
<li><a
href=
"/test1"
>
Test 1
</a>
—
<code>
Model
</code></li>
<li><a
href=
"/test2"
>
Test 2
</a>
—
parametr metody i
<code>
Model
</code></li>
<li><a
href=
"/test3"
>
Test 3
</a>
—
parametr
<code>
@ModelAttribute
</code></li>
<li><a
href=
"/test4"
>
Test 4
</a>
—
<code>
@ModelAttribute
</code>
przy metodzie i w parametrze
</li>
<li><a
href=
"/test4a"
>
Test 4a
</a>
—
<code>
@ModelAttribute
</code>
przy metodzie, ale nie w parametrze
</li>
<li><a
href=
"/test5"
>
Test 5
</a>
—
<code>
@ModelAttribute
</code>
w połączeniu z
<code>
@SessionAttributes
</code></li>
<li><a
href=
"/test6"
>
Test 6
</a>
—
<code>
@Component
</code>
w połączeniu z
<code>
@Autowired
</code></li>
<li><a
href=
"/test7"
>
Test 7
</a>
—
<code>
@Component @Scope(session)
</code></li>
<li><a
href=
"/test8"
>
Test 8
</a>
—
<code>
@Bean
</code>
w połączeniu z
<code>
@Autowired
</code></li>
</ul>
</body>
</html>
PC31-SpringTechnicznie/src/main/resources/templates/licznik.html
0 → 100644
View file @
44dc5a66
<!DOCTYPE html>
<html
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Test beanów za pomocą licznika
</title>
<link
rel=
"stylesheet"
type=
"text/css"
th:href=
"@{/styl.css}"
/>
</head>
<body>
<h1>
Test beanów za pomocą licznika
</h1>
<p>
licznik =
<strong
th:text=
"${licznik}"
>
X
</strong></p>
</body>
</html>
PC31-SpringTechnicznie/src/main/resources/templates/names.html
0 → 100644
View file @
44dc5a66
<!DOCTYPE html>
<html
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Names
</title>
</head>
<body>
<ul>
<li
th:each=
"name : ${names}"
th:text=
"${name}"
>
ktoś
</li>
</ul>
<p><a
th:href=
"@{/}"
>
wróć do spisu treści
</a></p>
</body>
</html>
PC31-SpringTechnicznie/src/main/resources/templates/ping.html
0 → 100644
View file @
44dc5a66
<!DOCTYPE html>
<html
lang=
"pl"
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Ping
</title>
<link
rel=
"stylesheet"
type=
"text/css"
th:href=
"@{/styl.css}"
href=
"../static/styl.css"
>
</head>
<body>
<h2>
Informacje o kliencie
</h2>
<ul>
<li>
Adres:
<strong
th:text=
"${clientInfo.ip}"
>
1.2.3.4
</strong></li>
<li>
Przeglądarka:
<strong
th:text=
"${clientInfo.userAgent}"
>
IE
</strong></li>
<li>
Data:
<strong
th:text=
"${clientInfo.data}"
>
data
</strong></li>
</ul>
</body>
</html>
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