Skip to content

Commit

Permalink
mas ejemplos
Browse files Browse the repository at this point in the history
  • Loading branch information
Elemma00 committed Jun 30, 2024
1 parent 0ee2873 commit 260a19a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.emma.curso.springboot.jpa.springboot_jpa;

import java.util.List;
import java.util.Optional;
import java.util.Scanner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.Transactional;

import com.emma.curso.springboot.jpa.springboot_jpa.entities.Person;
import com.emma.curso.springboot.jpa.springboot_jpa.repositories.PersonRepository;


@SpringBootApplication
public class SpringbootJpaApplication implements CommandLineRunner {

Expand All @@ -22,13 +26,103 @@ public static void main(String[] args) {

@Override
public void run(String... args) throws Exception {
personalizedQueries2();
}

@Transactional(readOnly = true)
public void personalizedQueries2(){
System.out.println("consulta que puebla y devuelve un objeto entity de una instancia personalizada");
List<Person> persons = repository.findAllObjectPersonPersonalized();
persons.forEach(System.out::println);
}

@Transactional(readOnly = true)
public void personalizedQueries(){
Scanner scanner = new Scanner(System.in);
System.out.println("Ingrese el id de la persona");
Long id = scanner.nextLong();
scanner.close();
String name = repository.getNameById(id);
String fullName = repository.getFullNameById(id);
System.out.println(name);
System.out.println(fullName);
}

@Transactional
public void delete2(){
Scanner scanner = new Scanner(System.in);
System.out.println("Ingrese el id de la persona a eliminar");
Long id = scanner.nextLong();

Optional<Person> personOptional = repository.findById(id);
personOptional.ifPresent(repository::delete);
}


@Transactional
public void delete(){
Scanner scanner = new Scanner(System.in);
System.out.println("Ingrese el id de la persona a eliminar");
Long id = scanner.nextLong();
repository.deleteById(id);
scanner.close();
repository.findAll().forEach(System.out::println);
}

@Transactional
public void update(){

Scanner scanner = new Scanner(System.in);
System.out.println("Ingrese el id de la persona a actualizar");
Long id = scanner.nextLong();

Optional<Person> personOptional = repository.findById(id);
personOptional.ifPresent(person -> {
System.out.println(person);
System.out.println("Ingrese el lenguaje de programación");
String programmingLanguage = scanner.next();
person.setProgrammingLanguage(programmingLanguage);
Person personDB = repository.save(person);
System.out.println(personDB);
});

scanner.close();

}

@Transactional
public void create() {
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
String lastName = scanner.nextLine();
String programmingLanguage = scanner.nextLine();
scanner.close();
Person person = new Person(null, name, lastName, programmingLanguage);

Person personNew = repository.save(person);
System.out.println(personNew);

repository.findById(personNew.getId()).ifPresent(System.out::println);

}

@Transactional(readOnly = true)
public void findOne() {
repository.findById(1L).ifPresent(System.out::println);
repository.findByNameIgnoreCase("pepe").ifPresent(System.out::println);
repository.findByNameStartingWithIgnoreCase("pe").ifPresent(System.out::println);
}

public void list() {
// List<Person> persons = (List<Person>) repository.findAll();
// List<Person> persons = repository.findByProgrammingLanguage("Java");
// List<Person> persons = repository.buscarByProgrammingLanguague("Java");
List<Person> persons = repository.findByProgrammingLanguageAndName("Java", "Andres");

persons.stream().forEach(person -> System.out.println(person));

List<Object[]> personsValues = repository.obtenerPersonData();
personsValues.stream().forEach(p -> System.out.println(p[0] + " es experto en " + p[1]));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class Person {
public Person() {
}

public Person(String name, String lastname){
this.name = name;
this.lastname = lastname;
}

public Person(Long id, String name, String lastname, String programmingLanguage) {
this.id = id;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
package com.emma.curso.springboot.jpa.springboot_jpa.repositories;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import com.emma.curso.springboot.jpa.springboot_jpa.entities.Person;

public interface PersonRepository extends CrudRepository<Person,Long> {

@Query("select new Person(p.name, p.lastname) from Person p")
List<Person> findAllObjectPersonPersonalized();

@Query("select p from Person p where p.id=?1")
String getNameById(Long id);

@Query("select concat(p.name, ' ', p.lastname) as fullname from Person p where p.id=?1")
String getFullNameById(Long id);

List<Person> findByProgrammingLanguage(String programmingLanguage);

Optional<Person> findByNameStartingWithIgnoreCase(String name);

Optional<Person> findByNameIgnoreCase(String name);

Optional<Person> findByNameContaining(String name);

@Query("select p from Person p where p.programmingLanguage=?1")
List<Person> buscarByProgrammingLanguague(String programmingLanguague);

List<Person> findByProgrammingLanguageAndName(String programmingLanguage, String name);

@Query("select p.name, p.programmingLanguage from Person p")
List<Object[]> obtenerPersonData();

@Query("select p.name, p.programmingLanguage from Person p where p.programmingLanguage=?1 and p.name=?2")
List<Object[]> obtenerPersonData(String programmingLanguage, String name);


}

0 comments on commit 260a19a

Please sign in to comment.