Skip to content

File tree

9 files changed

+195
-3
lines changed

9 files changed

+195
-3
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
services:
2+
db2:
3+
image: icr.io/db2_community/db2
4+
container_name: db2server
5+
hostname: db2server
6+
privileged: true
7+
restart: unless-stopped
8+
ports:
9+
- "50000:50000"
10+
environment:
11+
LICENSE: accept
12+
DB2INST1_PASSWORD: mypassword
13+
DBNAME: testdb
14+
BLU: "false"
15+
ENABLE_ORACLE_COMPATIBILITY: "false"
16+
UPDATEAVAIL: "NO"
17+
TO_CREATE_SAMPLEDB: "false"
18+
volumes:
19+
- db2_data:/database
20+
healthcheck:
21+
test: ["CMD", "su", "-", "db2inst1", "-c", "db2 connect to testdb || exit 1"]
22+
interval: 30s
23+
retries: 5
24+
start_period: 60s
25+
timeout: 10s
26+
27+
volumes:
28+
db2_data:
29+
driver: local

persistence-modules/spring-boot-persistence-5/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@
5151
<scope>test</scope>
5252
</dependency>
5353
<dependency>
54-
<groupId>ch.qos.logback</groupId>
54+
<groupId>com.ibm.db2</groupId>
55+
<artifactId>jcc</artifactId>
56+
<version>${db2.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>ch.qos.logback</groupId>
5560
<artifactId>logback-core</artifactId>
5661
</dependency>
5762
<dependency>
58-
<groupId>ch.qos.logback</groupId>
63+
<groupId>ch.qos.logback</groupId>
5964
<artifactId>logback-classic</artifactId>
6065
</dependency>
6166
</dependencies>
@@ -77,6 +82,7 @@
7782
<hikari.version>6.2.1</hikari.version>
7883
<instancio.version>5.2.1</instancio.version>
7984
<spring-boot.version>3.4.1</spring-boot.version>
85+
<db2.version>12.1.0.0</db2.version>
8086
</properties>
8187

8288
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.db2database;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ArticleApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ArticleApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.db2database.controller;
2+
3+
import com.baeldung.db2database.entity.Article;
4+
import com.baeldung.db2database.repository.ArticleRepository;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RestController;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.util.UriComponentsBuilder;
12+
13+
import java.net.URI;
14+
15+
@RestController
16+
public class ArticleController {
17+
18+
private final ArticleRepository articleRepository ;
19+
20+
public ArticleController(ArticleRepository articleRepository) {
21+
this.articleRepository = articleRepository;
22+
}
23+
24+
25+
@PostMapping("/create-article")
26+
private ResponseEntity<Article> createArticle(@RequestBody Article article, UriComponentsBuilder ucb) {
27+
Article newArticle = new Article();
28+
newArticle.setAuthor(article.getAuthor());
29+
newArticle.setBody(article.getBody());
30+
newArticle.setTitle(article.getTitle());
31+
Article savedArticle = articleRepository.save(newArticle);
32+
URI location = ucb.path("/articles/{id}").buildAndExpand(savedArticle.getId()).toUri();
33+
34+
return ResponseEntity.created(location).body(savedArticle);
35+
}
36+
37+
@GetMapping("/articles/{id}")
38+
public ResponseEntity<Article> getArticleById(@PathVariable Long id) {
39+
return articleRepository.findById(id)
40+
.map(ResponseEntity::ok)
41+
.orElseGet(() -> ResponseEntity.notFound().build());
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.db2database.entity;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
8+
@Entity
9+
public class Article {
10+
@Id
11+
@GeneratedValue(strategy = GenerationType.IDENTITY)
12+
private Long id;
13+
private String title;
14+
private String body;
15+
private String author;
16+
17+
public Long getId() {
18+
return id;
19+
}
20+
21+
public void setId(Long id) {
22+
this.id = id;
23+
}
24+
25+
public String getTitle() {
26+
return title;
27+
}
28+
29+
public void setTitle(String title) {
30+
this.title = title;
31+
}
32+
33+
public String getBody() {
34+
return body;
35+
}
36+
37+
public void setBody(String body) {
38+
this.body = body;
39+
}
40+
41+
public String getAuthor() {
42+
return author;
43+
}
44+
45+
public void setAuthor(String author) {
46+
this.author = author;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.db2database.repository;
2+
3+
import com.baeldung.db2database.entity.Article;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface ArticleRepository extends JpaRepository<Article, Long> {
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring.datasource.url=jdbc:db2://localhost:50000/testdb
2+
spring.datasource.username=db2inst1
3+
spring.datasource.password=mypassword
4+
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
5+
6+
spring.jpa.database-platform=org.hibernate.dialect.DB2Dialect
7+
spring.jpa.hibernate.ddl-auto=update
8+
spring.jpa.show-sql=true

persistence-modules/spring-boot-persistence-5/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ spring.datasource.username=sa
44
spring.datasource.password=password
55
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
66
spring.sql.init.data-locations=classpath:db/data.sql
7-
spring.sql.init.schema-locations=classpath:db/schema.sql
7+
spring.sql.init.schema-locations=classpath:db/schema.sql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.db2database;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.boot.test.web.client.TestRestTemplate;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
import java.net.URI;
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import com.baeldung.db2database.entity.Article;
13+
import org.springframework.test.context.ActiveProfiles;
14+
15+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
16+
@ActiveProfiles("db2")
17+
class ArticleApplicationLiveTest {
18+
19+
@Autowired
20+
TestRestTemplate restTemplate;
21+
22+
23+
@Test
24+
void givenNewArticleObject_whenMakingAPostRequest_thenReturnCreated() {
25+
Article article = new Article();
26+
article.setTitle("Introduction to Java");
27+
article.setAuthor("Baeldung");
28+
article.setBody("Java is a programming language created by James Gosling");
29+
ResponseEntity<Article> createResponse = restTemplate.postForEntity("/create-article", article, Article.class);
30+
assertThat(createResponse.getStatusCode()).isEqualTo(HttpStatus.CREATED);
31+
32+
URI locationOfNewArticle = createResponse.getHeaders().getLocation();
33+
ResponseEntity<String> getResponse = restTemplate.getForEntity(locationOfNewArticle, String.class);
34+
assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
35+
}
36+
}
37+

0 commit comments

Comments
 (0)