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
Post a Comment