Database Design
μ΄μ μ μ€κ³ν API Design μ κΈ°λ°μΌλ‘ κ°λ¨νκ² Database μ€κ³ λ° sample data λ₯Ό μΆκ°ν΄λ³΄μ.
CREATE DATABASE IF NOT EXISTS `employee_directory`;
USE `employee_directory`;
--
-- Table structure for table `employee`
--
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`id` int NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) DEFAULT NULL,
`last_name` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
--
-- Data for table `employee`
--
INSERT INTO `employee` VALUES
(1,'Leslie','Andrews','leslie@luv2code.com'),
(2,'Emma','Baumgarten','emma@luv2code.com'),
(3,'Avani','Gupta','avani@luv2code.com'),
(4,'Yuri','Petrov','yuri@luv2code.com'),
(5,'Juan','Vega','juan@luv2code.com');Create Project

Dependency λ₯Ό μμ κ°μ΄ ν¬ν¨μν¨ λ€, project λ₯Ό generate νμ.
Development Process
Development Process λ μ°μ λ€μ λ¨κ³λ₯Ό λ°λΌμ μ§ννλ€.
- Update DB configs in
application.properties- Create Employee entity (entity class)
- Create DAO interface
- Create DAO implementation
- Create REST controller to use DAO
μ²μμλ μλμ κ°μ Architecture λ‘ κ΅¬μ±λλ€.

μ΄μ μ DAO λ₯Ό μ²μ λ€λ€μ λμ κ°μ΄, DAO λ₯Ό Controller μμ μ§μ injection λ°μμ μ¬μ©νλ ꡬ쑰μ΄λ€. μ΄μ Development Process μ μλ λ¨κ³λ€μ νλμ© κ΅¬νν΄λ³΄μ.
μ°μ Database configuration μ λ€μκ³Ό κ°μ΄ application.properties μ update νλ€.
# JDBC properties
spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory
spring.datasource.username=username
spring.datasource.password=passwordκ·Έλ¦¬κ³ Database Design ν κ²μ κΈ°λ°μΌλ‘ Entity Class λ₯Ό μμ±νλ€. Employee.java μ λ€μκ³Ό κ°μ΄ μμ±νλ€.
@Entity
@Table(name="employee")
public class Employee {
// define fields
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String email;
// define constructors
...
// define getter/setter
...
// define toString
...
}μ΄λ κ² @Entity λ₯Ό μ¬μ©νμ¬ JPA κ° μμμ μ€μ DB μ employee table λ° κ° field μ mapping μ μννλλ‘ νλ©΄ entity class μ€μ μ λλ¬λ€. μ΄μ λ DAO λ₯Ό μν Interface μ κ·Έμ λν Implementation μ λ§λ€μ.
μ΄μ―€μμ λ€μ Boss κ° μμ²ν API Requirements λ₯Ό νμΈν΄λ³΄μ.
Create a RET API for the Employee Directory
REST clients should be able to
- get a list of employees
- get a single employee by id
- add a new employee
- update an employee
- delete an employee
requirements μ 첫 λ²μ§Έ μ¬νλΆν° νλμ© κ΅¬ννλ μμΌλ‘ μ§ννλ©΄ μ’μ κ² κ°λ€. λ€μ DAO λ‘ λμμμ, λͺ¨λ employee μ list λ₯Ό return ν μ μλ method λΆν° declare νκ³ , define νλ©΄ λ κ² κ°λ€.
EmployeeDAO.java interface μλ λ€μκ³Ό κ°μ΄ μμ±νκ³ ,
public interface EmployeeDAO {
List<Employee> findAll();
}μ΄λ₯Ό implement νλ EmployeeDAOImpl.java μλ λ€μκ³Ό κ°μ΄ μμ±νλ©΄ λ κ²μ΄λ€.
@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
// define field for entity manager
private EntityManager entityManager;
// set up constructor injection
public EmployeeDAOImpl(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public List<Employee> findAll() {
// create a query
TypedQuery<Employee> theQuery = entityManager.createQuery("from Employee", Employee.class);
return theQuery.getResultList();
}
}λ§μ§λ§μΌλ‘, ν΄λΉ DAO λ₯Ό Injection λ°μ μ¬μ©νλ REST Controller λ₯Ό μμ±νλ©΄ requirement νλμ Development Process κ° λμ΄ λλ€. EmployeeRestController.java μλ λ€μκ³Ό κ°μ΄ μμ±νλ€.
@RestController
@RequestMapping("/api")
public class EmployeeRestController {
private EmployeeDAO employeeDAO;
// quick and dirty: inject employee dao using constructor injection
public EmployeeRestController(EmployeeDAO employeeDAO) {
this.employeeDAO = employeeDAO;
}
// expose "/employees" and return a list of employees
@GetMapping("/employees")
public List<Employee> findAll() {
return employeeDAO.findAll();
}
}Potential Problem
κ·Έλ¬λ μ μ¬μ μΈ λ¬Έμ μ μ΄ λ³΄μ΄λ κ² κ°λ€. λ§μ½μ Buiness logic μ΄ μ‘°κΈ λ 볡μ‘ν΄μ Έμ νλμ method μ μ¬λ¬κ°μ DAO κ° μ¬μ©λκ³ , logic μ΄ λ³΅μ‘ν΄μ§λ©΄ Controller λ΄λΆμ code κ° λ¬΄νν λμ΄λ μ μ§λ³΄μμ μ΄λ €μμ΄ μκΈΈ κ²μ΄λ€.
κ·Έλ λ€λ©΄ Controller μ Buisness logic μ λΆλ¦¬μν¬ νμκ° μμ κ² κ°λ€.