- 전체
- JAVA 일반
- JAVA 수학
- JAVA 그래픽
- JAVA 자료구조
- JAVA 인공지능
- JAVA 인터넷
- Java Framework
- Java GUI (AWT,SWING,SWT,JFACE)
- SWT and RCP (web RAP/RWT)[eclipse], EMF
JAVA 자료구조 [JPA] 쿼리메서드 : 쿼리 연습 조회(findBy..) , 페이징처리
2019.03.24 14:40
[JPA] 쿼리메서드 : 쿼리 연습 조회(findBy..) , 페이징처리
[JPA] 쿼리메서드 : 쿼리 연습 조회(findBy..)
JPA에서 사용하는 Named Query나 JPQL(Java Persistence Query Language), Query dsl 학습이 필요
* 쿼리 메소드 이용
- Spring Data JPA는 메소드의 이름만으로 원하는 질의(query)를 실행, 단 SELECT에 해당
- find .. By 쿼리 메소드를 작성시 find뒤에 엔티티 타입지정, By뒤엔 칼럼명;
ex) 'findBoardByTitle' >> Board 테이블의 Title 칼럼
ex) 'findBoardByTitle("제목177")
- 리턴 타입은 Page<T>, Slice<T>, List<T> 와 같은 Collection<T>형태
- findBy..로시작하는 쿼리 메소드는 지정하는 속상의 값에 따라 파라미터 타입이 결정된다.
|
Keyword
|
Sample
|
JPQL snippet
|
|
And
|
findByLastnameAndFirstname
|
where x.lastname=?1 and x.firstname=?2
|
|
Or
|
findByLastnameOrFirstname
|
where x.lastname=?1 or x.firstname=2
|
|
Between
|
findByStartDateBetween
|
where x.startDate between 1? and ?2
|
|
LessThan
|
findByAgeLessThan
|
where x.age < ?1
|
|
GreaterThan
|
findByAgeGreaterThan
|
where x.age > ?1
|
|
IsNull
|
findByAgeIsNull
|
where x.age is null
|
|
IsNotNull, NotNull
|
findByAge(Is)NotNull
|
where x.age not null
|
|
Like
|
findByFirstnameLike
|
where x.firstname like ?1
|
|
NotLike
|
findByFirstnameNotLike
|
where x.firstname not like ?1
|
|
StartingWith
|
findByFirstnameStartingWith
|
where x.firstname like ?1 (parameter bound with appended %)
|
|
EndingWith
|
findByFirstnameEndingWith
|
where x.firstname like ?1 (parameter bound wrappend in %)
|
|
OrderBy
|
findByAgeOrderByLastnameDesc
|
where x.age = ?1 order by x.lastname desc
|
|
Not
|
findByLastnameNot
|
where x.lastname <> ?1
|
|
In
|
findByAgeIn(Collection<Age>ages)
|
where x.age in ?1
|
|
NotIn
|
findByAgeNotIn(Collection <Age>age)
|
where x.age not in ?1
|
|
True
|
findByAcitiveTrue()
|
where x.active = true
|
|
False
|
findByActiveFalse()
|
where x.active = false
|
* like 구문 처리
① 단순한 like >> Like
② 키워드 + '%' >> StartingWith
③ '%' + 키워드 >> EndingWith
④ '%' + 키워드 + '%' >> Containing
ex) '05' 라는 문자 게시글 검색
- Repository Interface ▶ public Collection<Board> findByTitleContaining(String title);
- 호출 ▶ Collection<Board> results = repo.findByTitleContaining("05");
- 결과 ▶ 타이틀(Title) 칼럼에서 '05'가 포함(Containing)된 데이터 조회
- SQL ▶ WHERE title LIKE '%05%';
*and 혹은 or 조건 처리
속성이 두개 이상일때는 파라미터 역시 속성의 수만큼 맞춤
- public Collection<Board> findByTitleContatiningOrContentContaining(String title, String content);
- 호출 ▶ Collection<Board> results = repo.findByTitleContaining("05","a");
- SQL ▶ WHERE title LIKE '%05%' OR title '%a%' ;
*부등호 처리
- Repository Interface ▶ public Collection<Board> findByTitleContatiningAndBnoGreaterThan(String keywoard,Long num);
- 호출 ▶ Collection<Board> results = repo.findByTitleContatiningAndBnoGreaterThan("5",50L);
- 결과 ▶ 제목(Title)에 '5' 가 포함(Containing)되어있고 번호(Bno)가 50보다 큰(GreaterThan) 데이터 조회
- SQL ▶ WHERE title LIKE '%5%' AND bno > 50
*order by처리
- Asc
- Desc (역순)
- Repository Interface ▶ public Collection<Board> findByBnoGreaterThanOrderByBnoDesc(Long num);
- 호출 ▶ Collection<Board> results = repo.findByBnoGreaterThanOrderByBnoDesc(90L);
- 결과 ▶ 번호(Bno)가 90보다 큰(GreaterThan) 데이터를 번호(Bno)기준으로 역순(Desc)으로 정렬(OrderBy)
- SQL ▶ WHERE bno>90 ORDER BY bno DESC
: 쿼리메소드들은 마지막 파라미터로 Pageable 인터페이스, Sort 인터페이스 를 사용할수 있다.
* Pageable 인터페이스 ‥ 페이지처리 / Sort 인터페이스 ‥ 정렬
- org.springframework.data.domain.Pageable 인터페이스를 구현한 클래스 중 PageRequest 클래스 이용
[주의]
PageRequestd 의 경우 스프링부트의 버전에 주의
2.0 >> new PageRequest()는 deprecated 때문에 사용하면 안되고, PageRequest.of()를 사용
▼ PageRequest의 생성자
|
생성자
|
설명
|
|
PageRequest (int page, int size)
|
페이지번호, 페이지당 데이터수
|
|
PageRequest(int page, int size
, Sort.Direction direction, String .. props) |
페이지번호, 페이지당 데이터수, 정렬방향, 속성(칼럼)들
|
|
PageRequest (int page, int size, Sort sort)
|
페이지번호, 페이지당 데이터수, 정렬 방향
|
- Console에 나온 SQL문 >> .. where board0_.title like ? limit ?
- MySQL 이기 때문에 자동 limit가 적용
- 1번 방법 (페이징)
Pageable paging = new PageRequest(0,10);
SQL >> where board0_.title like ? limit ?
- 2번 방법 (페이징+정렬)
Pageable paging = new PageRequest(0,10,Sort.Direction.ASC,"bno");
SQL >> where board0_.title like ? order by board0_.bno asc limit ?
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 6 |
java4autocad Java for Autocad
| 졸리운_곰 | 2017.04.26 | 387 |
| 5 | Java3D and Eclipse | 졸리운_곰 | 2015.08.12 | 317 |
| 4 |
Draw2d Intro
| 졸리운_곰 | 2015.07.29 | 514 |
| 3 |
FreeLayout: A New Java Layout
| 졸리운_곰 | 2015.05.14 | 315 |
| 2 |
XML file to Treeviewer
| 졸리운_곰 | 2015.03.19 | 398 |
| 1 |
Java GUI(AWT) 프로그래밍
| 졸리운_곰 | 2015.03.09 | 938 |

