JPA - ORM
JPA stands for Java Persistence API. It is an object-relational mapping (ORM) framework that allows us to map Java objects to tables in a relational database.
This guide will walk you through the process of setting up a Spring Boot project, defining the necessary entities, creating the repository, implementing service methods, and exposing REST endpoints.
Step 1: Create a Spring Boot Project
Start by creating a new Spring Boot project in your preferred IDE. You can use Spring Initializer (https://start.spring.io/) or use your IDE's project creation wizard. Select the following dependencies:
Spring Web
Spring Data JPA
MySQL Driver
Once the project is created, import it into your IDE.
Step 2: Define Entity Classes
Create the following entity classes to represent the school schema:
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
private int age;
// Constructors, getters, setters, and other methods
}
Step 3: Create Repository Interfaces
Create a repository interface for each entity by extending the JpaRepository
:
public interface StudentRepository extends JpaRepository<Student, Long> {
// Custom queries can be added here if needed
}
Step 4: Implement Service Classes
Create service classes to encapsulate the business logic. Here's a simple example for the StudentService
:
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public List<Student> getAllStudents() {
return studentRepository.findAll();
}
public Student getStudentById(Long id) {
return studentRepository.findById(id).orElse(null);
}
public Student saveStudent(Student student) {
return studentRepository.save(student);
}
public void deleteStudent(Long id) {
studentRepository.deleteById(id);
}
}
Step 5: Create REST Controllers
Develop REST controllers to handle HTTP requests and interact with the service classes:
@RestController
@RequestMapping("/api/students")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping
public List<Student> getAllStudents() {
return studentService.getAllStudents();
}
@GetMapping("/{id}")
public ResponseEntity<Student> getStudentById(@PathVariable Long id) {
Student student = studentService.getStudentById(id);
return ResponseEntity.ok(student);
}
@PostMapping
public ResponseEntity<Student> createStudent(@RequestBody Student student) {
Student savedStudent = studentService.saveStudent(student);
return new ResponseEntity<>(savedStudent, HttpStatus.CREATED);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteStudent(@PathVariable Long id) {
studentService.deleteStudent(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Step 6: Configure Database Connection
Configure the database connection in the application.properties
or application.yml
file:
spring.datasource.url=jdbc:mysql://localhost:3306/school_db
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
Step 7: Run the Application
Run the Spring Boot application. The REST APIs should be accessible at http://localhost:8080/api/students
.
You can now extend and customize the application based on your specific requirements. If you have any questions or encounter issues, feel free to refer to the official Spring Boot documentation or seek help from the community.
Last updated
Was this helpful?