Paso 1: Configurar el Proyecto 1.1 En PostgreSQL, crea una nueva base de datos llamada music_app: sql: "CREATE DATABASE music_app;"
1.2 Agregar las dependencias necesarias para Spring Boot, JPA, PostgreSQL e Hibernate:
xml org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web org.postgresql postgresql 42.2.18 org.springframework.boot spring-boot-starter-thymeleaf
1.3 Configurar la conexión a la base de datos en el archivo application.properties.
"properties" spring.datasource.url=jdbc:postgresql://localhost:5432/music_app spring.datasource.username=postgres spring.datasource.password=MI-CONTRASEÑA spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect spring.jpa.hibernate.ddl-auto=update
Paso 2: Modelar y Mapear Entidades
2.1 Definir Entidad Cantante
java: import javax.persistence.*; import java.util.ArrayList; import java.util.List;
@Entity public class Cantante { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
private String nombre;
@OneToMany(mappedBy = "cantante", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Cancion> canciones = new ArrayList<>();
public void agregarCancion(Cancion cancion) {
canciones.add(cancion);
cancion.setCantante(this);
}
// Getters y setters
}
2.2 Definir Entidad Cancion:
java import javax.persistence.*;
@Entity public class Cancion { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
private String titulo;
@ManyToOne
@JoinColumn(name = "cantante_id")
private Cantante cantante;
// Getters y setters
}
Paso 3: Crear Repositorios 3.1 Repositorio CantanteRepository
java: import org.springframework.data.jpa.repository.JpaRepository;
public interface CantanteRepository extends JpaRepository<Cantante, Long> { Cantante findByNombre(String nombre); }
3.2 Repositorio CancionRepository
java: import org.springframework.data.jpa.repository.JpaRepository;
public interface CancionRepository extends JpaRepository<Cancion, Long> { List findByCantanteNombre(String nombre); }
Paso 4: Crear Servicio y Controlador
4.1 Servicio MusicaService
java: import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
@Service public class MusicaService { @Autowired private CantanteRepository cantanteRepository;
@Autowired
private CancionRepository cancionRepository;
public Cantante registrarCantante(String nombre) {
Cantante cantante = new Cantante();
cantante.setNombre(nombre);
return cantanteRepository.save(cantante);
}
public Cancion registrarCancion(String titulo, String nombreCantante) {
Cantante cantante = cantanteRepository.findByNombre(nombreCantante);
if (cantante == null) {
cantante = registrarCantante(nombreCantante);
}
Cancion cancion = new Cancion();
cancion.setTitulo(titulo);
cancion.setCantante(cantante);
return cancionRepository.save(cancion);
}
public List<Cancion> buscarCancionesPorCantante(String nombreCantante) {
return cancionRepository.findByCantanteNombre(nombreCantante);
}
}
4.2 MusicaController
java: import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController @RequestMapping("/api") public class MusicaController { @Autowired private MusicaService musicaService;
@PostMapping("/cantantes")
public Cantante registrarCantante(@RequestParam String nombre) {
return musicaService.registrarCantante(nombre);
}
@PostMapping("/canciones")
public Cancion registrarCancion(@RequestParam String titulo, @RequestParam String nombreCantante) {
return musicaService.registrarCancion(titulo, nombreCantante);
}
@GetMapping("/canciones")
public List<Cancion> buscarCancionesPorCantante(@RequestParam String nombreCantante) {
return musicaService.buscarCancionesPorCantante(nombreCantante);
}
}