JPQL 예제

  • JPQL 할게 많으니 따로 공부!
// JpaRepository<엔티티클래스명, Id타입> 를 상속받는 interface를 정의한다.
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
    // 사원의 이름(first, last), 이메일 정보
    @Query("SELECT e FROM Employee e")
    List<Employee> getEmployeeAll();

    // 특정부서(이름) 에 속한 사원의 이름을 출력하시오.
    @Query("SELECT e FROM Employee e join fetch e.department d WHERE d.departmentName = :name")
    List<Employee> getEmployeeJoinDepartment(@Param("name") String name);

    //사원의 이름(first_name, last_name)을 출력하세요. 1page가 3건씩 보여집니다.
    // 특정부서(이름) 에 속한 사원의 이름을 출력하시오.
    @Query("SELECT e FROM Employee e WHERE e.department.departmentName = :departmentName ORDER BY e.firstName")
    public Page<Employee> getEmployeeByDepartmentName(@Param("departmentName") String departmentName, Pageable pageable);

    //사원이름으로 like 검색한 검색결과 출력
    @Query("select e from Employee e WHERE e.firstName Like CONCAT(UPPER(:name),'%') ORDER BY e.firstName")
    public Page<Employee> getEmployeeByFirstName(@Param("name") String name, Pageable pageable);
}
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
public class EmployeeRepositoryTest {
    @Autowired
    EmployeeRepository employeeRepository; // test할 대상

    @Test
    public void initTest(){
        // 아무런 코드가 없음. 에러가 안난다면 설정은 잘되어 있다는 뜻.
    }

    @Test
    public void findAll() throws Exception{
        List<Employee> all = employeeRepository.getEmployeeAll();
        for(Employee employee : all){
            System.out.println("Name : " + employee.getFirstName()+ " " + employee.getLastName());
            System.out.println("E-mail : " + employee.getEmail());
        }
    }

    @Test
    public void employeeJoinDepartment() throws Exception{
        List<Employee> all = employeeRepository.getEmployeeJoinDepartment("IT");
        System.out.println("IT부서에 일하는 사람");
        for(Employee employee : all){
            System.out.println("Name : " + employee.getFirstName()+ " " + employee.getLastName());
            System.out.println("E-mail : " + employee.getEmail());
        }
    }

    @Test
    public void employeeJoinDepartmentPage() throws Exception{
        Pageable page = PageRequest.of(1, 3);
        Page<Employee> all = employeeRepository.getEmployeeByDepartmentName("Sales", page);
        System.out.println("Sales부서에 일하는 사람");
        System.out.println("전체 건수 : " + all.getTotalElements());
        for(Employee employee : all){
            System.out.println("Name : " + employee.getFirstName()+ " " + employee.getLastName());
            System.out.println("E-mail : " + employee.getEmail());
        }
    }

    @Test
    public void employeeByFirstName() throws Exception{
        Pageable page = PageRequest.of(1, 3);
        Page<Employee> all = employeeRepository.getEmployeeByFirstName("A", page);
        System.out.println("A로 시작하는 사람");
        System.out.println("전체 건수 : " + all.getTotalElements());
        System.out.println("페이지 : " + all.getTotalPages());
        System.out.println("현재 건수 : " + all.getNumberOfElements());
        for(Employee employee : all){
            System.out.println("Name : " + employee.getFirstName()+ " " + employee.getLastName());
            System.out.println("E-mail : " + employee.getEmail());
        }
    }
}