-
Notifications
You must be signed in to change notification settings - Fork 827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code Sample for spring_jdbc Getting started #198
Open
peterrsongg
wants to merge
4
commits into
oracle-samples:main
Choose a base branch
from
peterrsongg:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
87bfc6d
Code Sample for spring_jdbc Getting started
peterrsongg 191e433
Made changes to readme and student service class
peterrsongg adb43f8
Update odbjc to 21.5, add ucp logging to application.properties
peterrsongg f159b6d
Merge remote-tracking branch 'upstream/main'
peterrsongg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,14 @@ | ||
# SpringBoot JDBC Code Sample | ||
|
||
This is the code sample that accompanies the "Getting Started with SpringBoot & JDBC" | ||
video. This showcases using JDBC with UCP in a simple application, utilizing JPA to | ||
retrieve the data from the Autonomous Database. | ||
|
||
# Main Components of the Code | ||
|
||
* TestController.java: TestController.java is the RestController that maps the public endpoints to the students table. In this case, it maps '/students' to a list of all students. | ||
* Student.java: Models the Student class to the Students table in the database. | ||
* StudentRepository.java: Extends JpaRepository to utilize built-in queries. | ||
* StudentService.java: Implements one method that retrieves all students. | ||
* application.properties: Provides database connection information along with UCP connection information | ||
|
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,50 @@ | ||
<?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>2.6.7</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.example</groupId> | ||
<artifactId>demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
<properties> | ||
<java.version>11</java.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.oracle.database.jdbc</groupId> | ||
<artifactId>ojdbc11-production</artifactId> | ||
<version>21.5.0.0</version> | ||
<type>pom</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
24 changes: 24 additions & 0 deletions
24
java/spring_jdbc_example/src/main/java/com/example/demo/DemoApplication.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,24 @@ | ||
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
Licensed under the Universal Permissive License v 1.0 | ||
as shown at http://oss.oracle.com/licenses/upl */ | ||
|
||
/* | ||
DESCRIPTION | ||
This is the main class that runs the springboot application | ||
|
||
Peter Song 05/11/2022 - Creation | ||
*/ | ||
|
||
package com.example.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class DemoApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DemoApplication.class, args); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
java/spring_jdbc_example/src/main/java/com/example/demo/Service/StudentService.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,31 @@ | ||
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
Licensed under the Universal Permissive License v 1.0 | ||
as shown at http://oss.oracle.com/licenses/upl */ | ||
|
||
/* | ||
DESCRIPTION | ||
The StudentService class uses a StudentRepository object to retrieve a | ||
list of all students | ||
|
||
Peter Song 05/11/2022 - Creation | ||
*/ | ||
|
||
package com.example.demo.Service; | ||
|
||
|
||
import com.example.demo.model.Student; | ||
import com.example.demo.repository.StudentRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class StudentService { | ||
@Autowired | ||
private StudentRepository studentRepository; | ||
public List<Student> findAll(){ | ||
List<Student> students = studentRepository.findAll(); | ||
return students; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
java/spring_jdbc_example/src/main/java/com/example/demo/controller/TestController.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,33 @@ | ||
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
Licensed under the Universal Permissive License v 1.0 | ||
as shown at http://oss.oracle.com/licenses/upl */ | ||
|
||
/* | ||
DESCRIPTION | ||
This code sample calls upon a rest endpoint to retrieve all students. | ||
It is part of the greater | ||
|
||
peter song 05/11/2022 - Creation | ||
*/ | ||
|
||
package com.example.demo.controller; | ||
|
||
|
||
import com.example.demo.Service.StudentService; | ||
import com.example.demo.model.Student; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
public class TestController { | ||
@Autowired | ||
private StudentService studentService; | ||
@GetMapping(value = "/students") | ||
public List<Student> getAllStudents(){ | ||
return studentService.findAll(); | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
java/spring_jdbc_example/src/main/java/com/example/demo/model/Student.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,70 @@ | ||
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
Licensed under the Universal Permissive License v 1.0 | ||
as shown at http://oss.oracle.com/licenses/upl */ | ||
|
||
/* | ||
DESCRIPTION | ||
The code sample maps the students table from Autonomous Database | ||
to this students class, using JPA | ||
|
||
Peter Song 05/11/2022 - Creation | ||
*/ | ||
|
||
package com.example.demo.model; | ||
|
||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Table(name = "Students") | ||
public class Student { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
int ID; | ||
@Column(name = "first_name") | ||
String first_name; | ||
@Column(name = "last_name") | ||
String last_name; | ||
public Student(){ | ||
|
||
} | ||
public Student(int ID, String first_name, String last_name) { | ||
this.ID = ID; | ||
this.first_name = first_name; | ||
this.last_name = last_name; | ||
} | ||
|
||
public int getID() { | ||
return ID; | ||
} | ||
|
||
public void setID(int ID) { | ||
this.ID = ID; | ||
} | ||
|
||
public String getFirst_name() { | ||
return first_name; | ||
} | ||
|
||
public void setFirst_name(String first_name) { | ||
this.first_name = first_name; | ||
} | ||
|
||
public String getLast_name() { | ||
return last_name; | ||
} | ||
|
||
public void setLast_name(String last_name) { | ||
this.last_name = last_name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Student{" + | ||
"ID=" + ID + | ||
", first_name='" + first_name + '\'' + | ||
", last_name='" + last_name + '\'' + | ||
'}'; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
java/spring_jdbc_example/src/main/java/com/example/demo/repository/StudentRepository.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,30 @@ | ||
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
Licensed under the Universal Permissive License v 1.0 | ||
as shown at http://oss.oracle.com/licenses/upl */ | ||
|
||
/* | ||
DESCRIPTION | ||
The code sample extends the JpaRepository | ||
to take advantage of built-in queries provided | ||
by JPA | ||
|
||
Peter Song 05/11/2022 - Creation | ||
*/ | ||
|
||
package com.example.demo.repository; | ||
|
||
|
||
import com.example.demo.model.Student; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
@Repository | ||
@Transactional | ||
@EnableTransactionManagement | ||
public interface StudentRepository extends JpaRepository<Student, Integer> { | ||
|
||
|
||
} |
31 changes: 31 additions & 0 deletions
31
java/spring_jdbc_example/src/main/resources/application.properties
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,31 @@ | ||
#Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
#Licensed under the Universal Permissive License v 1.0 | ||
#as shown at http://oss.oracle.com/licenses/upl */ | ||
# | ||
# | ||
#DESCRIPTION | ||
# This application.properties class provides the relevant information to connect | ||
# to an Oracle Autonomous Database. | ||
#Peter Song 05/11/2022 - Creation | ||
# | ||
|
||
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver | ||
spring.datasource.url=jdbc:oracle:thin:@adbps_medium?TNS_ADMIN=/Users/psong/Desktop/JDBC_vid_series/Wallet_ADBPS | ||
spring.datasource.username=<enter_username_here> | ||
spring.datasource.password=<enter_password_here> | ||
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect | ||
oracle.jdbc.fanEnabled=false | ||
|
||
#UCP Properties | ||
spring.datasource.type=oracle.ucp.jdbc.PoolDataSource | ||
spring.datasource.oracleucp.connection-factory-class-name=oracle.jdbc.pool.OracleDataSource | ||
spring.datasource.oracleucp.sql-for-validate-connection=select * from dual | ||
spring.datasource.oracleucp.connection-pool-name=connectionPoolName1 | ||
spring.datasource.oracleucp.initial-pool-size=15 | ||
spring.datasource.oracleucp.min-pool-size=10 | ||
spring.datasource.oracleucp.max-pool-size=30 | ||
|
||
#UCP Logging | ||
logging.level.root=trace | ||
logging.file.name=logs.log | ||
logging.level.oracle.ucp=trace |
13 changes: 13 additions & 0 deletions
13
java/spring_jdbc_example/src/test/java/com/example/demo/DemoApplicationTests.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,13 @@ | ||
package com.example.demo; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class DemoApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a placeholder just like we have for username and password.