Skip to content

06) helloSpringDataJpa

choi jae ho edited this page Jun 2, 2021 · 13 revisions

πŸ“‹ Spring Data JPA

1. Creating DAO

문제

  • λ‹€λ₯Έ entity λ₯Ό λ§Œλ“€λ•Œ μœ μ‚¬ν•œ μ½”λ“œμ˜ 반볡.
  • DAO νŒ¨ν„΄μ΄ κ°™λ‹€λŠ” 점.
  • μ½”λ“œμ˜ 반볡 image
  • entity type , primary key만 λ°”κΏ”μ£Όλ©΄ λ‹€λ₯Έ entity 도 λ‹€λ₯Ό 것이 μ—†λ‹€.

2. Spring Data JPA

μž₯점

  • 틀에 λ°•ν˜€μžˆλŠ” DAO codeλ₯Ό μ΅œμ†Œν™”,
  • DAOλ₯Ό μ‹€μ œλ‘œ λ§Œλ“€μ–΄μ€„ ν•„μš”κ°€ μ—†λ‹€

μ œκ³΅λ˜λŠ” μΈν„°νŽ˜μ΄μŠ€

image

  • Spring DATA JPA λŠ” repository interfaces λ₯Ό μ œκ³΅ν•œλ‹€.
    1. CrudRepository
    1. PagingAndSortingRepository
    1. JpaRepository

4. Query Method

  • idλ₯Ό 기반으둜 ν•΄μ„œ μ‘°νšŒν•˜κΈ° λ³΄λ‹€λŠ”, 검색을 ν†΅ν•΄μ„œ μ‘°νšŒν•  κ²½μš°κ°€ μžˆλ‹€.
  • κ·Έ 뢀뢄에 λŒ€ν•΄μ„œλŠ” μΆ”κ°€μ μœΌλ‘œ κ΅¬ν˜„ν•΄μ•Ό ν•œλ‹€.
  • Spring Data JPA λŠ” CRUD operationsλ₯Ό μ œκ³΅ν•œλ‹€.
  • method names에 따라 λ™μ μœΌλ‘œ 쿼리λ₯Ό μ œκ³΅ν•œλ‹€.

ex)

  • User findByEmail(String email), 이메일을 κΈ°μ€€μœΌλ‘œ μ‘°νšŒν•˜λŠ” λ©”μ„œλ“œ λ‚΄λΆ€μ μœΌλ‘œ SQL μžλ™μ μœΌλ‘œ λ§Œλ“€μ–΄μ§„λ‹€. 이름을 λ°”νƒ•μœΌλ‘œ λ©”μ„œλ“œλ₯Ό μžλ™μœΌλ‘œ λ§Œλ“€μ–΄μ€€λ‹€λŠ”μ .
  • User findByEmailAndPassword(String email, String password) 도 λ§ˆμ°¬κ°€μ§€λ‘œ λ§Œλ“€μ–΄μ€€λ‹€.
  • return νƒ€μž…μ΄ ν•˜λ‚˜ 이상이면, List or Page

image

  • find By + λ³€μˆ˜μ΄λ¦„ 으둜 λ„£μ–΄ μ£Όλ©΄ μžλ™μƒμ„±λœλ‹€.

μ°Έμ‘°

image

  • ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©, κ²€μƒ‰ν• λ•Œ μ°Έμ‘°

JPA vs Spring Data JPA

image

μ˜ˆμ‹œ

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() );
      }
}