Skip to content

Commit c872192

Browse files
feat(spring-oracle-example): implement CRUD operations with JdbcClient and initialize database
1 parent 69e8d70 commit c872192

File tree

13 files changed

+406
-40
lines changed

13 files changed

+406
-40
lines changed

spring-oracle-example/compose.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ services:
99
environment:
1010
SPRING_PROFILES_ACTIVE: default
1111
SERVER_PORT: 8080
12-
SPRING_DATASOURCE_URL: jdbc:oracle:thin:@oracle-db:1521/XEPDB1
13-
SPRING_DATASOURCE_USERNAME: TEST_SCHEMA
14-
SPRING_DATASOURCE_PASSWORD: Ag101Pwd123
12+
SPRING_DATASOURCE_URL: jdbc:oracle:thin:@oracle-db:1521
13+
SPRING_DATASOURCE_USERNAME: developer
14+
SPRING_DATASOURCE_PASSWORD: developer
1515
SPRING_DATASOURCE_DRIVER: oracle.jdbc.OracleDriver
16-
SPRING_JPA_HIBERNATE_DDL_AUTO: update
16+
SPRING_JPA_HIBERNATE_DDL_AUTO: none
1717
SPRING_JPA_SHOW_SQL: true
1818
SPRING_HIBERNATE_DIALECT: org.hibernate.dialect.OracleDialect
1919
ports:
@@ -28,4 +28,5 @@ services:
2828
ORACLE_PASSWORD: "Admin123"
2929
shm_size: 1g
3030
volumes:
31-
- ./docker/oracle:/docker-entrypoint-initdb.d
31+
- ./docker/oracle:/docker-entrypoint-initdb.d
32+
- ./src/main/resources/sql:/docker-entrypoint-initdb.d

spring-oracle-example/docker/oracle/init.sql

Lines changed: 0 additions & 5 deletions
This file was deleted.

spring-oracle-example/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<lombok.version>1.18.42</lombok.version>
2424
<oracle.database.version>23.9.0.25.07</oracle.database.version>
2525
<maven.compiler.plugin.version>3.14.1</maven.compiler.plugin.version>
26+
<instancio.version>5.5.1</instancio.version>
2627
</properties>
2728

2829
<dependencies>
@@ -53,6 +54,12 @@
5354
<version>${lombok.version}</version>
5455
</dependency>
5556

