Reflect grouping in directory structure
This commit is contained in:
1
framework/jdbc-postgresql/README.adoc
Normal file
1
framework/jdbc-postgresql/README.adoc
Normal file
@@ -0,0 +1 @@
|
||||
Tests if JDBC with PostgreSQL is working
|
||||
17
framework/jdbc-postgresql/build.gradle
Normal file
17
framework/jdbc-postgresql/build.gradle
Normal file
@@ -0,0 +1,17 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot'
|
||||
id 'org.springframework.aot.smoke-test'
|
||||
id 'org.graalvm.buildtools.native'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
|
||||
implementation("org.springframework.boot:spring-boot-starter-jdbc")
|
||||
runtimeOnly("org.postgresql:postgresql")
|
||||
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
|
||||
appTestImplementation(project(":aot-smoke-test-support"))
|
||||
appTestImplementation("org.awaitility:awaitility:4.2.0")
|
||||
}
|
||||
8
framework/jdbc-postgresql/docker-compose.yml
Normal file
8
framework/jdbc-postgresql/docker-compose.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
version: '3.1'
|
||||
services:
|
||||
postgres:
|
||||
image: 'postgres:14'
|
||||
environment:
|
||||
- 'POSTGRES_PASSWORD=password'
|
||||
ports:
|
||||
- '5432'
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.jdbc.postgresql;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.awaitility.Awaitility;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.smoketest.support.assertj.AssertableOutput;
|
||||
import org.springframework.aot.smoketest.support.junit.ApplicationTest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ApplicationTest
|
||||
class JdbcPostgreSQLApplicationAotTests {
|
||||
|
||||
@Test
|
||||
void authorsCanBeQueried(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(
|
||||
() -> assertThat(output).hasSingleLineContaining("Found author: Author[id=1, name=mbhave]")
|
||||
.hasSingleLineContaining("Found author: Author[id=2, name=snicoll]")
|
||||
.hasSingleLineContaining("Found author: Author[id=3, name=wilkinsona]"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.example.jdbc.postgresql;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
record Author(long id, String name) {
|
||||
enum Mapper implements RowMapper<Author> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Author mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return new Author(rs.getInt("id"), rs.getString("name"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.example.jdbc.postgresql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CLR implements CommandLineRunner {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public CLR(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
insertAuthors();
|
||||
queryAuthors();
|
||||
}
|
||||
|
||||
private void queryAuthors() {
|
||||
List<Author> authors = jdbcTemplate.query("SELECT * FROM authors", Author.Mapper.INSTANCE);
|
||||
for (Author author : authors) {
|
||||
System.out.printf("Found author: %s%n", author);
|
||||
}
|
||||
}
|
||||
|
||||
private void insertAuthors() {
|
||||
List<String> authors = List.of("mbhave", "snicoll", "wilkinsona");
|
||||
|
||||
for (String author : authors) {
|
||||
this.jdbcTemplate.update("INSERT INTO authors (name) VALUES (?)", author);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.jdbc.postgresql;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class JdbcPostgreSQLApplication {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
SpringApplication.run(JdbcPostgreSQLApplication.class, args);
|
||||
Thread.currentThread().join(); // To be able to measure memory consumption
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
spring.sql.init.mode=always
|
||||
spring.datasource.url=jdbc:postgresql://${POSTGRES_HOST:localhost}:${POSTGRES_PORT_5432:5432}/postgres
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=password
|
||||
5
framework/jdbc-postgresql/src/main/resources/schema.sql
Normal file
5
framework/jdbc-postgresql/src/main/resources/schema.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
CREATE TABLE authors
|
||||
(
|
||||
id integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
||||
name varchar NOT NULL
|
||||
);
|
||||
Reference in New Issue
Block a user