Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 김영한
- transaction
- 인프런
- Proxy
- Greedy
- Android
- 스프링
- http
- kotlin
- SpringBoot
- 스프링 핵심 기능
- 백준
- Exception
- pointcut
- Spring Boot
- QueryDSL
- spring
- 스프링 핵심 원리
- 알고리즘
- db
- java
- JPQL
- springdatajpa
- 그리디
- Thymeleaf
- AOP
- Servlet
- JDBC
- jpa
- 자바
Archives
- Today
- Total
개발자되기 프로젝트
JPA Repository와 Querydsl 본문
1. 순수 JPA Repository -> Querydsl
@Repository
public class MemberJpaRepository {
private final EntityManager em;
private final JPAQueryFactory queryFactory;
public MemberJpaRepository(EntityManager em) {
this.em = em;
this.queryFactory = new JPAQueryFactory(em);
}
public void save(Member member){
em.persist(member);
}
public Optional<Member> findById(Long id){
Member findMember = em.find(Member.class, id);
return Optional.ofNullable(findMember);
}
public List<Member> findAll_Querydsl(){
return queryFactory
.select(member)
.from(member)
.fetch();
}
public List<Member> findAll(){
return em.createQuery("select m from Member m", Member.class).getResultList();
}
public List<Member> findByUsername_Querydsl(String username){
return queryFactory
.selectFrom(member)
.where(member.username.eq(username))
.fetch();
}
public List<Member> findByUsername(String username){
return em.createQuery("select m from Member m where m.username = :username", Member.class)
.setParameter("username", username)
.getResultList();
}
}
@SpringBootTest
@Transactional
class MemberJpaRepositoryTest {
@Autowired
EntityManager em;
@Autowired MemberJpaRepository memberJpaRepository;
@Test
public void basicTest(){
Member member = new Member("member1", 10);
memberJpaRepository.save(member);
Member findMember = memberJpaRepository.findById(member.getId()).get();
assertThat(findMember).isEqualTo(member);
List<Member> result1 = memberJpaRepository.findAll_Querydsl();
assertThat(result1).containsExactly(member);
List<Member> result2 = memberJpaRepository.findByUsername_Querydsl("member1");
assertThat(result2).containsExactly(member);
}
}
2. JPAQueryFactory 사용 방법?
- 생성자로 EntityManager를 받아서 JPAQueryFactory에 넣어주거나
- injection이 하나 ㅋㅋ
- 스프링 bean으로 등록
- injfection 두 개.. 귀찮..
@SpringBootApplication
public class QuerydslApplication {
public static void main(String[] args) {
SpringApplication.run(QuerydslApplication.class, args);
}
@Bean
JPAQueryFactory jpaQueryFactory(EntityManager em){
return new JPAQueryFactory(em);
}
}
public MemberJpaRepository(EntityManager em, JPAQueryFactory queryFactory) {
this.em = em;
this.queryFactory = queryFactory;
}
* 참고
JPAQueryFactory의 동시성 문제는 EntityManager에 의존함
Spring과 EntityManager를 엮어서 쓰면 Transaction 단위로 동시성 관계 없이 분리되어 동작함.
3. GitHub : 210904 Querydsl, Repository
'인프런 > [인프런] QueryDsl' 카테고리의 다른 글
API 개발 (0) | 2021.09.04 |
---|---|
동적쿼리 성능 최적화 : Builder , where 다중 파라미터 (0) | 2021.09.04 |
SQL Function 호출 (0) | 2021.09.04 |
벌크 연산 (0) | 2021.09.04 |
동적쿼리 : BooleanBuilder, Where 다중 파라미터 (0) | 2021.09.03 |
Comments