Skip to content

Commit c37942e

Browse files
committed
Initial
1 parent 4ee5820 commit c37942e

17 files changed

+308
-0
lines changed

jdbi/.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/gradlew text eol=lf
2+
*.bat text eol=crlf
3+
*.jar binary

jdbi/.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
HELP.md
2+
.gradle
3+
build/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**/build/
6+
!**/src/test/**/build/
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
bin/
17+
!**/src/main/**/bin/
18+
!**/src/test/**/bin/
19+
20+
### IntelliJ IDEA ###
21+
.idea
22+
*.iws
23+
*.iml
24+
*.ipr
25+
out/
26+
!**/src/main/**/out/
27+
!**/src/test/**/out/
28+
29+
### NetBeans ###
30+
/nbproject/private/
31+
/nbbuild/
32+
/dist/
33+
/nbdist/
34+
/.nb-gradle/
35+
36+
### VS Code ###
37+
.vscode/

jdbi/build.gradle.kts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
plugins {
2+
java
3+
id("org.springframework.boot") version "3.5.6"
4+
id("io.spring.dependency-management") version "1.1.7"
5+
}
6+
7+
group = "zin.rashidi.boot"
8+
version = "0.0.1-SNAPSHOT"
9+
description = "jdbi"
10+
11+
java {
12+
toolchain {
13+
languageVersion = JavaLanguageVersion.of(25)
14+
}
15+
}
16+
17+
repositories {
18+
mavenCentral()
19+
}
20+
21+
dependencies {
22+
implementation(platform("org.jdbi:jdbi3-bom:3.49.6"))
23+
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
24+
implementation("org.jdbi:jdbi3-postgres")
25+
implementation("org.jdbi:jdbi3-spring")
26+
runtimeOnly("org.postgresql:postgresql")
27+
testImplementation("org.springframework.boot:spring-boot-starter-test")
28+
testImplementation("org.springframework.boot:spring-boot-testcontainers")
29+
testImplementation("org.testcontainers:junit-jupiter")
30+
testImplementation("org.testcontainers:postgresql")
31+
testImplementation("org.jdbi:jdbi3-testcontainers")
32+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
33+
}
34+
35+
tasks.withType<Test> {
36+
useJUnitPlatform()
37+
}

jdbi/settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = "jdbi"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package zin.rashidi.boot.jdbi;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class JdbiApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(JdbiApplication.class, args);
11+
}
12+
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package zin.rashidi.boot.jdbi.book;
2+
3+
/**
4+
* @author Rashidi Zin
5+
*/
6+
record Author(Long id, String name) {
7+
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package zin.rashidi.boot.jdbi.book;
2+
3+
import org.jdbi.v3.spring.JdbiRepository;
4+
import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
5+
import org.jdbi.v3.sqlobject.customizer.Bind;
6+
import org.jdbi.v3.sqlobject.statement.SqlQuery;
7+
8+
import java.util.Optional;
9+
import java.util.stream.Stream;
10+
11+
/**
12+
* @author Rashidi Zin
13+
*/
14+
@JdbiRepository
15+
@RegisterRowMapper(AuthorRowMapper.class)
16+
interface AuthorReadOnlyRepository {
17+
18+
@SqlQuery("SELECT * FROM author")
19+
Stream<Author> findAll();
20+
21+
@SqlQuery("SELECT * FROM author WHERE id = :id")
22+
Optional<Author> findById(@Bind("id") Long id);
23+
24+
@SqlQuery("SELECT * FROM author WHERE name = :name")
25+
Stream<Author> findByName(@Bind("name") String name);
26+
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package zin.rashidi.boot.jdbi.book;
2+
3+
import org.jdbi.v3.core.mapper.RowMapper;
4+
import org.jdbi.v3.core.statement.StatementContext;
5+
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
9+
/**
10+
* @author Rashidi Zin
11+
*/
12+
public class AuthorRowMapper implements RowMapper<Author> {
13+
14+
@Override
15+
public Author map(ResultSet rs, StatementContext ctx) throws SQLException {
16+
return new Author(rs.getLong("id"), rs.getString("name"));
17+
}
18+
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package zin.rashidi.boot.jdbi.book;
2+
3+
/**
4+
* @author Rashidi Zin
5+
*/
6+
class Book {
7+
8+
private final String isbn;
9+
private final String title;
10+
private final Author author;
11+
12+
Book(String isbn, String title, Author author) {
13+
this.isbn = isbn;
14+
this.title = title;
15+
this.author = author;
16+
}
17+
18+
String isbn() {
19+
return isbn;
20+
}
21+
22+
String title() {
23+
return title;
24+
}
25+
26+
Author author() {
27+
return author;
28+
}
29+
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package zin.rashidi.boot.jdbi.configuration;
2+
3+
import org.jdbi.v3.core.Jdbi;
4+
import org.jdbi.v3.postgres.PostgresPlugin;
5+
import org.jdbi.v3.spring.EnableJdbiRepositories;
6+
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
import javax.sql.DataSource;
11+
12+
/**
13+
* @author Rashidi Zin
14+
*/
15+
@Configuration
16+
@EnableJdbiRepositories(basePackages = "zin.rashidi.boot.jdbi")
17+
public class RepositoryConfiguration {
18+
19+
@Bean
20+
public Jdbi jdbi(DataSource dataSource) {
21+
return Jdbi.create(dataSource)
22+
.installPlugin(new PostgresPlugin())
23+
.installPlugin(new SqlObjectPlugin());
24+
}
25+
26+
}

0 commit comments

Comments
 (0)