-
Notifications
You must be signed in to change notification settings - Fork 0
06) helloSpringDataJpa
choi jae ho edited this page Jun 2, 2021
·
13 revisions
- ๋ค๋ฅธ entity ๋ฅผ ๋ง๋ค๋ ์ ์ฌํ ์ฝ๋์ ๋ฐ๋ณต.
- DAO ํจํด์ด ๊ฐ๋ค๋ ์ .
- ์ฝ๋์ ๋ฐ๋ณต
- entity type , primary key๋ง ๋ฐ๊ฟ์ฃผ๋ฉด ๋ค๋ฅธ entity ๋ ๋ค๋ฅผ ๊ฒ์ด ์๋ค.
- ํ์ ๋ฐํ์๋ DAO code๋ฅผ ์ต์ํ,
- DAO๋ฅผ ์ค์ ๋ก ๋ง๋ค์ด์ค ํ์๊ฐ ์๋ค
- Spring DATA JPA ๋ repository interfaces ๋ฅผ ์ ๊ณตํ๋ค.
-
- CrudRepository
-
- PagingAndSortingRepository
-
- JpaRepository
- id๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํด์ ์กฐํํ๊ธฐ ๋ณด๋ค๋, ๊ฒ์์ ํตํด์ ์กฐํํ ๊ฒฝ์ฐ๊ฐ ์๋ค.
- ๊ทธ ๋ถ๋ถ์ ๋ํด์๋ ์ถ๊ฐ์ ์ผ๋ก ๊ตฌํํด์ผ ํ๋ค.
- Spring Data JPA ๋ CRUD operations๋ฅผ ์ ๊ณตํ๋ค.
- method names์ ๋ฐ๋ผ ๋์ ์ผ๋ก ์ฟผ๋ฆฌ๋ฅผ ์ ๊ณตํ๋ค.
ex)
- User findByEmail(String email), ์ด๋ฉ์ผ์ ๊ธฐ์ค์ผ๋ก ์กฐํํ๋ ๋ฉ์๋ ๋ด๋ถ์ ์ผ๋ก SQL ์๋์ ์ผ๋ก ๋ง๋ค์ด์ง๋ค. ์ด๋ฆ์ ๋ฐํ์ผ๋ก ๋ฉ์๋๋ฅผ ์๋์ผ๋ก ๋ง๋ค์ด์ค๋ค๋์ .
- User findByEmailAndPassword(String email, String password) ๋ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ง๋ค์ด์ค๋ค.
- return ํ์ ์ด ํ๋ ์ด์์ด๋ฉด, List or Page
- find By + ๋ณ์์ด๋ฆ ์ผ๋ก ๋ฃ์ด ์ฃผ๋ฉด ์๋์์ฑ๋๋ค.
- ํค์๋๋ฅผ ์ฌ์ฉ, ๊ฒ์ํ ๋ ์ฐธ์กฐ
1) ๋ค์๊ฐ์ ์กฐ๊ฑด ๊ธฐ์ , ๊ฒ์ํ์์ title ํน์ content์์ ํน์ ๋จ์ด๊ฐ ํฌํจ๋ ๊ธ ๋ชฉ๋ก ์กฐํ
public interface BoardRepository extends JpaRepositoty<Board, Long> {
List<Board> findByTitleContainingOrContentContaining(String title, String content);
}
@SpringBootTest
public class QueryMethodTest {
@Autowired
private BoardRepository repo;
@Test
public void testFindByTitleContainingOrContentContaining() {
List<Board> boardList = repo.findByTitleContainingOrContentContaining(โ17โ, โ17โ);
for(Board board: boardList) { System.out.println(โ๏ โ + board.toString() )};
}
}
2) Pagination, ๊ฒ์ํ์์ title๋ณ์์ โํ์ฑ๋โ ๋ผ๋ ๊ฒ์์ด๊ฐ ํฌํจ๋ ๊ฒ์๊ธ ๋ชฉ๋ก ๊ฒ์ ํ์ด์ง ๋จ์๋ก ์กฐํ
public interface BoardRepository extends JpaRepositoty<Board, Long> {
List<Board> findByTitleContaining(String searchKeyword, Pageable paging);
}
@Test
public void testFindByTitleContaining() {
Pageable paging = PageRequest.of(0, 5);
List<Board> boardList= repo.findByTitleContaining(โํ์ฑ๋โ, paging);
for(Board board: boardList) {
System.out.println(โ->โ + board.toString() );
}
}
3) Pagination and Sort, ๊ฒ์ํ์์ title๋ณ์์ โํ์ฑ๋โ์ด๋ผ๋ ๊ฒ์์ด๊ฐ ํฌํจ๋ ๊ฒ์๊ธ ๋ชฉ๋ก ๊ฒ์ โseqโ ๋ณ์์ ๋ฐ๋ผ ๋ด๋ฆผ ์ฐจ์์ผ๋ก ์ ๋ ฌ
public interface BoardRepository extends JpaRepositoty<Board, Long> {
List<Board> findByTitleContaining(String searchKeyword, Pageable paging);
}
@Test
public void testFindByTitleContaining( ) {
Pageable paging = PageRequest.of(0,5, Sort.Direction.DESC, โseqโ);
List<Board> boardList= repo.findByTitleContaining(โํ์ฑ๋โ, paging);
for(Board board: boardList) {
System.out.println(โ->โ + board.toString() );
}
5) Return Type: List -> Page , Page ๊ฐ์ฒด๋ ํ์ด์ง ์ฒ๋ฆฌํ ๋ ๋ค์ํ ์ ๋ณด๋ฅผ ์ถ๊ฐ๋ก ์ ๊ณต
public interface BoardRepository extends CrudRepositoty<Board, Long> {
Page<Board> findByTitleContaining(String searchKeyword, Pageable paging);
}
@Test
public void testFindByTitleContaining() {
Pageable paging = PageRequest.of(0,5, Sort.Direction.DESC, โseqโ);
Page<Board> pageInfo= repo.findByTitleContaining(โํ์ฑ๋โ, paging);
System.out.println(โPage size: โ + pageInfo.getSize() );
System.out.println(โTotal Pages: โ + pageInfo.getTotalPages() );
System.out.println(โTotal Count: โ + pageInfo.getTotalElements() );
List<Board> boardList=pageInfo.getContent();
for(Board board: boardList) {
System.out.println(โ->โ + board.toString() );
}
}
- method ์ด๋ฆ์ด ๋๋ฌด ๋ณต์กํ๊ฑฐ๋, ์ง์ ์ฟผ๋ฆฌ๋ฅผ ๋ช ์์ ์ผ๋ก ์ค์ ํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ค.
@Query("select u from User u where u.email=?1 and u.password=?2 and u.enabled=true")
User findByEmailAndPassword(String email, String password);
public interface BoardRepository extends JpaRepositoty<Board, Long> {
//entity์ ํ๋๋ค์ ๋์๋ฌธ์๋ฅผ ์ ๊ฒฝ์ด๋ค.
@Query(โSELECT b From Board b โ
+ โWHERE b.title like %?1% ORDER BY b.seq DESCโ)
List<Board> queryAnnotationTest1(String searchKeyword);
}
@Test
Public void QueryAnnotationTest() {
List<Board> boardList=repo.queryAnnotationTest1(โํ์ฑ๋โ);
for(Board board: boardList) {
System.out.println(โ->โ + board.toString() );
}
}
//์ด๋ฆ์ ๊ธฐ๋ฐํด์ ๋ฃ๋๋ค.
public interface BoardRepository extends CrudRepositoty<Board, Long> {
@Query(โSELECT b From Board b WHERE b.title like %:searchKeyWord% โ
+ โORDER BY b.seq DESCโ)
List<Board> queryAnnotationTest1(@Param(โsearchKeywordโ)String searchKeyword);
}
public interface BoardRepository extends JpaRepositoty<Board, Long> {
@Query(value=โselect seq, title from board โ
+ โwhere title like โ%โ || ?1 โ%โ โ
+ โorder by seq decโ, nativeQuery=true);
List<Object[]> queryAnnotationTest2(String searchKeyword);
}