CRUD – Spring Boot + ReactJS + API Rest + MySQL #3 Entity

Spring – Backend

Model / Entity

src/main/java/com/tutofox/company/models/Employee.java

package com.tutofox.company.models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "employee")
public class Employee {

	// TODO- Generate Getters and Setter of all the fields 
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private Integer id;
	
	@Column(name = "name")
	private String name;
	
	@Column(name = "email")
	private String email;
	
	@Column(name = "address")
	private String address;
	
	@Column(name = "phone")
	private Long phone;
	 
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	} 

	public Employee(Integer id, String name, String email, String address, Long phone) {
		super();
		this.id = id;
		this.name = name;
		this.email = email;
		this.address = address;
		this.phone = phone;
	} 

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Long getPhone() {
		return phone;
	}

	public void setPhone(Long phone) {
		this.phone = phone;
	}
  
}

Repository

src/main/java/com/tutofox/company/repository/EmployeeRepository.java

package com.tutofox.company.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.tutofox.company.models.Employee;
 
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {

	List<Employee> findByName(String name);
	List<Employee> findByNameLike(String name);
}

Controller

src/main/java/com/tutofox/company/controllers/EmployeeController.java

package com.tutofox.company.controllers;
  
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tutofox.company.models.Employee;
import com.tutofox.company.repository.EmployeeRepository;
 

@RestController
@RequestMapping("/api/employee")
public class EmployeeController {

	@Autowired
	EmployeeRepository employeeRepository;
	
	@GetMapping(value = "/test")
	public List<Employee> test() {
		return employeeRepository.findAll();
	}
	
	@GetMapping(value = "/test2")
	public Optional<Employee> test2() {
		int id = 2;
		return employeeRepository.findById(id);
	} 
	
	@GetMapping(value = "/test3")
	public List<Employee> test3() {
		String name = "Maria";
		String likeName = "%"+name+"%";
		return employeeRepository.findByNameLike(name);
	} 	
	 
}

src/main/resources/application.properties

# Indicar el DBMS
spring.jpa.database: MYSQL
# Indica si debe mostrar el log de las consultas sql ejecutadas
spring.jpa.show-sql: true 
# Configurar Hibernate
spring.jpa.hibernate.ddl-auto: update
# he SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect 
#  mostrar sql 
spring.jpa.properties.hibernate.format_sql=true
# mostrar el error de sql
logging.level.org.hibernate.SQL=DEBUG

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *