dependencies {
// Hibernate
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("mysql:mysql-connector-java")
...
}
spring:
datasource:
url: jdbc:mysql://<host>:<port>/<dbname>
username: <username>
password: <password>
jpa:
hibernate:
ddl-auto: update # 운영시에는 validate 추천
show-sql: true
@Entity
@Table(name="employee")
@Getter
@Setter
public class Employee {
@id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name", length = 30, nullable = false)
private String name;
}
<property name="hibernate.hbm2ddl.auto" value="create"/>
uniqueConstraints 속성 : DDL 생성 시에 유니크 제약조건을 만듦
value 속성값 더보기value
Board board = new Board();
board.setId("board1");
em.persist(board);
@Entity
@SequenceGenerator(
name = "BOARD_SEQ_GENERATOR",
sequenceName = "BOARD_SEQ", // 실제 DB의 Sequence Name
initialValue = 1,
allocationSize = 1 )
public class Board {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BOARD_SEQ_GENERATOR")
private Long id;
}
@Entity
@TableGenerator(
name = "BOARD_SEQ_GENERATOR",
table = "MY_SEQUENCE", // 실제 DB의 Table name
pkColumnValue = "BOARD_SEQ",
allocationSize = 1 )
public class Board {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "BOARD_SEQ_GENERATOR")
private Long id;
}
@Controller
public class EmployeeController {
private EmployeeService employeeService;
@Autowired
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@PostMapping("/employee")
public @ResponseBody Result addEmployee(@RequestBody Employee employee) {
return employeeService.addEmployee(employee);
}
@GetMapping("/employees")
public @ResponseBody Result getEmployees() {
return employeeService.getEmployees();
}
@GetMapping("/employees/{id}")
public @ResponseBody Result getEmployeeById(@PathVariable Long id) {
return employeeService.getEmployeeById(id);
}
@PutMapping("/employees/{id}")
public @ResponseBody Result updateEmployeeById(@PathVariable Long id, @RequestBody Employee employee) {
return employeeService.updateEmployeeById(id, employee);
}
@DeleteMapping("/employees/{id}")
public @ResponseBody Result deleteEmployeeById(@PathVariable Long id) {
return employeeService.deleteEmployeeById(id);
}
}
@Service
public class EmployeeServiceImplement implements EmployeeService {
private EmployeeRepository employeeRepository;
@Autowired
public EmployeeServiceImplement(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@Override
public Result addEmployee(Employee employee) {
employeeRepository.save(employee);
return new Result(ResultCode.OK);
}
@Override
public Result getEmployees() {
Result result = new Result(ResultCode.OK);
result.setData(employeeRepository.findAll());
return result;
}
@Override
public Result getEmployeeById(Long id) {
Result result = new Result(ResultCode.OK);
result.setData(employeeRepository.findEmployeeById(id));
return result;
}
@Override
public Result updateEmployeeById(Long id, Employee employee) {
employee.setId(id);
employeeRepository.save(employee);
return new Result(ResultCode.OK);
}
@Override
public Result deleteEmployeeById(Long id) {
employeeRepository.deleteById(id);
return new Result(ResultCode.OK);
}
}
@Transactional
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
Employee findEmployeeById(Long id);
List<Employee> findEmployeeByName(String name);
}