Glab S14 Jbeeck 2024 1
Glab S14 Jbeeck 2024 1
LABORATORIO Nº 14
JPA en Spring Boot
Alumno(s): Nota
Grupo: Ciclo: IV
Requiere No
Excelente Bueno Puntaje
Criterio de Evaluación mejora acept.
(4pts) (3pts) Logrado
(2pts) (0pts)
Instala, configura la arquitectura necesaria
para la puesta en marcha de la aplicación de
software.
Crea el MVC propuesto.
La información se almacena en la BD.
Logra conectarse a los endpoints propuestos.
Se comunican efectivamente de manera oral,
escrita y gráfica.
TEMA: MVC persistente y JPA en el Framework Spring Boot
OBJETIVOS
● Crear un proyecto con conexión a una base de datos relacional.
REQUERIMIENTOS
● Java SDK
● MySQL.
Nro. xxx
Página 2/9
DISEÑO Y DESARROLLO DE SOFTWARE Laboratorio Nro. 01
CURSO: Desarrollo de Aplicaciones Web
MARCO TEÓRICO
PROCEDIMIENTO
(** El laboratorio se ha diseñado para ser desarrollado en grupos de 2 o 3 personas**)
package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int age;
Creación del Repositorio, la interfaz para las operaciones CRUD en la entidad Student.
package com.example.demo.repository;
import com.example.demo.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
Creación del Servicio Student. La clase contiene la lógica de negocios para las operaciones de Student.
package com.example.demo.service;
import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
// Custom method to find students whose name starts with a specific prefix
public List<Student> getStudentsByPrefix(String prefix) {
return studentRepository.findByPrefix(prefix);
}
package com.example.demo.controller;
import com.example.demo.model.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/students")
public class StudentController {
@Autowired
private StudentService studentService;
@PostMapping
public Student createStudent(@RequestBody Student student) {
return studentService.createStudent(student);
}
@GetMapping
public List<Student> getAllStudents() {
Nro. xxx
Página 6/9
DISEÑO Y DESARROLLO DE SOFTWARE Laboratorio Nro. 01
CURSO: Desarrollo de Aplicaciones Web
return studentService.getAllStudents();
}
@GetMapping("/{id}")
public ResponseEntity<Student> getStudentById(@PathVariable Long id) {
Optional<Student> student = studentService.getStudentById(id);
return student.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
@PutMapping("/{id}")
public ResponseEntity<Student> updateStudent(@PathVariable Long id, @RequestBody Student
studentDetails) {
Student updatedStudent = studentService.updateStudent(id, studentDetails);
return ResponseEntity.ok(updatedStudent);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteStudent(@PathVariable Long id) {
studentService.deleteStudent(id);
return ResponseEntity.noContent().build();
}
// Custom endpoint to find students whose name starts with a specific prefix
@GetMapping("/byprefix/{prefix}")
public List<Student> getStudentsByPrefix(@PathVariable String prefix) {
return studentService.getStudentsByPrefix(prefix);
}
GET /api/students/byname/{name}
GET /api/students/byname/John20Doe%
http://localhost:8080/api/students/byname/John%20Doe
GET /api/students/byage/{minAge}/{maxAge}
GET /api/students/byage/20/25
http://localhost:8080/api/students/byage/20/25
GET /api/students/byprefix/{prefix}
GET /api/students/byprefix/John
http://localhost:8080/api/students/byprefix/John
Nro. xxx
Página 8/9
DISEÑO Y DESARROLLO DE SOFTWARE Laboratorio Nro. 01
CURSO: Desarrollo de Aplicaciones Web
2. Cree un objeto usando JPA y pruebe los endpoints (mínimo tres) que ustedes vean necesario.
Nro. xxx
Página 9/9
DISEÑO Y DESARROLLO DE SOFTWARE Laboratorio Nro. 01
CURSO: Desarrollo de Aplicaciones Web
Conclusiones:
Indicar las conclusiones que llegó después de los temas tratados de manera práctica en este laboratorio.