-
SpringBoot | JPA, @Query를 이용하여 쿼리 작성하기▼ Backend/스프링 (Spring) 2021. 10. 25. 10:48반응형
▶ @Query, JPQL
JPQL(Java Persistence Query Language)는 JPA (Java Persistence API)의 일부로 정의된 플랫폼 독립적인 객체지향 쿼리 언어이다.
JPA에서 기본적으로 제공하는 메서드 외에 직접 쿼리를 작성할 경우에 주로 사용한다.
import
import org.springframework.transaction.annotation.Transactional; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param;
INSERT 삽입
public static final String INSERT_MENU = "INSERT INTO MENU (ID, MENU_NAME) VALUES (:menuId, :menuName)"; @Transactional @Modifying @Query(value = INSERT_MENU, nativeQuery = true) public int insertMenu(@Param("menuId") Long menuId, @Param("menuName") String menuName);
SELECT 조회
public static final String FIND_MENU = "SELECT ID, MENU_ID, MENU_NAME FROM MENU"; @Query(value = FIND_MENU, nativeQuery = true) public List<Menu> selectMenu(); public static final String FIND_MENU_ID = "SELECT ID, MENU_NAME FROM MENU WHERE MENU_ID=:menuId"; @Query(value = FIND_MENU_ID, nativeQuery = true) public Menu selectByMenuId(@Param("menuId") Long menuId);
UPDATE 수정
public static final String UPDATE_MENU = "UPDATE MENU SET MENU_NAME=:#{#menu.menuName} WHERE ID = :#{#menu.id}"; @Transactional @Modifying @Query(value = UPDATE_MENU, nativeQuery = true) public int updateMenu(@Param("Menu") Menu menu);
DELETE 삭제
public static final String DELETE_MENU = "DELETE FROM MENU WHERE ID =:menuId"; @Transactional @Modifying @Query(value = UPDATE_MENU, nativeQuery = true) public int deleteMenu(@Param("menuId") Long menuId);
반응형'▼ Backend > 스프링 (Spring)' 카테고리의 다른 글
Spring Boot | RESTful API 호출시 Cross Origin Requests Sharing (CORS) (0) 2021.11.19 Spring Boot | 로그인 구현하기 (Spring Security) (7) 2021.11.17 Spring Boot | JPA Error, Null return value from advice does not match primitive return (0) 2021.10.14 Spring Boot | JPA Error, Executing an update/delete query (0) 2021.10.14 Spring Boot | Gradle, Jar 빌드&배포하기 (3) 2021.10.08