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
4025637e
Commit
4025637e
authored
Aug 19, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Inicjalizacja bazy jako operacja webowa
parent
ead6c5db
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
48 deletions
+43
-48
Pc51SpringDataH2Application.java
...in/java/com/example/demo/Pc51SpringDataH2Application.java
+0
-46
DBService.java
...DataH2/src/main/java/com/example/demo/repo/DBService.java
+22
-0
RootController.java
...H2/src/main/java/com/example/demo/web/RootController.java
+12
-0
index.html
PC51-SpringDataH2/src/main/resources/templates/index.html
+9
-2
No files found.
PC51-SpringDataH2/src/main/java/com/example/demo/Pc51SpringDataH2Application.java
View file @
4025637e
package
com
.
example
.
demo
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.boot.CommandLineRunner
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.context.annotation.Bean
;
import
com.example.demo.model.Customer
;
import
com.example.demo.repo.CustomerRepository
;
@SpringBootApplication
public
class
Pc51SpringDataH2Application
{
...
...
@@ -17,43 +10,4 @@ public class Pc51SpringDataH2Application {
SpringApplication
.
run
(
Pc51SpringDataH2Application
.
class
,
args
);
}
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
Pc51SpringDataH2Application
.
class
);
@Bean
public
CommandLineRunner
demo
(
CustomerRepository
repository
)
{
return
(
args
)
->
{
// save a few customers
repository
.
save
(
new
Customer
(
"Jack"
,
"Bauer"
));
repository
.
save
(
new
Customer
(
"Chloe"
,
"O'Brian"
));
repository
.
save
(
new
Customer
(
"Kim"
,
"Bauer"
));
repository
.
save
(
new
Customer
(
"David"
,
"Palmer"
));
repository
.
save
(
new
Customer
(
"Michelle"
,
"Dessler"
));
// fetch all customers
log
.
info
(
"Customers found with findAll():"
);
log
.
info
(
"-------------------------------"
);
for
(
Customer
customer
:
repository
.
findAll
())
{
log
.
info
(
customer
.
toString
());
}
log
.
info
(
""
);
// fetch an individual customer by ID
Customer
customer
=
repository
.
findById
(
1L
);
log
.
info
(
"Customer found with findById(1L):"
);
log
.
info
(
"--------------------------------"
);
log
.
info
(
customer
.
toString
());
log
.
info
(
""
);
// fetch customers by last name
log
.
info
(
"Customer found with findByLastName('Bauer'):"
);
log
.
info
(
"--------------------------------------------"
);
repository
.
findByLastName
(
"Bauer"
).
forEach
(
bauer
->
{
log
.
info
(
bauer
.
toString
());
});
// for (Customer bauer : repository.findByLastName("Bauer")) {
// log.info(bauer.toString());
// }
log
.
info
(
""
);
};
}
}
PC51-SpringDataH2/src/main/java/com/example/demo/repo/DBService.java
0 → 100644
View file @
4025637e
package
com
.
example
.
demo
.
repo
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
com.example.demo.model.Customer
;
@Service
public
class
DBService
{
@Autowired
private
CustomerRepository
customerRepository
;
public
void
initDatabase
()
{
customerRepository
.
save
(
new
Customer
(
"Jack"
,
"Bauer"
));
customerRepository
.
save
(
new
Customer
(
"Chloe"
,
"O'Brian"
));
customerRepository
.
save
(
new
Customer
(
"Kim"
,
"Bauer"
));
customerRepository
.
save
(
new
Customer
(
"David"
,
"Palmer"
));
customerRepository
.
save
(
new
Customer
(
"Michelle"
,
"Dessler"
));
}
}
PC51-SpringDataH2/src/main/java/com/example/demo/web/RootController.java
View file @
4025637e
package
com
.
example
.
demo
.
web
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
com.example.demo.repo.DBService
;
@Controller
public
class
RootController
{
@Autowired
private
DBService
dbService
;
@RequestMapping
(
"/"
)
public
String
root
()
{
return
"index.html"
;
}
@RequestMapping
(
"/init"
)
public
String
initDatabase
()
{
dbService
.
initDatabase
();
return
"redirect:/"
;
}
}
PC51-SpringDataH2/src/main/resources/templates/index.html
View file @
4025637e
...
...
@@ -6,9 +6,16 @@
<link
rel=
"stylesheet"
type=
"text/css"
th:href=
"@{/styl.css}"
href=
"../static/styl.css"
>
</head>
<body>
<h1>
Spis treści
</h1>
<h1>
Zabawy w H2
</h1>
<form
id=
"init-form"
th:action=
"@{/init}"
>
<button>
Zainicjuj bazę
</button>
</form>
<h2>
Zapytania REST-owe
</h2>
<ul>
<li><a
href=
"#"
th:href=
"@{/}"
>
self
</a></li>
<li><a
th:href=
"@{/rest/customers}"
>
/rest/customers
</a></li>
<li><a
th:href=
"@{/rest/customers/1}"
>
/rest/customers/1
</a></li>
</ul>
</body>
...
...
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