Skip to content

Commit

Permalink
JdbcClientを試す
Browse files Browse the repository at this point in the history
  • Loading branch information
backpaper0 committed Jan 20, 2024
1 parent 8145612 commit 5b0d9a6
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 0 deletions.
36 changes: 36 additions & 0 deletions jdbc-client-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

*.log

39 changes: 39 additions & 0 deletions jdbc-client-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>spring-boot-sandbox-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>jdbc-client-example</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
12 changes: 12 additions & 0 deletions jdbc-client-example/src/main/java/com/example/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.entity;

public record CudExample(Integer id, String textContent) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.entity;

public record RExample(Integer id, String textContent) {
}
Empty file.
3 changes: 3 additions & 0 deletions jdbc-client-example/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
insert into r_example (text_content) values ('foo'), ('bar'), ('baz'), ('qux');

insert into cud_example (text_content) values ('foo'), ('bar');
9 changes: 9 additions & 0 deletions jdbc-client-example/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
create table r_example (
id identity,
text_content varchar
);

create table cud_example (
id identity,
text_content varchar
);
92 changes: 92 additions & 0 deletions jdbc-client-example/src/test/java/com/example/JdbcClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.example;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.example.entity.CudExample;
import com.example.entity.RExample;

@SpringBootTest
public class JdbcClientTest {

@Autowired
JdbcClient jdbc;

@Test
void findOne() {
RExample actual = jdbc.sql("select * from r_example where id = ?")
.param(2)
.query(RExample.class)
.optional()
.get();
assertEquals(new RExample(2, "bar"), actual);
}

@Test
void findList() {
List<RExample> actual = jdbc.sql("select * from r_example where id in (:id1, :id2) order by id asc")
.param("id1", 2)
.param("id2", 3)
.query(RExample.class)
.list();
List<RExample> expected = List.of(new RExample(2, "bar"), new RExample(3, "baz"));
assertEquals(expected, actual);
}

@Test
void insert() {
KeyHolder keyHolder = new GeneratedKeyHolder();
int actual1 = jdbc.sql("insert into cud_example (text_content) values (:textContent)")
.param("textContent", "baz")
.update(keyHolder);
assertEquals(1, actual1);
Number key = keyHolder.getKey();
assertNotNull(key);

CudExample actual2 = jdbc.sql("select * from cud_example where id = ?")
.param(key)
.query(CudExample.class)
.single();
assertEquals(new CudExample(key.intValue(), "baz"), actual2);
}

@Test
void update() {
int actual1 = jdbc.sql("update cud_example set text_content = :textContent where id = :id")
.param("textContent", "bxx")
.param("id", 2)
.update();
assertEquals(1, actual1);

CudExample actual2 = jdbc.sql("select * from cud_example where id = ?")
.param(2)
.query(CudExample.class)
.single();
assertEquals(new CudExample(2, "bxx"), actual2);
}

@Test
void delete() {
int actual1 = jdbc.sql("delete cud_example where id = ?")
.param(1)
.update();
assertEquals(1, actual1);

Optional<CudExample> actual2 = jdbc.sql("select * from cud_example where id = ?")
.param(1)
.query(CudExample.class)
.optional();
assertTrue(actual2.isEmpty());
}
}

0 comments on commit 5b0d9a6

Please sign in to comment.