Skip to content

Commit 0e1bbf5

Browse files
Code sample - 23c with Spring Data JDBC (#314)
* Code sample - 23c with Spring Data JDBC * additional adjustments * fixed indentation * updated to 23ai as requested * updated to 23ai + driver 23.6.0.24.10 as requested, updated to ucp17
1 parent b9834d6 commit 0e1bbf5

File tree

10 files changed

+449
-0
lines changed

10 files changed

+449
-0
lines changed

java/23ai-springboot3-jdbc/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[Spring Data JDBC with the Oracle Database for Java Developers - Getting Started Guide](https://medium.com/oracledevs/spring-data-jdbc-with-the-oracle-database-23c-for-java-developers-getting-started-guide-1c4640fc8d27)

java/23ai-springboot3-jdbc/pom.xml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>3.2.0</version>
10+
<relativePath /> <!-- lookup parent from repository -->
11+
</parent>
12+
<groupId>com.oracle.dev.springdata.jdbc</groupId>
13+
<artifactId>23ai-springboot3-jdbc</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>23ai-springboot3-jdbc</name>
16+
<properties>
17+
<java.version>21</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.oracle.database.jdbc</groupId>
22+
<artifactId>ojdbc11</artifactId>
23+
<version>23.6.0.24.10</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.oracle.database.jdbc</groupId>
27+
<artifactId>ucp17</artifactId>
28+
<version>23.6.0.24.10</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
47+
<!-- Metrics -->
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-actuator</artifactId>
51+
</dependency>
52+
53+
</dependencies>
54+
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-maven-plugin</artifactId>
60+
</plugin>
61+
<plugin>
62+
<artifactId>maven-compiler-plugin</artifactId>
63+
<configuration>
64+
<compilerArgs>
65+
</compilerArgs>
66+
</configuration>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
CREATE USER ORACLE_23C_USER IDENTIFIED BY <YOUR_PASSWORD>;
2+
GRANT DB_DEVELOPER_ROLE TO ORACLE_23C_USER;
3+
GRANT CREATE SESSION TO ORACLE_23C_USER;
4+
GRANT UNLIMITED TABLESPACE TO ORACLE_23C_USER;
5+
6+
CREATE TABLE Employee
7+
(id NUMBER(10) CONSTRAINT pk_employee PRIMARY KEY,
8+
name VARCHAR2(20),
9+
job VARCHAR2(20),
10+
salary NUMBER(10),
11+
commission NUMBER(10));
12+
13+
INSERT INTO Employee VALUES(7369,'JOHN','CLERK',7902,NULL);
14+
INSERT INTO Employee VALUES(7499,'PETER','SALESMAN',7698,300);
15+
INSERT INTO Employee VALUES(7521,'JEFF','SALESMAN',7698,500);
16+
INSERT INTO Employee VALUES(7566,'MARK','MANAGER',7839,NULL);
17+
INSERT INTO Employee VALUES(7654,'MARTIN','SALESMAN',7698,1400);
18+
INSERT INTO Employee VALUES(7698,'ADAM','MANAGER',7839,NULL);
19+
INSERT INTO Employee VALUES(7782,'CLARK','MANAGER',7839,NULL);
20+
INSERT INTO Employee VALUES(7788,'SCOTT','ANALYST',7566,NULL);
21+
INSERT INTO Employee VALUES(7839,'KING','PRESIDENT',NULL);
22+
INSERT INTO Employee VALUES(7844,'TURNER','SALESMAN',0);
23+
INSERT INTO Employee VALUES(7876,'ADAMS','CLERK',7788,NULL);
24+
INSERT INTO Employee VALUES(7900,'JAMES','CLERK',7698,NULL);
25+
INSERT INTO Employee VALUES(7902,'FORD','ANALYST',7566,NULL);
26+
INSERT INTO Employee VALUES(7934,'MILLER','CLERK',7782,NULL);
27+
28+
COMMIT;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.springdata.jdbc;
23+
24+
import java.util.Objects;
25+
26+
import org.springframework.data.annotation.Id;
27+
import org.springframework.data.relational.core.mapping.Table;
28+
29+
@Table
30+
public class Employee {
31+
32+
private @Id Long id;
33+
private String name;
34+
private String job;
35+
private Integer salary;
36+
private Integer commission;
37+
38+
public Employee() {
39+
}
40+
41+
public Employee(Long id, String name, String job, Integer salary,
42+
Integer commission) {
43+
this.id = id;
44+
this.name = name;
45+
this.job = job;
46+
this.salary = salary;
47+
this.commission = commission;
48+
}
49+
50+
public Long getId() {
51+
return id;
52+
}
53+
54+
public void setId(Long id) {
55+
this.id = id;
56+
}
57+
58+
public String getName() {
59+
return name;
60+
}
61+
62+
public void setName(String name) {
63+
this.name = name;
64+
}
65+
66+
public String getJob() {
67+
return job;
68+
}
69+
70+
public void setJob(String job) {
71+
this.job = job;
72+
}
73+
74+
public Integer getSalary() {
75+
return salary;
76+
}
77+
78+
public void setSalary(Integer salary) {
79+
this.salary = salary;
80+
}
81+
82+
public Integer getCommission() {
83+
return commission;
84+
}
85+
86+
public void setCommission(Integer commission) {
87+
this.commission = commission;
88+
}
89+
90+
@Override
91+
public int hashCode() {
92+
return Objects.hash(commission, id, job, name, salary);
93+
}
94+
95+
@Override
96+
public boolean equals(Object obj) {
97+
if (this == obj)
98+
return true;
99+
if (obj == null)
100+
return false;
101+
if (getClass() != obj.getClass())
102+
return false;
103+
Employee other = (Employee) obj;
104+
return Objects.equals(commission, other.commission)
105+
&& Objects.equals(id, other.id) && Objects.equals(job, other.job)
106+
&& Objects.equals(name, other.name)
107+
&& Objects.equals(salary, other.salary);
108+
}
109+
110+
public String toString() {
111+
return String.format("%20s %20s %20s %20s %20s", id, name, job, salary,
112+
commission);
113+
114+
}
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.springdata.jdbc;
23+
24+
import org.springframework.boot.SpringApplication;
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
27+
@SpringBootApplication
28+
public class EmployeeApplication {
29+
30+
public static void main(String[] args) {
31+
SpringApplication.run(EmployeeApplication.class, args);
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.springdata.jdbc;
23+
24+
import org.springframework.web.bind.annotation.DeleteMapping;
25+
import org.springframework.web.bind.annotation.GetMapping;
26+
import org.springframework.web.bind.annotation.PathVariable;
27+
import org.springframework.web.bind.annotation.PostMapping;
28+
import org.springframework.web.bind.annotation.PutMapping;
29+
import org.springframework.web.bind.annotation.RequestBody;
30+
import org.springframework.web.bind.annotation.RestController;
31+
32+
@RestController
33+
class EmployeeController {
34+
35+
private final EmployeeRepository repository;
36+
37+
EmployeeController(EmployeeRepository repository) {
38+
this.repository = repository;
39+
}
40+
41+
@GetMapping("/employees")
42+
Iterable<Employee> all() {
43+
return repository.findAll();
44+
}
45+
46+
@PostMapping("/employees")
47+
Employee newEmployee(@RequestBody Employee newEmployee) {
48+
return repository.save(newEmployee);
49+
}
50+
51+
@GetMapping("/employees/{id}")
52+
Employee one(@PathVariable Long id) {
53+
54+
return repository.findById(id)
55+
.orElseThrow(() -> new EmployeeNotFoundException(id));
56+
}
57+
58+
@PutMapping("/employees/{id}")
59+
Employee replaceEmployee(@RequestBody Employee newEmployee,
60+
@PathVariable Long id) {
61+
62+
return repository.findById(id).map(employee -> {
63+
employee.setName(newEmployee.getName());
64+
employee.setJob(newEmployee.getJob());
65+
return repository.save(employee);
66+
}).orElseGet(() -> {
67+
newEmployee.setId(id);
68+
return repository.save(newEmployee);
69+
});
70+
}
71+
72+
@DeleteMapping("/employees/{id}")
73+
void deleteEmployee(@PathVariable Long id) {
74+
repository.deleteById(id);
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.springdata.jdbc;
23+
24+
import org.springframework.http.HttpStatus;
25+
import org.springframework.web.bind.annotation.ControllerAdvice;
26+
import org.springframework.web.bind.annotation.ExceptionHandler;
27+
import org.springframework.web.bind.annotation.ResponseBody;
28+
import org.springframework.web.bind.annotation.ResponseStatus;
29+
30+
@ControllerAdvice
31+
class EmployeeNotFoundAdvice {
32+
33+
@ResponseBody
34+
@ExceptionHandler(EmployeeNotFoundException.class)
35+
@ResponseStatus(HttpStatus.NOT_FOUND)
36+
String employeeNotFoundHandler(EmployeeNotFoundException ex) {
37+
return ex.getMessage();
38+
}
39+
}

0 commit comments

Comments
 (0)