Add connection between spring boot server and postgresql database

Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
master
Guillaume Lours 5 years ago
parent 7d4d65c780
commit ddcbf6f7bd

@ -37,6 +37,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>

@ -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";
}

@ -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();
}
}

@ -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<Greeting, Integer> {
}

@ -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

@ -0,0 +1 @@
INSERT INTO GREETINGS(name) values ('Docker');

@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS GREETINGS (
id serial PRIMARY KEY,
name varchar(50) NOT NULL
);

@ -5,5 +5,5 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Hello from Docker!</p>
<p>Hello from ${name}!</p>
</body>

@ -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: {}

Loading…
Cancel
Save