From ddcbf6f7bda91a02e27d88043d25615282b4eac2 Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Thu, 12 Mar 2020 14:16:55 +0100 Subject: [PATCH] Add connection between spring boot server and postgresql database Signed-off-by: Guillaume Lours --- samples/spring-postgres/backend/pom.xml | 9 +++ .../project/controllers/HomeController.java | 8 +++ .../com/company/project/entity/Greeting.java | 57 +++++++++++++++++++ .../repository/GreetingRepository.java | 9 +++ .../src/main/resources/application.properties | 10 ++++ .../backend/src/main/resources/data.sql | 1 + .../backend/src/main/resources/schema.sql | 4 ++ .../src/main/resources/templates/home.ftlh | 2 +- samples/spring-postgres/docker-compose.yaml | 8 +++ 9 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 samples/spring-postgres/backend/src/main/java/com/company/project/entity/Greeting.java create mode 100644 samples/spring-postgres/backend/src/main/java/com/company/project/repository/GreetingRepository.java create mode 100644 samples/spring-postgres/backend/src/main/resources/data.sql create mode 100644 samples/spring-postgres/backend/src/main/resources/schema.sql diff --git a/samples/spring-postgres/backend/pom.xml b/samples/spring-postgres/backend/pom.xml index 99faf25..b0471a1 100755 --- a/samples/spring-postgres/backend/pom.xml +++ b/samples/spring-postgres/backend/pom.xml @@ -37,6 +37,15 @@ org.springframework.boot spring-boot-starter-jersey + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.postgresql + postgresql + runtime + org.springframework.session spring-session-core diff --git a/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java b/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java index a683b50..c23d706 100755 --- a/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java +++ b/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java @@ -1,5 +1,8 @@ package com.company.project.controllers; +import com.company.project.entity.Greeting; +import com.company.project.repository.GreetingRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -7,8 +10,13 @@ import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { + @Autowired + private GreetingRepository repository; + @GetMapping("/") public String showHome(String name, Model model) { + Greeting dockerGreeting = repository.findById(1).orElse(new Greeting("Not Found 😕")); + model = model.addAttribute("name", dockerGreeting.getName()); return "home"; } diff --git a/samples/spring-postgres/backend/src/main/java/com/company/project/entity/Greeting.java b/samples/spring-postgres/backend/src/main/java/com/company/project/entity/Greeting.java new file mode 100644 index 0000000..67c1079 --- /dev/null +++ b/samples/spring-postgres/backend/src/main/java/com/company/project/entity/Greeting.java @@ -0,0 +1,57 @@ +package com.company.project.entity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "GREETINGS") +public class Greeting { + + @Id + private int id; + private String name; + + public Greeting() { + } + + public Greeting(String name) { + this.name = name; + } + + public Greeting(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Greeting greeting = (Greeting) o; + + return name.equals(greeting.name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } +} diff --git a/samples/spring-postgres/backend/src/main/java/com/company/project/repository/GreetingRepository.java b/samples/spring-postgres/backend/src/main/java/com/company/project/repository/GreetingRepository.java new file mode 100644 index 0000000..8927337 --- /dev/null +++ b/samples/spring-postgres/backend/src/main/java/com/company/project/repository/GreetingRepository.java @@ -0,0 +1,9 @@ +package com.company.project.repository; + +import com.company.project.entity.Greeting; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface GreetingRepository extends CrudRepository { +} diff --git a/samples/spring-postgres/backend/src/main/resources/application.properties b/samples/spring-postgres/backend/src/main/resources/application.properties index 8b13789..e911c04 100755 --- a/samples/spring-postgres/backend/src/main/resources/application.properties +++ b/samples/spring-postgres/backend/src/main/resources/application.properties @@ -1 +1,11 @@ +spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect +spring.jpa.hibernate.ddl-auto=none +spring.jpa.hibernate.show-sql=true +spring.datasource.url=jdbc:postgresql://db:5432/${POSTGRES_DB} +spring.datasource.username=postgres +spring.datasource.password=${POSTGRES_PASSWORD:db-wrz2z} +spring.datasource.initialization-mode=always +spring.datasource.initialize=true +spring.datasource.schema=classpath:/schema.sql +spring.datasource.continue-on-error=true diff --git a/samples/spring-postgres/backend/src/main/resources/data.sql b/samples/spring-postgres/backend/src/main/resources/data.sql new file mode 100644 index 0000000..122079f --- /dev/null +++ b/samples/spring-postgres/backend/src/main/resources/data.sql @@ -0,0 +1 @@ +INSERT INTO GREETINGS(name) values ('Docker'); diff --git a/samples/spring-postgres/backend/src/main/resources/schema.sql b/samples/spring-postgres/backend/src/main/resources/schema.sql new file mode 100644 index 0000000..bd40371 --- /dev/null +++ b/samples/spring-postgres/backend/src/main/resources/schema.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS GREETINGS ( + id serial PRIMARY KEY, + name varchar(50) NOT NULL +); diff --git a/samples/spring-postgres/backend/src/main/resources/templates/home.ftlh b/samples/spring-postgres/backend/src/main/resources/templates/home.ftlh index daba182..42d0c29 100755 --- a/samples/spring-postgres/backend/src/main/resources/templates/home.ftlh +++ b/samples/spring-postgres/backend/src/main/resources/templates/home.ftlh @@ -5,5 +5,5 @@ -

Hello from Docker!

+

Hello from ${name}!

diff --git a/samples/spring-postgres/docker-compose.yaml b/samples/spring-postgres/docker-compose.yaml index 307a6a7..d6cb620 100644 --- a/samples/spring-postgres/docker-compose.yaml +++ b/samples/spring-postgres/docker-compose.yaml @@ -4,6 +4,10 @@ services: build: backend ports: - 80:8080 + environment: + POSTGRES_DB: example + networks: + - spring-postges db: environment: POSTGRES_DB: example @@ -14,8 +18,12 @@ services: - db-password volumes: - db-data:/var/lib/postgresql/data + networks: + - spring-postges volumes: db-data: {} secrets: db-password: file: db/password.txt +networks: + spring-postges: {}