Spring Boot with Spring Data JPA

๐Ÿ”ง What is Spring Boot?

A framework that makes it easy to build Java web applications.

Auto-configures a lot of stuff for you. You don’t have to manually write boilerplate code.



---

๐Ÿ“ฆ What is Spring Data JPA?

A part of Spring that makes database operations super easy.

You just create interfaces, and Spring Data JPA auto-generates SQL behind the scenes.



---

๐Ÿง  Simple Analogy

Imagine you're the manager (Spring Boot).
You have a helper (Spring Data JPA) who knows how to talk to the database.

You just say:

> “Hey, get me all students”
or
“Save this new student”



And the helper (JPA) does it for you automatically.


---

๐Ÿ’ก Technologies Used

Spring Boot

Spring Data JPA

H2 Database (In-memory for testing)



---

✅ Step-by-step Example: "Student Management"

1. ๐Ÿ“ Entity: Student.java

import jakarta.persistence.*;

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;

    // Getters and Setters
}

๐Ÿ“ This is our table in the database. Each Student object becomes a row.


---

2. ๐Ÿ“ Repository: StudentRepository.java

import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository extends JpaRepository<Student, Long> {
    // No need to write SQL! Spring creates everything.
}

๐Ÿ“ This is like our data access layer. Just extend JpaRepository and done!


---

3. ๐Ÿ“ Service: StudentService.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentRepository repo;

    public List<Student> getAllStudents() {
        return repo.findAll();
    }

    public Student addStudent(Student s) {
        return repo.save(s);
    }

    public void deleteStudent(Long id) {
        repo.deleteById(id);
    }
}

๐Ÿ“ This is where we write business logic.


---

4. ๐Ÿ“ Controller: StudentController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService service;

    @GetMapping
    public List<Student> getStudents() {
        return service.getAllStudents();
    }

    @PostMapping
    public Student createStudent(@RequestBody Student student) {
        return service.addStudent(student);
    }

    @DeleteMapping("/{id}")
    public void deleteStudent(@PathVariable Long id) {
        service.deleteStudent(id);
    }
}

๐Ÿ“ This is where we define our API endpoints.


---

๐ŸŒฑ application.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true

๐Ÿ“ This sets up an in-memory H2 database for quick testing.


---

๐Ÿ“ธ Visual Flow

Browser/Postman → Controller → Service → Repository → Database


---

๐Ÿงช Sample Test via Postman

POST /students

{
  "name": "Alice",
  "email": "alice@example.com"
}

GET /students
→ Returns list of students.


---

๐Ÿคฉ That's it!

Without writing any SQL, you:

Created a table

Added data

Fetched data

Deleted data



---

Would you like me to create a diagram or a GitHub-style project structure too?


Comments

Popular posts from this blog

Post GIS

What is GIS?

Spring Boot Application Properties and YAML Configuration