57+
<dependency>
58+
<groupId>org.instancio</groupId>
59+
<artifactId>instancio-core</artifactId>
60+
<version>${instancio.version}</version>
61+
</dependency>
62+
5663
<dependency>
5764
<groupId>org.springframework.boot</groupId>
5865
<artifactId>spring-boot-starter-test</artifactId>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.io.example.config;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
6+
import org.springframework.boot.context.properties.ConfigurationProperties;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.context.annotation.Primary;
10+
import org.springframework.jdbc.core.simple.JdbcClient;
11+
12+
@Configuration
13+
public class DatabaseConfig {
14+
15+
@Bean
16+
@Primary
17+
@ConfigurationProperties("spring.datasource")
18+
public DataSourceProperties dataSourceProperties() {
19+
return new DataSourceProperties();
20+
}
21+
22+
@Bean
23+
@Primary
24+
public DataSource dataSource(DataSourceProperties properties) {
25+
return properties.initializeDataSourceBuilder()
26+
.build();
27+
}
28+
29+
@Bean
30+
@Primary
31+
public JdbcClient jdbcClient(DataSource dataSource) {
32+
return JdbcClient.create(dataSource);
33+
}
34+
35+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.io.example.dao;
2+
3+
import com.io.example.dto.JdbcClientCrudExampleDto;
4+
import com.io.example.exception.JdbcClientCrudExampleException;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.jdbc.core.simple.JdbcClient;
7+
import org.springframework.stereotype.Component;
8+
import java.util.List;
9+
10+
@Component
11+
@RequiredArgsConstructor
12+
public class JdbcClientCrudExampleDao {
13+
14+
private final JdbcClient jdbc;
15+
16+
public void create(JdbcClientCrudExampleDto record) {
17+
try {
18+
jdbc.sql("""
19+
INSERT INTO developer.records_example (id, name, value)
20+
VALUES (?, ?, ?)
21+
""")
22+
.param(record.getId())
23+
.param(record.getName())
24+
.param(record.getValue())
25+
.update();
26+
} catch (Exception e) {
27+
throw new JdbcClientCrudExampleException("Failed to insert record: " + record, e);
28+
}
29+
}
30+
31+
public List<JdbcClientCrudExampleDto> findAll() {
32+
try {
33+
return jdbc.sql("""
34+
SELECT id, name, value, created_at
35+
FROM developer.records_example
36+
""")
37+
.query(JdbcClientCrudExampleDto.class)
38+
.list();
39+
} catch (Exception e) {
40+
throw new JdbcClientCrudExampleException("Failed to retrieve all records", e);
41+
}
42+
}
43+
44+
public JdbcClientCrudExampleDto findById(Long id) {
45+
try {
46+
return jdbc.sql("""
47+
SELECT id, name, value, created_at
48+
FROM developer.records_example
49+
WHERE id = ?
50+
""")
51+
.param(id)
52+
.query(JdbcClientCrudExampleDto.class)
53+
.optional()
54+
.orElseThrow(() -> new JdbcClientCrudExampleException("No record found for id = " + id));
55+
} catch (Exception e) {
56+
throw new JdbcClientCrudExampleException("Failed to retrieve record for id = " + id, e);
57+
}
58+
}
59+
60+
public void update(Long id, String name, String value) {
61+
try {
62+
int affected = jdbc.sql("""
63+
UPDATE developer.records_example
64+
SET name = ?, value = ?
65+
WHERE id = ?
66+
""")
67+
.param(name)
68+
.param(value)
69+
.param(id)
70+
.update();
71+
72+
if (affected == 0) {
73+
throw new JdbcClientCrudExampleException("No record found to update for id = " + id);
74+
}
75+
76+
} catch (Exception e) {
77+
throw new JdbcClientCrudExampleException("Failed to update record with id = " + id, e);
78+
}
79+
}
80+
81+
public void delete(Long id) {
82+
try {
83+
int affected = jdbc.sql("""
84+
DELETE FROM developer.records_example
85+
WHERE id = ?
86+
""")
87+
.param(id)
88+
.update();
89+
90+
if (affected == 0) {
91+
throw new JdbcClientCrudExampleException("No record found to delete for id = " + id);
92+
}
93+
94+
} catch (Exception e) {
95+
throw new JdbcClientCrudExampleException("Failed to delete record with id = " + id, e);
96+
}
97+
}
98+
99+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.io.example.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import java.time.LocalDateTime;
8+
9+
@Data
10+
@Builder
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
public class JdbcClientCrudExampleDto {
14+
15+
private Long id;
16+
private String name;
17+
private String value;
18+
private LocalDateTime createdAt;
19+
20+
}

spring-oracle-example/src/main/java/com/io/example/entity/TestEntity.java

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.io.example.exception;
2+
3+
public class JdbcClientCrudExampleException extends RuntimeException {
4+
5+
public JdbcClientCrudExampleException(String message) {
6+
super(message);
7+
}
8+
9+
public JdbcClientCrudExampleException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
12+
13+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.io.example.init;
2+
3+
import com.io.example.dao.JdbcClientCrudExampleDao;
4+
import com.io.example.dto.JdbcClientCrudExampleDto;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.boot.CommandLineRunner;
8+
import org.springframework.stereotype.Component;
9+
10+
@Slf4j
11+
@Component
12+
@RequiredArgsConstructor
13+
public class JdbcClientCrudExampleInit implements CommandLineRunner {
14+
15+
private final JdbcClientCrudExampleDao dao;
16+
17+
@Override
18+
public void run(String... args) {
19+
20+
var entity = JdbcClientCrudExampleDto.builder()
21+
.id(1L)
22+
.name("Test Name")
23+
.value("Some value")
24+
.build();
25+
26+
dao.create(entity);
27+
log.info("Inserted: {}", entity);
28+
29+
JdbcClientCrudExampleDto fetched = dao.findById(1L);
30+
log.info("Fetched: {}", fetched);
31+
32+
fetched.setName("Updated Name");
33+
fetched.setValue("Updated value");
34+
35+
dao.update(fetched.getId(), fetched.getName(), fetched.getValue());
36+
log.info("Updated: {}", fetched);
37+
38+
dao.delete(1L);
39+
log.info("Deleted entity with id 1");
40+
41+
}
42+
43+
}

spring-oracle-example/src/main/resources/application-dev.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ spring:
33
name: spring-oracle-example-dev
44

55
datasource:
6-
url: ${SPRING_DATASOURCE_URL:jdbc:oracle:thin:@localhost:1521/XEPDB1}
7-
username: ${SPRING_DATASOURCE_USERNAME:TEST_SCHEMA}
8-
password: ${SPRING_DATASOURCE_PASSWORD:Ag101Pwd123}
6+
url: ${SPRING_DATASOURCE_URL:jdbc:oracle:thin:@localhost:1521}
7+
username: ${SPRING_DATASOURCE_USERNAME:developer}
8+
password: ${SPRING_DATASOURCE_PASSWORD:developer}
99
driver-class-name: ${SPRING_DATASOURCE_DRIVER:oracle.jdbc.OracleDriver}
1010

1111
jpa:
1212
hibernate:
13-
ddl-auto: ${SPRING_JPA_HIBERNATE_DDL_AUTO:update}
13+
ddl-auto: ${SPRING_JPA_HIBERNATE_DDL_AUTO:none}
1414
show-sql: ${SPRING_JPA_SHOW_SQL:true}
1515
properties:
1616
hibernate:

0 commit comments

Comments
 (0)