package br.com.alura.Spring.Data;
import java.util.Scanner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import Service.CrudCargoService;
@SpringBootApplication
public class SpringDataApplication implements CommandLineRunner {
private Boolean system = true;
private final CrudCargoService service;
public SpringDataApplication(CrudCargoService service) {
this.service = service;
}
public static void main(String[] args) {
SpringApplication.run(SpringDataApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Scanner sc = new Scanner(System.in);
while (system) {
System.out.print("Qual operaçao deseja ?");
System.out.println("0 - sair");
System.out.println("1 - cargo");
int action = sc.nextInt();
if (action == 1) {
service.inicial(sc);
}else {
system = false;
}
}
}
}
package Service;
import java.util.Scanner;
import org.springframework.stereotype.Service;
import br.com.alura.Spring.Data.orm.Cargo;
import repository.CargoRepository;
@Service
public class CrudCargoService {
private final CargoRepository repository;
public CrudCargoService(CargoRepository repository) {
this.repository = repository;
}
public void inicial(Scanner sc) {
salvar(sc);
}
private void salvar(Scanner sc) {
System.out.print("Descricao do cargo: ");
String descricao = sc.next();
Cargo cargo = new Cargo();
cargo.setDescricao(descricao);
repository.save(cargo);
System.out.println("salvo");
}
}
package br.com.alura.Spring.Data.orm;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "cargos")
public class Cargo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String descricao;
public Cargo() {
}
public Cargo(Integer id, String descricao) {
this.id = id;
this.descricao = descricao;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
}
package repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import br.com.alura.Spring.Data.orm.Cargo;
@Repository
public interface CargoRepository extends CrudRepository<Cargo, Integer>{
}