,

How to develop a Spring Boot API with data from Oracle using CRUD operations

Posted by

In today’s world, Spring Boot APIs have become an integral part of software development. APIs allow the data systems to be portable it is not machine dependent. In our use case, we intend to create a Spring Boot API with CRUD (Create, Read, Update, Delete) operations using an Oracle database.

Before we begin, let’s take a quick look at some of the tools and technologies we’ll be using:

Spring Boot: A popular Java-based framework for building web applications.
Oracle Database: A relational database management system that is widely used in enterprise applications.
Spring Data JPA: this is a library that makes API streamlines interaction with databases using the Java Persistence API (JPA).
Maven: A build automation tool that is used to manage dependencies and build the project.
Now that we have a basic understanding of the tools and technologies involved, let’s dive into the implementation.

Step 1: Setting up the Oracle database


to set up the Oracle database first create a new schema and a table to store the data. Example table name employees with columns “id”, “name”, “email”, and “phone”.

Step 2: Create a Spring Boot project


we are required to create a Spring Boot project using Maven, IDE like IntelliJ, Eclipse, and VS Code are the ones usually recommended to use to create a new Spring Boot project, these are the steps:

Open your IDE and select “File” -> “New” -> “Project”.
Select “Maven” -> “Maven Project” and click “Next”.
Select “Spring Boot” and click “Next”.
Enter the Group ID, Artifact ID, and other details as required.
Click “Finish” to create the project.


Step 3: Add dependencies


for Spring Boot APIs we are required to add the dependencies examples are the ones explained below

Spring Boot Starter Web: Provides the necessary libraries for building web applications.
Spring Boot Starter Data JPA: Provides the necessary libraries for working with JPA.
Oracle JDBC Driver: Provides the necessary libraries for connecting to the Oracle database.
Spring Boot Dependencies are added to the pom.xml file in the project

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa com.oracle.database.jdbc ojdbc8 19.3.0.0


Step 4: Configure the database


The next step is to configure the database connection. We need to provide the database URL, username, and password in the application.properties file. Open the application.properties file and add the following code:

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update


Step 5: Create the entity class


the entity class is what is used to represent the “employees” table in the database. the class for our entity we will call “Employee” with fields such:-“id”, “name”, “email”, and “phone”. annotate the class with “@Entity” and specify the table name using “@Table”.

@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String email;
private String phone;
// getters and setters
}


Step 6: Create the repository interface


The next step is to create the repository interface that provides the necessary methods for CRUD operations. We can create an interface called “EmployeeRepository” that extends the JpaRepository interface and specifies the entity class and the primary key type.

@Repository
public interface EmployeeRepository extends JpaRepository {
}


Step 7: Create the service class


the service class is what provides the business logic for the API. Our Service we will call “EmployeeService” uses the repository to perform CRUD operations.

@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
public List getAllEmployees() {
return employeeRepository.findAll();
}
public Employee getEmployeeById(Long id) {
return employeeRepository.findById(id).orElse(null);
}
public Employee addEmployee(Employee employee) {
return employeeRepository.save(employee);
}
public Employee updateEmployee(Employee employee) {
return employeeRepository.save(employee);
}
public void deleteEmployee(Long id) {
employeeRepository.deleteById(id);
}
}

Step 8: Create the controller class


The final step is to create the controller class that exposes the API endpoints. We can create a class called EmployeeController that uses the service to perform CRUD operations.

@RestController
@RequestMapping("/api/v1")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/employees")
public List getAllEmployees() {
return employeeService.getAllEmployees();
}
@GetMapping("/employees/{id}")
public Employee getEmployeeById(@PathVariable Long id) {
return employeeService.getEmployeeById(id);
}
@PostMapping("/employees")
public Employee addEmployee(@RequestBody Employee employee) {
return employeeService.addEmployee(employee);
}
@PutMapping("/employees")
public Employee updateEmployee(@RequestBody Employee employee) {
return employeeService.updateEmployee(employee);
}
@DeleteMapping("/employees/{id}")
public void deleteEmployee(@PathVariable Long id) {
employeeService.deleteEmployee(id);
}
}


That’s it! We have now created a Spring Boot API with CRUD operations using an Oracle database. You can run the application and test the API endpoints using tools such as Postman.

Conclusion


In this blog post, we have explored how to create a Spring Boot API with CRUD operations using an Oracle database. We have used tools and technologies such as Spring Boot, Oracle Database, Spring Data JPA, and Maven to build the application.

Related Content

Spring Boot APIs