******************************************* JPA ******************************************* ------------------------------------------ JpaApplication.java ------------------------------------------ package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAutoConfiguration public class JpaApplication { public static void main(String[] args) { SpringApplication.run(JpaApplication.class, args); } } ------------------------------------------ DevConfiguration.java ------------------------------------------ package com.example; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.example.bootstrap.DatabaseBootstrap; @Configuration public class DevConfiguration { @Bean public DatabaseBootstrap databaseBootstrap() { return new DatabaseBootstrap(); } } ------------------------------------------ DatabaseBootstrap.java ------------------------------------------ package com.example.bootstrap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import com.example.model.Customer; import com.example.repository.CustomerRepository; public class DatabaseBootstrap implements InitializingBean { @Autowired CustomerRepository repository; private static Logger log = LoggerFactory.getLogger(DatabaseBootstrap.class); @Override public void afterPropertiesSet() throws Exception { if (repository.findByFirstnameAndLastname("Dirk", "Schocke") == null) { Customer customer = new Customer(); customer.setFirstname("Dirk"); customer.setLastname("Schocke"); repository.save(customer); log.info("Dirk Schocke created"); } log.info("Bootstrap finished"); } } ------------------------------------------ CustomerController.java ------------------------------------------ package com.example.controller; 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.RequestMethod; import org.springframework.web.servlet.ModelAndView; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; import com.example.model.Customer; import com.example.repository.CustomerRepository; @Controller public class CustomerController { @Autowired CustomerRepository repository; @RequestMapping(value = "/customers", method = RequestMethod.GET) public ModelAndView getCustomer() { return new ModelAndView("customers") .addObject("customers", repository.findAll()) .addObject("createLink", linkTo(methodOn(CustomerController.class).createCustomers(null, null)).withRel("Create")); // wg.
} @RequestMapping(value = "/customers/create", method = RequestMethod.POST) public ModelAndView createCustomers(String firstname, String lastname) { Customer customer = new Customer(); customer.setFirstname(firstname); customer.setLastname(lastname); repository.save(customer); return new ModelAndView("created-customer.html") .addObject("overview", linkTo(methodOn(CustomerController.class).getCustomer()).withRel("Overview")); } } ------------------------------------------ Customer.java ------------------------------------------ package com.example.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Customer { @Id @GeneratedValue private Long id; private String firstname; private String lastname; public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } } ------------------------------------------ Checkout.java ------------------------------------------ package com.example.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Checkout { @Id @GeneratedValue private Long id; @OneToOne private Customer customer; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } } ------------------------------------------ CustomerRepository.java ------------------------------------------ package com.example.repository; import org.springframework.data.repository.CrudRepository; import com.example.model.Customer; public interface CustomerRepository extends CrudRepository{ // Hibernate generiert automatisch eine SQL-Abfrage public Customer findByFirstnameAndLastname(String firstname, String lastname); } ------------------------------------------ CheckoutRepository.java ------------------------------------------ package com.example.repository; import org.springframework.data.repository.CrudRepository; import com.example.model.Checkout; public interface CheckoutRepository extends CrudRepository { } ------------------------------------------ customers.html ------------------------------------------

Customers

Create customer

------------------------------------------ created-customer.html ------------------------------------------ Customer created
Back to overview ------------------------------------------ mobile / customers.html ------------------------------------------
Firstname Lastname
------------------------------------------ application.properties ------------------------------------------ spring.mobile.devicedelegatingviewresolver.enabled=true spring.security.user.name=admin spring.security.user.password=admin ------------------------------------------ pom.xml ------------------------------------------ 4.0.0 com.example jpa 0.0.1-SNAPSHOT jar jpa Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE UTF-8 UTF-8 10 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-data-rest org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-mobile 1.5.15.RELEASE org.springframework.boot spring-boot-starter-security com.h2database h2 runtime org.thymeleaf thymeleaf-spring5 compile org.webjars jquery-mobile 1.4.5 compile org.webjars jquery 2.1.1 compile org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin *********************************** Thymeleaf *********************************** // customers.html Server: @RequestMapping(value = "/customers", method = RequestMethod.GET) public ModelAndView getCustomer() { return new ModelAndView("customers") .addObject("customers", repository.findAll()) .addObject("createLink", linkTo(methodOn(CustomerController.class).createCustomers(null, null)).withRel("Create")); // wg.
} HTML:

Customers

Create customer

// created-customer.html Server: @RequestMapping(value = "/customers/create", method = RequestMethod.POST) public ModelAndView createCustomers(String firstname, String lastname) { Customer customer = new Customer(); customer.setFirstname(firstname); customer.setLastname(lastname); repository.save(customer); return new ModelAndView("created-customer.html") .addObject("overview", linkTo(methodOn(CustomerController.class).getCustomer()).withRel("Overview")); } HTML: Customer created
Back to overview