[개발] (1/2) Item Entity, Repository, Test 실습 Querydsl
필기자
2022-09-13 15:53
6,538
0
본문
Item Entity, Repository, Test 실습 Querydsl
pom.xml 파일 추가
<!--querydsl 의존성 추가-->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>5.0.0</version>
</dependency>
<!--querydsl 의존성 추가-->
<!--querydsl 플러그인 설정-->
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
<!--querydsl 플러그인 설정-->
오른쪽 Maven 탭 > Lifecycle > compile 클릭
왼쪽 Project > target/generated-sources/java/kr/hull/shop/entity/QItem.java 파일 생성확인
Flie > Project Structure(ctrl+alt+shift+s) > Modules > target > generated-sources 선택 후 상단 Sources 탭 클릭(다른탭은 해제)
src/test/java/kr/hull/shop/repository/ItemRepositoryTest.java 다음 내용 추가(이어 쓰기)
@PersistenceContext
EntityManager em;
@Test
@DisplayName("Querydsl 조회 확인")
public void queryDslTest(){
this.createItemList();
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
QItem qItem = QItem.item;
JPAQuery<Item> query = queryFactory.selectFrom(qItem)
.where(qItem.itemSellStatus.eq(ItemSellStatus.SELL))
.where(qItem.itemDetail.like("%" + "좋은 상품 설명" + "%"))
.orderBy(qItem.price.desc());
List<Item> itemList = query.fetch();
for(Item item : itemList){
System.out.println(item.toString());
}
}
결과 QUERY
select
item0_.item_id as item_id1_0_,
item0_.item_detail as item_det2_0_,
item0_.item_nm as item_nm3_0_,
item0_.item_sell_status as item_sel4_0_,
item0_.price as price5_0_,
item0_.reg_time as reg_time6_0_,
item0_.stock_number as stock_nu7_0_,
item0_.update_time as update_t8_0_
from
item item0_
where
item0_.item_sell_status='SELL'
and (
item0_.item_detail like '%좋은 상품 설명%' escape '!'
)
order by
item0_.price desc
결과
댓글목록0