Spring boot 튜토리얼 (2) - CRUD

임쿠쿠·2022년 1월 30일
0

SpringBoot

목록 보기
2/6
post-thumbnail

4. GetMapping (Fetching data form DB)

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();
    }
}

5. Fetching Data by ID

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();
    }
}

6. Deleting data from DB

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);
    }
}

7. Updating data to DB

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);
    }
}

8. Fetching data by property name

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

profile
Pay it forward

0개의 댓글