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.
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/.
Configure Application Properties:
Open the src/main/resources/application.properties
file and add the following configuration:
properties# 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
Define the Entity:
Create a Product
class representing the entity you want to manage. Annotate the class with JPA annotations.
javaimport 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
}
ProductRepository
interface that extends JpaRepository
to perform CRUD operations.javaimport org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository<Product, Long> {
}
ProductController
class to handle HTTP requests and interact with the repository.javaimport 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);
}
}
ProductController
class.You can then use tools like Postman or CURL to test the CRUD operations:
/api/products
with JSON payload./api/products
./api/products/{id}
./api/products/{id}
with JSON payload./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.