This content originally appeared on DEV Community and was authored by Tien Nguyen
In this tutorial, we're gonna build a Spring Boot and Oracle example with database connection that uses Hibernate, Spring Data JPA to make CRUD Operations. You'll know:
- How to configure Spring Data, JPA, Hibernate to work with Oracle Database
- How to define Data Models and Repository interfaces
- Way to create Spring Rest Controller to process HTTP requests
- Way to use Spring Data JPA to interact with Oracle Database
Full Article at: https://bezkoder.com/spring-boot-hibernate-oracle/
Overview of Spring Boot and Oracle example
We will build a Spring Boot + Hibernate + Oracle example that exports Rest CRUD API for a Tutorial application:
- Each Tutotial has id, title, description, published status.
- Apis help to create, retrieve, update, delete Tutorials.
- Apis also support custom finder methods such as find by published status or by title.
These are APIs that we need to provide:
Methods | Urls | Actions |
---|---|---|
POST | /api/tutorials | create new Tutorial |
GET | /api/tutorials | retrieve all Tutorials |
GET | /api/tutorials/:id | retrieve a Tutorial by :id
|
PUT | /api/tutorials/:id | update a Tutorial by :id
|
DELETE | /api/tutorials/:id | delete a Tutorial by :id
|
DELETE | /api/tutorials | delete all Tutorials |
GET | /api/tutorials/published | find all published Tutorials |
GET | /api/tutorials?title=[keyword] | find all Tutorials which title contains keyword
|
- We make CRUD operations & finder methods with Hibernate and Spring Data JPA's
JpaRepository
. - The database will be Oracle 12c/19c by configuring project dependency & datasource.
When you run the Spring Boot + Oracle example, tutorials table will be automatically generated in Oracle Database.
You can see things like this:
Create some Tutorials:
Check Oracle database after create operation:
Update some Tutorials:
Check Oracle database after update operation:
Get all Tutorials:
Get a Tutorial by Id:
Find all published Tutorials:
Find all Tutorials which title contains 'ring':
Delete a Tutorial:
Check Oracle database after delete Operation:
Delete all Tutorials:
Now the table has no record:
Technology
- Java 8
- Spring Boot 2 (with Spring Web MVC, Spring Data JPA)
- Oracle 12c or 19c
- Maven 3.6.1
Project Structure
Let me explain it briefly.
– Tutorial
data model class corresponds to entity and table tutorials.
– TutorialRepository
is an interface that extends JpaRepository for CRUD methods and custom finder methods. It will be autowired in TutorialController
.
– TutorialController
is a RestController which has request mapping methods for RESTful requests such as: getAllTutorials, createTutorial, updateTutorial, deleteTutorial, findByPublished...
– Configuration for Spring Datasource, JPA & Hibernate in application.properties.
– pom.xml contains dependencies for Spring Boot and Oracle.
For step by step and Github, please visit:
https://bezkoder.com/spring-boot-hibernate-oracle/
Further Reading
If you want to add Pagination to this Spring project, you can find the instruction at:
Spring Boot Pagination & Filter example | Spring JPA, Pageable
To sort/order by multiple fields:
Spring Data JPA Sort/Order by multiple Columns | Spring Boot
Handle Exception for this Rest APIs is necessary:
- Spring Boot @ControllerAdvice & @ExceptionHandler example
- @RestControllerAdvice example in Spring Boot
Or way to write Unit Test for the JPA Repository:
Spring Boot Unit Test for JPA Repositiory with @DataJpaTest
More Practice:
- Spring Boot Rest XML example – Web service with XML Response
- Spring Boot Multipart File upload example
- Spring Boot Pagination and Sorting example
This content originally appeared on DEV Community and was authored by Tien Nguyen
Tien Nguyen | Sciencx (2021-05-20T05:02:30+00:00) Spring Boot + Oracle example: Build a CRUD app. Retrieved from https://www.scien.cx/2021/05/20/spring-boot-oracle-example-build-a-crud-app/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.