Spring Boot application with JPA (Java Persistence API) for performing CRUD (Create, Read, Update, Delete) operations

Category : Spring Boot | Sub Category : Spring Boot | By Prasad Bonam Last updated: 2023-08-07 18:32:37 Viewed : 68

Spring Boot application with JPA (Java Persistence API) for performing CRUD (Create, Read, Update, Delete) operations:

Here is an example of a Spring Boot application with JPA (Java Persistence API) for performing CRUD (Create, Read, Update, Delete) operations on a simple entity. In this example, we will create a basic "Product" entity with fields like ID, name, and price.

  1. Create a Spring Boot Project: You can use Spring Initializer to generate a new Spring Boot project with the necessary dependencies (Spring Web, Spring Data JPA, H2 Database, etc.). You can access Spring Initializer at https://start.spring.io/.

    1. Configure Application Properties: Open the src/main/resources/application.properties file and add the following configuration:

    # H2 Configuration spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.h2.console.enabled=true # JPA Configuration spring.jpa.hibernate.ddl-auto=update
  2. Define the Entity: Create a Product class representing the entity you want to manage. Annotate the class with JPA annotations.

import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private double price; // getters and setters }
  1. Create a Repository: Create a ProductRepository interface that extends JpaRepository to perform CRUD operations.
import org.springframework.data.jpa.repository.JpaRepository; public interface ProductRepository extends JpaRepository<Product, Long> { }
  1. Create a Controller: Create a ProductController class to handle HTTP requests and interact with the repository.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/products") public class ProductController { @Autowired private ProductRepository productRepository; @GetMapping public List<Product> getAllProducts() { return productRepository.findAll(); } @GetMapping("/{id}") public Product getProductById(@PathVariable Long id) { return productRepository.findById(id).orElse(null); } @PostMapping public Product createProduct(@RequestBody Product product) { return productRepository.save(product); } @PutMapping("/{id}") public Product updateProduct(@PathVariable Long id, @RequestBody Product productDetails) { Product product = productRepository.findById(id).orElse(null); if (product != null) { product.setName(productDetails.getName()); product.setPrice(productDetails.getPrice()); return productRepository.save(product); } return null; } @DeleteMapping("/{id}") public void deleteProduct(@PathVariable Long id) { productRepository.deleteById(id); } }
  1. Run the Application: Run your Spring Boot application. It will start an embedded web server and expose the RESTful endpoints defined in the ProductController class.

You can then use tools like Postman or CURL to test the CRUD operations:

  • Create a new product: POST to /api/products with JSON payload.
  • Retrieve all products: GET from /api/products.
  • Retrieve a specific product: GET from /api/products/{id}.
  • Update a product: PUT to /api/products/{id} with JSON payload.
  • Delete a product: DELETE from /api/products/{id}.

Remember to configure your application.properties or application.yml file with the appropriate database settings and other configurations.

This example demonstrates the basic setup for Spring Boot with JPA CRUD operations. You can extend and customize this example based on your specific requirements.

Related Articles

Leave a Comment: