[Spring JPA 실습 #8] Spring JPA @Query

 

 

 

@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]

댓글

Designed by JB FACTORY