Commit 4025637e by Patryk Czarnik

Inicjalizacja bazy jako operacja webowa

parent ead6c5db
package com.example.demo; 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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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 @SpringBootApplication
public class Pc51SpringDataH2Application { public class Pc51SpringDataH2Application {
...@@ -17,43 +10,4 @@ public class Pc51SpringDataH2Application { ...@@ -17,43 +10,4 @@ public class Pc51SpringDataH2Application {
SpringApplication.run(Pc51SpringDataH2Application.class, args); 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("");
};
}
} }
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"));
}
}
package com.example.demo.web; package com.example.demo.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.repo.DBService;
@Controller @Controller
public class RootController { public class RootController {
@Autowired
private DBService dbService;
@RequestMapping("/") @RequestMapping("/")
public String root() { public String root() {
return "index.html"; return "index.html";
} }
@RequestMapping("/init")
public String initDatabase() {
dbService.initDatabase();
return "redirect:/";
}
} }
...@@ -6,9 +6,16 @@ ...@@ -6,9 +6,16 @@
<link rel="stylesheet" type="text/css" th:href="@{/styl.css}" href="../static/styl.css"> <link rel="stylesheet" type="text/css" th:href="@{/styl.css}" href="../static/styl.css">
</head> </head>
<body> <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> <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> </ul>
</body> </body>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment