Spring-MVC-CRUD-Operations
In this tutorial, you will build a simple CRUD application (Create, Read, Update, Delete) in Spring Boot.
We will use
1. Create Database Table
Create a table in your PostgreSQL database
id is auto-incremented using a PostgreSQL sequence.
2. Project Structure
3. Main Application Class
We will use
- Spring Data JPA to interact with the database
- PostgreSQL as the database
- REST API to expose CRUD endpoints
1. Create Database Table
Create a table in your PostgreSQL database
CREATE TABLE product ( id SERIAL PRIMARY KEY, name VARCHAR(100), quantity INTEGER, price DOUBLE PRECISION);
id is auto-incremented using a PostgreSQL sequence.
2. Project Structure
src/main/java/com/quipoin/crud/example/ ├─ controller/ │ └─ ProductController.java ├─ entity/ │ └─ Product.java ├─ repository/ │ └─ ProductRepository.java ├─ service/ │ └─ ProductService.java └─ SpringBootCrudExample2Application.javapom.xml
3. Main Application Class
package com.quipoin.crud.example;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class SpringBootCrudExample2Application { public static void main(String[] args) { SpringApplication.run(SpringBootCrudExample2Application.class, args); }}
4. Product Entity
package com.quipoin.crud.example.entity;
import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;
@Entitypublic class Product { @Id @GeneratedValue private int id; private String name; private int quantity; private double price;
public Product() {}
// Constructor, getters, and setters // (keep your IDE to auto-generate or write them as shown earlier)}
5. Product Repository
package com.quipoin.crud.example.repository;
import org.springframework.data.jpa.repository.JpaRepository;import com.quipoin.crud.example.entity.Product;
public interface ProductRepository extends JpaRepository<Product, Integer> { Product findByName(String name);}
6. Product Service
Handles business logic
package com.quipoin.crud.example.service;
import com.quipoin.crud.example.entity.Product;import com.quipoin.crud.example.repository.ProductRepository;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;
@Servicepublic class ProductService { @Autowired private ProductRepository repository;
public Product saveProduct(Product product) { return repository.save(product); }
public List<Product> saveProducts(List<Product> products) { return repository.saveAll(products); }
public List<Product> getProducts() { return repository.findAll(); }
public Product getProductById(int id) { return repository.findById(id).orElse(null); }
public Product getProductByName(String name) { return repository.findByName(name); }
public String deleteProduct(int id) { repository.deleteById(id); return "Product removed !! " + id; }
public Product updateProduct(Product product) { Product existingProduct = repository.findById(product.getId()).orElse(null); if (existingProduct != null) { existingProduct.setName(product.getName()); existingProduct.setQuantity(product.getQuantity()); existingProduct.setPrice(product.getPrice()); return repository.save(existingProduct); } return null; }}
7. Product Controller
Exposes RESTful endpoints
package com.quipoin.crud.example.controller;
import com.quipoin.crud.example.entity.Product;import com.quipoin.crud.example.service.ProductService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;
@RestControllerpublic class ProductController { @Autowired private ProductService service;
@PostMapping("/addProduct") public Product addProduct(@RequestBody Product product) { return service.saveProduct(product); }
@PostMapping("/addProducts") public List<Product> addProducts(@RequestBody List<Product> products) { return service.saveProducts(products); }
@GetMapping("/products") public List<Product> findAllProducts() { return service.getProducts(); }
@GetMapping("/productById/{id}") public Product findProductById(@PathVariable int id) { return service.getProductById(id); }
@GetMapping("/product/{name}") public Product findProductByName(@PathVariable String name) { return service.getProductByName(name); }
@PutMapping("/update") public Product updateProduct(@RequestBody Product product) { return service.updateProduct(product); }
@DeleteMapping("/delete/{id}") public String deleteProduct(@PathVariable int id) { return service.deleteProduct(id); }}
8. pom.xml
Contains dependencies for Spring Boot, Spring Data JPA, PostgresSQL
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies>
9. How it works
- Spring Boot runs your application
- Controller handles HTTP requests
- Service contains business logic
- Repository handles database operations
10. Run your project
mvn spring-boot:run
Access REST API
- GET all products - /products
- POST new product - /addProduct
- etc.
Conclusion
Using Spring Boot + Spring Data JPA, you can build CRUD applications quickly.
If you had like, I can also help add
Using Spring Boot + Spring Data JPA, you can build CRUD applications quickly.
- Simple
- Maintainable
- Scalable
If you had like, I can also help add
- Swagger API docs
- Custom exception handling
- Frontend (Angular / React) to consume this API