티스토리 뷰

안녕하세요.

이전에 만든 Entity를 CompileQuerydsl 하여 QType Class를 만들어줍니다.

  • QuerydslBasicTest.java

    @SpringBootTest
    @Transactional
    public class QuerydslBasicTest {
    
      @Autowired
      EntityManager em;
    
      JPAQueryFactory queryFactory;
    
      @BeforeEach
      public void before() {
          queryFactory = new JPAQueryFactory(em); // 동시성 문제 해결됩니다.
    
          Team teamA = new Team("TEAM A");
          Team teamB = new Team("TEAM B");
          em.persist(teamA);
          em.persist(teamB);
    
          Member member1 = new Member("member1", 10, teamA);
          Member member2 = new Member("member2", 20, teamA);
    
          Member member3 = new Member("member3", 30, teamB);
          Member member4 = new Member("member4", 40, teamB);
    
          em.persist(member1);
          em.persist(member2);
          em.persist(member3);
          em.persist(member4);
      }
    
      @Test
      public void startJPQL() throws Exception {
    
          String query = "select m from Member m where m.username = :username";
          Member findMember = em.createQuery(query, Member.class)
                  .setParameter("username", "member1")
                  .getSingleResult();
    
          assertThat(findMember.getUsername()).isEqualTo("member1");
      }
    
      @Test
      public void startQuerydsl() throws Exception {
    
          QMember m = new QMember("m");
    
          Member findMember = queryFactory.selectFrom(m)
                  .where(m.username.eq("member1"))
                  .fetchOne();
    
          assertThat(findMember.getUsername()).isEqualTo("member1");
      }
    }

EntityManager 로 JPAQueryFactory 생성
Querydsl은 JPQL 빌더
JPQL: 문자(실행 시점 오류), Querydsl: 코드(컴파일 시점 오류)
JPQL: 파라미터 바인딩 직접, Querydsl: 파라미터 바인딩 자동 처리

감사합니다.

참고

www.inflearn.com/course/Querydsl-%EC%8B%A4%EC%A0%84#

'Dev > Spring Data' 카테고리의 다른 글

[Querydsl] 7. 결과조회  (0) 2021.01.19
[Querydsl] 6. Querydsl Where절  (0) 2021.01.19
[Querydsl] 4. 예제용 도메인 생성  (0) 2021.01.19
[Querydsl] 3. JPA yml 설정  (0) 2021.01.19
[Querydsl] 2. H2 Database  (0) 2021.01.19
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함