عملیات CRUD (ایجاد ، خواندن ، به روزرسانی ، حذف) با استفاده از Boot Spring Java با PostgreSQL-2025

در اینجا یک نمونه گام به گام از عملیات CRUD (ایجاد ، خواندن ، به روزرسانی ، حذف) با استفاده از Boot Spring Java با PostgreSQL ، به روز شده برای 2025 بهترین روش ها آورده شده است.
✅ ابزارها و فناوری ها:
🔧 مرحله 1: یک پروژه بوت بهار ایجاد کنید
از Spring Initializr استفاده کنید:
- پروژه: Maven
- زبان: جاوا
- بوت بهار: 3.2.x
- وابستگی ها: -spring وب -pring داده JPA -PostgreSQL درایور -Spring Boot Devtools
پروژه را تولید و از حالت فشرده کنید.
🗂 مرحله 2: پیکربندی Application.Properties
در SRC/Main/Resources/Application.Properties:
spring.application.name=java-crud-postgre-sql
spring.datasource.url=jdbc:postgresql://localhost:5432/database_name
spring.datasource.username=DB user name
spring.datasource.password=DB password here
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
server.port=8080
اطمینان حاصل کنید که سرور PostgreSQL در حال اجرا است و پایگاه داده database_name وجود دارد.
🧱 مرحله 3: یک کلاس موجودیت ایجاد کنید
package com.example.demo.model;
import jakarta.persistence.*;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Double price;
// Getters and Setters
}
📦 مرحله 4: یک مخزن ایجاد کنید
package com.example.demo.repository;
import com.example.demo.model.Product;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository {
}
⚙ مرحله 5: ایجاد یک لایه سرویس (اختیاری اما بهترین تمرین)
package com.example.demo.service;
import com.example.demo.model.Product;
import com.example.demo.repository.ProductRepository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class ProductService {
private final ProductRepository repo;
public ProductService(ProductRepository repo) {
this.repo = repo;
}
public List getAll() {
return repo.findAll();
}
public Product save(Product product) {
return repo.save(product);
}
public Optional getById(Long id) {
return repo.findById(id);
}
public void deleteById(Long id) {
repo.deleteById(id);
}
public Product update(Long id, Product updatedProduct) {
return repo.findById(id).map(product -> {
product.setName(updatedProduct.getName());
product.setPrice(updatedProduct.getPrice());
return repo.save(product);
}).orElseThrow(() -> new RuntimeException("Product not found"));
}
}
🌐 مرحله 6: یک کنترلر استراحت ایجاد کنید
package com.example.demo.controller;
import com.example.demo.model.Product;
import com.example.demo.service.ProductService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/products")
public class ProductController {
private final ProductService service;
public ProductController(ProductService service) {
this.service = service;
}
@GetMapping
public List getAll() {
return service.getAll();
}
@GetMapping("/{id}")
public ResponseEntity getById(@PathVariable Long id) {
return service.getById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@PostMapping
public Product create(@RequestBody Product product) {
return service.save(product);
}
@PutMapping("/{id}")
public Product update(@PathVariable Long id, @RequestBody Product product) {
return service.update(id, product);
}
@DeleteMapping("/{id}")
public ResponseEntity delete(@PathVariable Long id) {
service.deleteById(id);
return ResponseEntity.noContent().build();
}
}
🧪 مرحله 7: تست با پستچی یا فرفری
ارسال /API /محصولات – ایجاد
دریافت /API /محصولات – همه را بخوانید
دریافت/api/products/{id} – یکی را بخوانید
قرار دادن/api/products/{id} – بروزرسانی
حذف/api/products/{id} – حذف
repo github: https://github.com/saifcse06/java-crud-postgre-sql/
اگر شک و یا پیشنهادی دارید ، در بخش نظرات به من اطلاع دهید. برنامه نویسی مبارک!