Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 20, 2021 05:02 am GMT

Spring Boot Oracle example: Build a CRUD app

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:

MethodsUrlsActions
POST/api/tutorialscreate new Tutorial
GET/api/tutorialsretrieve all Tutorials
GET/api/tutorials/:idretrieve a Tutorial by :id
PUT/api/tutorials/:idupdate a Tutorial by :id
DELETE/api/tutorials/:iddelete a Tutorial by :id
DELETE/api/tutorialsdelete all Tutorials
GET/api/tutorials/publishedfind 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:

spring-boot-oracle-example-database-table

Create some Tutorials:

spring-boot-oracle-example-crud-create-tutorial

Check Oracle database after create operation:

spring-boot-oracle-example-crud-database-create-tutorial

Update some Tutorials:

spring-boot-oracle-example-crud-update-tutorial

Check Oracle database after update operation:

spring-boot-oracle-example-crud-database-update-tutorial

Get all Tutorials:

Get a Tutorial by Id:

spring-boot-oracle-example-crud-retrieve-one-tutorial

Find all published Tutorials:

spring-boot-oracle-example-crud-search-tutorial-active

Find all Tutorials which title contains 'ring':

spring-boot-oracle-example-crud-search-tutorial-title

Delete a Tutorial:

spring-boot-oracle-example-crud-delete-tutorial

Check Oracle database after delete Operation:

spring-boot-oracle-example-crud-database-delete-tutorial

Delete all Tutorials:

spring-boot-oracle-example-crud-delete-all-tutorial

Now the table has no record:

spring-boot-oracle-example-crud-database-delete-all-tutorial

Technology

  • Java 8
  • Spring Boot 2 (with Spring Web MVC, Spring Data JPA)
  • Oracle 12c or 19c
  • Maven 3.6.1

Project Structure

spring-boot-oracle-example-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:

Or way to write Unit Test for the JPA Repository:
Spring Boot Unit Test for JPA Repositiory with @DataJpaTest

More Practice:


Original Link: https://dev.to/tienbku/spring-boot-oracle-example-build-a-crud-app-5g6a

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To