5. Spring Data JPA : 데이터 조회 (쿼리 메소드)

  • 쿼리 메소드는 스프링데이터 JPA의 핵심적인 기능 중 하나로 메소드 이름으로 쿼리를 생성할 수 있다는 장점이 있다.

  • 메소드 이름으로 쿼리를 생성하기 위해 인터페이스에서 사용할 사용자 쿼리 메소드를 정의해 준다.

  • 방식 : Collection<T> findBy + 속성이름(속성타입)

기존의 Board 를 활용한 데이터 조회 예제

  • fidBy를 이용한 컬럼 조회
  • like 구문
  • and or 처리
  • 부등호
  • order by
package com.kyhslam.persistence;

import java.util.Collection;
import java.util.List;

import org.springframework.data.repository.CrudRepository;

import com.kyhslam.domain.Board;

public interface BoardRepository extends CrudRepository<Board, Long> {

    public List<Board> findBoardByTitle(String title);

    public Collection<Board> findByWriter(String writer);

    // like 
    public Collection<Board> findByWriterContaining(String writer);

    // or
    public Collection<Board> findByTitleContainingOrContentContaining(String title, String content);

    // select * from tbl_boards where title like '%?%' and bno > ?;
    public Collection<Board> findByTitleContainingAndBnoGreaterThan(String keyword, Long num);

    // select * from tbl_boards where bno > ? ORDER BY bno DESC;
    public Collection<Board> findByBnoGreaterThanOrderByBnoDesc(Long bno);

}

테스트 소스

boot3ApplicationTest.java

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.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 testInsert200() {

        for(int i=1; i <= 200; i++) {
            Board board = new Board();
            board.setTitle("제목.." + i);
            board.setContent("내용...." +  i + " 채우기 ");
            board.setWriter("user0" + (i%10) );
            repo.save(board);
        }
    }


    //@Test
    public void testByTitle() {

        // before java 8
        /*
         * List<Board> list = repo.findBoardByTitle("제목..177");
         * 
         * for(int i=0; i < list.size(); i++) { System.out.println(list.get(i)); }
         */


        // java 8
        repo.findBoardByTitle("제목..177").forEach(board -> System.out.println(board));
    }


    //@Test
    public void testByWriter() {
        Collection<Board> results = repo.findByWriter("user00");

        results.forEach(board -> System.out.println(board));
    }

    //@Test
    public void testByWriterContaining() {
        Collection<Board>  results = repo.findByWriterContaining("05");

        results.forEach(board -> System.out.println(board.getBno()));
    }


    //@Test
    public void testByTitleAndBno() {
        Collection<Board> results = repo.findByTitleContainingAndBnoGreaterThan("5", 50L);

        results.forEach(board -> System.out.println(board.toString()));
    }

    @Test
    public void testBnoOrderBy() {
        Collection<Board> results = repo.findByBnoGreaterThanOrderByBnoDesc(90L);

        results.forEach(board -> System.out.println(board.toString()));
    }

}

댓글

Designed by JB FACTORY