Controller
@RestController
public class DepartmentController {
...
@GetMapping("/departments")
public List<Department> fetchDepartmentList() {
return departmentService.fetchDepartmentList();
}
}
Service
public interface DepartmentService {
...
List<Department> fetchDepartmentList();
}
ServiceImpl
public interface DepartmentService {
...
@Override
public List<Department> fetchDepartmentList() {
return departmentRepository.findAll();
}
}
Controller
@RestController
public class DepartmentController {
...
@GetMapping("/departments/{id}")
public Department fetchDepartmentById(@PathVariable("id") Long departmentId) {
return departmentService.fetchDepartmentById(departmentId);
}
}
Service
public interface DepartmentService {
...
public Department fetchDepartmentById(Long departmentId);
}
ServiceImpl
public interface DepartmentService {
...
@Override
public Department fetchDepartmentById(Long departmentId) {
return departmentRepository.findById(departmentId).get();
}
}
Controller
@RestController
public class DepartmentController {
...
@DeleteMapping("/departments/{id}")
public String deleteDepartmentById(@PathVariable("id") Long departmentId) {
departmentService.deleteDepartmentById(departmentId);
return "department deleted successfully!!";
}
}
Service
public interface DepartmentService {
...
public void deleteDepartmentById(Long departmentId);
}
ServiceImpl
public interface DepartmentService {
...
@Override
public void deleteDepartmentById(Long departmentId) {
departmentRepository.deleteById(departmentId);
}
}
Controller
@RestController
public class DepartmentController {
...
@PutMapping("/departments/{id}")
public Department updateDepartment(@PathVariable("id") Long departmentId,
@RequestBody Department department) {
return departmentService.updateDepartment(departmentId, department);
}
}
Service
public interface DepartmentService {
...
public Department updateDepartment(Long departmentId, Department department);
}
ServiceImpl
public interface DepartmentService {
...
@Override
public Department updateDepartment(Long departmentId, Department department) {
Department depDB = departmentRepository.findById(departmentId).get();
// not null && not blank
if(Objects.nonNull(department.getDepartmentName()) &&
!"".equalsIgnoreCase(department.getDepartmentName())) {
depDB.setDepartmentName(department.getDepartmentName());
}
if(Objects.nonNull(department.getDepartmentCode()) &&
!"".equalsIgnoreCase(department.getDepartmentCode())) {
depDB.setDepartmentName(department.getDepartmentCode());
}
if(Objects.nonNull(department.getDepartmentAddress()) &&
!"".equalsIgnoreCase(department.getDepartmentAddress())) {
depDB.setDepartmentName(department.getDepartmentAddress());
}
return departmentRepository.save(depDB);
}
}
Controller
@RestController
public class DepartmentController {
...
@GetMapping("/departments/name/{name}")
public Department fetchDepartmentByName(@PathVariable("name") String departmentName) {
return departmentService.fetchDepartmentByName(departmentName);
}
}
Service
public interface DepartmentService {
...
public Department fetchDepartmentByName(String departmentName);
}
Repository
@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {
public Department findByDepartmentName(String departmentName);
// findByDepartmentNameIgnoreCase 사용 시 대소문자 구분 x
public Department findByDepartmentNameIgnoreCase(String departmentName);
}
참고 - https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
ServiceImpl
public interface DepartmentService {
...
@Override
public Department fetchDepartmentByName(String departmentName) {
return departmentRepository.findByDepartmentName(departmentName);
}
}
참고 - Spring Boot Tutorial | Full In-depth Course / Daily Code Buffer