-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8145612
commit 5b0d9a6
Showing
9 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
jdbc-client-example/src/main/java/com/example/entity/CudExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
4 changes: 4 additions & 0 deletions
4
jdbc-client-example/src/main/java/com/example/entity/RExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
92
jdbc-client-example/src/test/java/com/example/JdbcClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |