From 8cd0741d6e6d5cea454a0e3a19b089e7dbec60a5 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Mon, 21 Sep 2020 23:57:17 -0400 Subject: [PATCH] got postgres working --- pom.xml | 11 ++++- .../com/example/demo/DemoApplication.java | 17 +++++--- src/main/java/com/example/demo/Person.java | 41 +++++++++++++++++++ .../com/example/demo/PersonRepository.java | 12 ++++++ src/main/resources/application.properties | 3 +- 5 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/example/demo/Person.java create mode 100644 src/main/java/com/example/demo/PersonRepository.java diff --git a/pom.xml b/pom.xml index 191718f..e9a91ad 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.3.3.RELEASE + 2.3.4.RELEASE com.example @@ -19,11 +19,20 @@ + + org.springframework.boot + spring-boot-starter-data-jpa + org.springframework.boot spring-boot-starter-web + + org.postgresql + postgresql + runtime + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index 0e9dae6..4d563a5 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -1,23 +1,28 @@ package com.example.demo; -import com.example.demo.Greeting; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.GetMapping; + import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.beans.factory.annotation.Autowired; @SpringBootApplication @RestController public class DemoApplication { + @Autowired + private PersonRepository personRepository; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @GetMapping("/hello") - public Greeting hello() { - return new Greeting(1, "hi"); + public String hello() { + Person n = new Person(); + n.setName("bob"); + n.setEmail("bob@bob.com"); + personRepository.save(n); + return "Saved"; } - } diff --git a/src/main/java/com/example/demo/Person.java b/src/main/java/com/example/demo/Person.java new file mode 100644 index 0000000..85a980b --- /dev/null +++ b/src/main/java/com/example/demo/Person.java @@ -0,0 +1,41 @@ +package com.example.demo; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity // This tells Hibernate to make a table out of this class +public class Person { + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private Integer id; + + private String name; + + private String email; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/src/main/java/com/example/demo/PersonRepository.java b/src/main/java/com/example/demo/PersonRepository.java new file mode 100644 index 0000000..9cd1ed7 --- /dev/null +++ b/src/main/java/com/example/demo/PersonRepository.java @@ -0,0 +1,12 @@ +package com.example.demo; + +import org.springframework.data.repository.CrudRepository; + +import com.example.demo.Person; + +// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository +// CRUD refers Create, Read, Update, Delete + +public interface PersonRepository extends CrudRepository { + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..144429e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,2 @@ - +spring.jpa.hibernate.ddl-auto=update +spring.datasource.url=jdbc:postgresql://localhost:5432/springdemo