-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
b9834d6
commit 0e1bbf5
Showing
10 changed files
with
449 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 @@ | ||
[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) |
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,71 @@ | ||
<?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>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>3.2.0</version> | ||
<relativePath /> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.oracle.dev.springdata.jdbc</groupId> | ||
<artifactId>23ai-springboot3-jdbc</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>23ai-springboot3-jdbc</name> | ||
<properties> | ||
<java.version>21</java.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.oracle.database.jdbc</groupId> | ||
<artifactId>ojdbc11</artifactId> | ||
<version>23.6.0.24.10</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.oracle.database.jdbc</groupId> | ||
<artifactId>ucp17</artifactId> | ||
<version>23.6.0.24.10</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jdbc</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Metrics --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<compilerArgs> | ||
</compilerArgs> | ||
</configuration> | ||
</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,28 @@ | ||
CREATE USER ORACLE_23C_USER IDENTIFIED BY <YOUR_PASSWORD>; | ||
GRANT DB_DEVELOPER_ROLE TO ORACLE_23C_USER; | ||
GRANT CREATE SESSION TO ORACLE_23C_USER; | ||
GRANT UNLIMITED TABLESPACE TO ORACLE_23C_USER; | ||
|
||
CREATE TABLE Employee | ||
(id NUMBER(10) CONSTRAINT pk_employee PRIMARY KEY, | ||
name VARCHAR2(20), | ||
job VARCHAR2(20), | ||
salary NUMBER(10), | ||
commission NUMBER(10)); | ||
|
||
INSERT INTO Employee VALUES(7369,'JOHN','CLERK',7902,NULL); | ||
INSERT INTO Employee VALUES(7499,'PETER','SALESMAN',7698,300); | ||
INSERT INTO Employee VALUES(7521,'JEFF','SALESMAN',7698,500); | ||
INSERT INTO Employee VALUES(7566,'MARK','MANAGER',7839,NULL); | ||
INSERT INTO Employee VALUES(7654,'MARTIN','SALESMAN',7698,1400); | ||
INSERT INTO Employee VALUES(7698,'ADAM','MANAGER',7839,NULL); | ||
INSERT INTO Employee VALUES(7782,'CLARK','MANAGER',7839,NULL); | ||
INSERT INTO Employee VALUES(7788,'SCOTT','ANALYST',7566,NULL); | ||
INSERT INTO Employee VALUES(7839,'KING','PRESIDENT',NULL); | ||
INSERT INTO Employee VALUES(7844,'TURNER','SALESMAN',0); | ||
INSERT INTO Employee VALUES(7876,'ADAMS','CLERK',7788,NULL); | ||
INSERT INTO Employee VALUES(7900,'JAMES','CLERK',7698,NULL); | ||
INSERT INTO Employee VALUES(7902,'FORD','ANALYST',7566,NULL); | ||
INSERT INTO Employee VALUES(7934,'MILLER','CLERK',7782,NULL); | ||
|
||
COMMIT; |
115 changes: 115 additions & 0 deletions
115
java/23ai-springboot3-jdbc/src/main/java/com/oracle/dev/springdata/jdbc/Employee.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,115 @@ | ||
/* | ||
Copyright (c) 2024, Oracle and/or its affiliates. | ||
This software is dual-licensed to you under the Universal Permissive License | ||
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License | ||
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose | ||
either license. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.oracle.dev.springdata.jdbc; | ||
|
||
import java.util.Objects; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
@Table | ||
public class Employee { | ||
|
||
private @Id Long id; | ||
private String name; | ||
private String job; | ||
private Integer salary; | ||
private Integer commission; | ||
|
||
public Employee() { | ||
} | ||
|
||
public Employee(Long id, String name, String job, Integer salary, | ||
Integer commission) { | ||
this.id = id; | ||
this.name = name; | ||
this.job = job; | ||
this.salary = salary; | ||
this.commission = commission; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getJob() { | ||
return job; | ||
} | ||
|
||
public void setJob(String job) { | ||
this.job = job; | ||
} | ||
|
||
public Integer getSalary() { | ||
return salary; | ||
} | ||
|
||
public void setSalary(Integer salary) { | ||
this.salary = salary; | ||
} | ||
|
||
public Integer getCommission() { | ||
return commission; | ||
} | ||
|
||
public void setCommission(Integer commission) { | ||
this.commission = commission; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(commission, id, job, name, salary); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
Employee other = (Employee) obj; | ||
return Objects.equals(commission, other.commission) | ||
&& Objects.equals(id, other.id) && Objects.equals(job, other.job) | ||
&& Objects.equals(name, other.name) | ||
&& Objects.equals(salary, other.salary); | ||
} | ||
|
||
public String toString() { | ||
return String.format("%20s %20s %20s %20s %20s", id, name, job, salary, | ||
commission); | ||
|
||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ai-springboot3-jdbc/src/main/java/com/oracle/dev/springdata/jdbc/EmployeeApplication.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,34 @@ | ||
/* | ||
Copyright (c) 2024, Oracle and/or its affiliates. | ||
This software is dual-licensed to you under the Universal Permissive License | ||
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License | ||
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose | ||
either license. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.oracle.dev.springdata.jdbc; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class EmployeeApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(EmployeeApplication.class, args); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...3ai-springboot3-jdbc/src/main/java/com/oracle/dev/springdata/jdbc/EmployeeController.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,76 @@ | ||
/* | ||
Copyright (c) 2024, Oracle and/or its affiliates. | ||
This software is dual-licensed to you under the Universal Permissive License | ||
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License | ||
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose | ||
either license. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.oracle.dev.springdata.jdbc; | ||
|
||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
class EmployeeController { | ||
|
||
private final EmployeeRepository repository; | ||
|
||
EmployeeController(EmployeeRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
@GetMapping("/employees") | ||
Iterable<Employee> all() { | ||
return repository.findAll(); | ||
} | ||
|
||
@PostMapping("/employees") | ||
Employee newEmployee(@RequestBody Employee newEmployee) { | ||
return repository.save(newEmployee); | ||
} | ||
|
||
@GetMapping("/employees/{id}") | ||
Employee one(@PathVariable Long id) { | ||
|
||
return repository.findById(id) | ||
.orElseThrow(() -> new EmployeeNotFoundException(id)); | ||
} | ||
|
||
@PutMapping("/employees/{id}") | ||
Employee replaceEmployee(@RequestBody Employee newEmployee, | ||
@PathVariable Long id) { | ||
|
||
return repository.findById(id).map(employee -> { | ||
employee.setName(newEmployee.getName()); | ||
employee.setJob(newEmployee.getJob()); | ||
return repository.save(employee); | ||
}).orElseGet(() -> { | ||
newEmployee.setId(id); | ||
return repository.save(newEmployee); | ||
}); | ||
} | ||
|
||
@DeleteMapping("/employees/{id}") | ||
void deleteEmployee(@PathVariable Long id) { | ||
repository.deleteById(id); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...springboot3-jdbc/src/main/java/com/oracle/dev/springdata/jdbc/EmployeeNotFoundAdvice.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,39 @@ | ||
/* | ||
Copyright (c) 2024, Oracle and/or its affiliates. | ||
This software is dual-licensed to you under the Universal Permissive License | ||
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License | ||
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose | ||
either license. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.oracle.dev.springdata.jdbc; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ControllerAdvice | ||
class EmployeeNotFoundAdvice { | ||
|
||
@ResponseBody | ||
@ExceptionHandler(EmployeeNotFoundException.class) | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
String employeeNotFoundHandler(EmployeeNotFoundException ex) { | ||
return ex.getMessage(); | ||
} | ||
} |
Oops, something went wrong.