[Spring JPA 실습 #8] Spring JPA @Query
- 📚 Spring/Spring JPA 실습
- 2020. 6. 16. 13:50
@Query에는 JPQL(객체 쿼리)을 이용한다.
JPQL은 쉽게 말해서 JPA에서 사용하는 Query Language이다.
package com.kyhslam.persistence;
import java.util.Collection;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import com.kyhslam.domain.Board;
public interface BoardRepository extends CrudRepository<Board, Long> {
@Query("SELECT b from Board b WHERE b.title LIKE %?1% AND b.bno > 0 order by b.bno DESC")
public List<Board> findTitle(String title);
}
@Query의 내용중에 '%?1%'는 눈여결 필요가 있다. '?'는 JDBC상에서 PreparedStatement에서 사용한 것과 동일하다고 생각하면 되고 '?1'은 첫 번째 전달되는 파라미터라고 생각하면 된다.
테스트 소스
package com.kyhslam;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import com.kyhslam.domain.Board;
import com.kyhslam.persistence.BoardRepository;
@RunWith(SpringRunner.class)
@SpringBootTest
public class boot3ApplicationTest {
@Autowired
private BoardRepository repo;
@Test
public void testQuery() {
repo.findTitle("17").forEach(board -> System.out.println(board.toString()));
}
}
결과
Hibernate: select board0_.bno as bno1_0_, board0_.content as content2_0_, board0_.regdate as regdate3_0_, board0_.title as title4_0_, board0_.updatedate as updatedate5_0_,
board0_.writer as writer6_0_ from tbl_boards board0_ where (board0_.title like ?) and board0_.bno>0 order by board0_.bno DESC
Board [bno=179, title=제목..179, writer=user09, content=내용....179 채우기 , regdate=2020-06-09 14:29:06.938, updatedate=2020-06-09 14:29:06.938]
Board [bno=178, title=제목..178, writer=user08, content=내용....178 채우기 , regdate=2020-06-09 14:29:06.92, updatedate=2020-06-09 14:29:06.92]
Board [bno=177, title=제목..177, writer=user07, content=내용....177 채우기 , regdate=2020-06-09 14:29:06.902, updatedate=2020-06-09 14:29:06.902]
Board [bno=176, title=제목..176, writer=user06, content=내용....176 채우기 , regdate=2020-06-09 14:29:06.886, updatedate=2020-06-09 14:29:06.886]
Board [bno=175, title=제목..175, writer=user05, content=내용....175 채우기 , regdate=2020-06-09 14:29:06.871, updatedate=2020-06-09 14:29:06.871]
Board [bno=174, title=제목..174, writer=user04, content=내용....174 채우기 , regdate=2020-06-09 14:29:06.856, updatedate=2020-06-09 14:29:06.856]
Board [bno=173, title=제목..173, writer=user03, content=내용....173 채우기 , regdate=2020-06-09 14:29:06.839, updatedate=2020-06-09 14:29:06.839]
Board [bno=172, title=제목..172, writer=user02, content=내용....172 채우기 , regdate=2020-06-09 14:29:06.818, updatedate=2020-06-09 14:29:06.818]
Board [bno=171, title=제목..171, writer=user01, content=내용....171 채우기 , regdate=2020-06-09 14:29:06.801, updatedate=2020-06-09 14:29:06.801]
Board [bno=170, title=제목..170, writer=user00, content=내용....170 채우기 , regdate=2020-06-09 14:29:06.785, updatedate=2020-06-09 14:29:06.785]
Board [bno=117, title=제목..117, writer=user07, content=내용....117 채우기 , regdate=2020-06-09 14:29:05.699, updatedate=2020-06-09 14:29:05.699]
Board [bno=17, title=제목..17, writer=user07, content=내용....17 채우기 , regdate=2020-06-09 14:29:03.908, updatedate=2020-06-09 14:29:03.908]
'📚 Spring > Spring JPA 실습' 카테고리의 다른 글
[Spring JPA 실습 #1] 실습을 위한 도메인 설계 및 정의 (0) | 2021.03.04 |
---|---|
엔티티 설계시 주의점 (0) | 2020.08.16 |
7. Spring Data JPA : 데이터 조회 Page<T> (0) | 2020.06.15 |
6. Spring Data JPA : 데이터 조회 페이징 (Pageable) (0) | 2020.06.15 |
5. Spring Data JPA : 데이터 조회 (쿼리 메소드) (0) | 2020.06.09 |