Skip to content

Commit 939e864

Browse files
committed
example of converters in Spring Data JDBC
1 parent 71d2a32 commit 939e864

15 files changed

Lines changed: 439 additions & 0 deletions

File tree

build-all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ build_gradle_module() {
3030

3131
chmod +x gradlew
3232

33+
build_gradle_module "spring-data/spring-data-jdbc-converter"
3334
build_gradle_module "solid"
3435
build_gradle_module "spring-boot/data-migration/flyway"
3536
build_gradle_module "reactive"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
HELP.md
2+
.gradle
3+
build/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**
6+
!**/src/test/**
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
17+
### IntelliJ IDEA ###
18+
.idea
19+
*.iws
20+
*.iml
21+
*.ipr
22+
out/
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
31+
### VS Code ###
32+
.vscode/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
plugins {
2+
id 'org.springframework.boot' version '2.2.5.RELEASE'
3+
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
4+
id 'java'
5+
}
6+
7+
group = 'com.example'
8+
version = '0.0.1-SNAPSHOT'
9+
sourceCompatibility = '1.8'
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
dependencies {
16+
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
17+
testImplementation('org.springframework.boot:spring-boot-starter-test') {
18+
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
19+
}
20+
implementation 'org.flywaydb:flyway-core'
21+
compileOnly 'org.projectlombok:lombok'
22+
annotationProcessor 'org.projectlombok:lombok'
23+
runtimeOnly 'com.h2database:h2'
24+
}
25+
26+
test {
27+
useJUnitPlatform()
28+
}
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

spring-data/spring-data-jdbc-converter/gradlew

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spring-data/spring-data-jdbc-converter/gradlew.bat

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'demo'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.reflectoring;
2+
3+
import java.util.Arrays;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.core.convert.converter.Converter;
6+
import org.springframework.data.convert.ReadingConverter;
7+
import org.springframework.data.convert.WritingConverter;
8+
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
9+
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
10+
11+
@Configuration
12+
class ConverterConfiguration extends AbstractJdbcConfiguration {
13+
14+
@Override
15+
public JdbcCustomConversions jdbcCustomConversions() {
16+
return new JdbcCustomConversions(Arrays.asList(
17+
new UserIdReadingConverter(),
18+
new UserIdWritingConverter()
19+
));
20+
}
21+
22+
@ReadingConverter
23+
static class UserIdReadingConverter implements Converter<Long, UserId> {
24+
25+
@Override
26+
public UserId convert(Long source) {
27+
return new UserId(source);
28+
}
29+
}
30+
31+
@WritingConverter
32+
static class UserIdWritingConverter implements Converter<UserId, Long> {
33+
34+
@Override
35+
public Long convert(UserId source) {
36+
return source.getValue();
37+
}
38+
}
39+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.reflectoring;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
6+
7+
@SpringBootApplication
8+
@EnableJdbcRepositories
9+
public class DemoApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(DemoApplication.class, args);
13+
}
14+
15+
}

0 commit comments

Comments
 (0)