querydsl: jpa + querydsl
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
package com.example.querydsl;
|
||||
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
@SpringBootApplication
|
||||
public class QuerydslApplication {
|
||||
@@ -10,4 +14,8 @@ public class QuerydslApplication {
|
||||
SpringApplication.run(QuerydslApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
JPAQueryFactory jpaQueryFactory(EntityManager em) {
|
||||
return new JPAQueryFactory(em);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.example.querydsl.controller;
|
||||
|
||||
import com.example.querydsl.entity.Member;
|
||||
import com.example.querydsl.entity.Team;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
@Profile("local")
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class InitMember {
|
||||
|
||||
private final InitMEmberService initMEmberService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
initMEmberService.init();
|
||||
}
|
||||
|
||||
@Component
|
||||
static class InitMEmberService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public void init() {
|
||||
Team teamA = new Team("teamA");
|
||||
Team teamB = new Team("teamB");
|
||||
em.persist(teamA);
|
||||
em.persist(teamB);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Team selectedTeam = i % 2 == 0 ? teamA : teamB;
|
||||
em.persist(new Member("member" + i, i, selectedTeam));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.querydsl.controller;
|
||||
|
||||
import com.example.querydsl.dto.MemberSearchCondition;
|
||||
import com.example.querydsl.dto.MemberTeamDto;
|
||||
import com.example.querydsl.repository.MemberJpaRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class MemberController {
|
||||
|
||||
private final MemberJpaRepository memberJpaRepository;
|
||||
|
||||
@GetMapping("/v1/members")
|
||||
public List<MemberTeamDto> searchMemberV1(MemberSearchCondition condition) {
|
||||
return memberJpaRepository.search(condition);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.querydsl.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class MemberSearchCondition {
|
||||
|
||||
private String username;
|
||||
private String teamName;
|
||||
private Integer ageGoe;
|
||||
private Integer ageLoe;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.querydsl.dto;
|
||||
|
||||
import com.querydsl.core.annotations.QueryProjection;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class MemberTeamDto {
|
||||
private Long memberId;
|
||||
private String username;
|
||||
private int age;
|
||||
private Long teamId;
|
||||
private String teamName;
|
||||
|
||||
@QueryProjection
|
||||
public MemberTeamDto(Long memberId, String username, int age, Long teamId, String teamName) {
|
||||
this.memberId = memberId;
|
||||
this.username = username;
|
||||
this.age = age;
|
||||
this.teamId = teamId;
|
||||
this.teamName = teamName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package com.example.querydsl.repository;
|
||||
|
||||
import com.example.querydsl.dto.MemberSearchCondition;
|
||||
import com.example.querydsl.dto.MemberTeamDto;
|
||||
import com.example.querydsl.dto.QMemberTeamDto;
|
||||
import com.example.querydsl.entity.Member;
|
||||
import com.querydsl.core.BooleanBuilder;
|
||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static com.example.querydsl.entity.QMember.member;
|
||||
import static com.example.querydsl.entity.QTeam.team;
|
||||
import static org.springframework.util.StringUtils.hasText;
|
||||
|
||||
@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() {
|
||||
return em.createQuery("select m from Member m", Member.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
public List<Member> findAll_Querydsl() {
|
||||
return queryFactory
|
||||
.selectFrom(member)
|
||||
.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();
|
||||
}
|
||||
|
||||
public List<Member> findByUsername_Querydsl(String username) {
|
||||
return queryFactory
|
||||
.selectFrom(member)
|
||||
.where(member.username.eq(username))
|
||||
.fetch();
|
||||
}
|
||||
|
||||
public List<MemberTeamDto> searchByBuilder(MemberSearchCondition condition) {
|
||||
BooleanBuilder builder = new BooleanBuilder();
|
||||
if (hasText(condition.getUsername())) {
|
||||
builder.and(member.username.eq(condition.getUsername()));
|
||||
}
|
||||
if (hasText(condition.getTeamName())) {
|
||||
builder.and(team.name.eq(condition.getTeamName()));
|
||||
}
|
||||
if (condition.getAgeGoe() != null) {
|
||||
builder.and(member.age.goe(condition.getAgeGoe()));
|
||||
}
|
||||
if (condition.getAgeLoe() != null) {
|
||||
builder.and(member.age.loe(condition.getAgeLoe()));
|
||||
}
|
||||
|
||||
return queryFactory
|
||||
.select(new QMemberTeamDto(
|
||||
member.id.as("memberId"),
|
||||
member.username,
|
||||
member.age,
|
||||
team.id.as("teamId"),
|
||||
team.name.as("teamName")))
|
||||
.from(member)
|
||||
.leftJoin(member.team, team)
|
||||
.where(builder)
|
||||
.fetch();
|
||||
}
|
||||
|
||||
public List<MemberTeamDto> search(MemberSearchCondition condition) {
|
||||
return queryFactory
|
||||
.select(new QMemberTeamDto(
|
||||
member.id.as("memberId"),
|
||||
member.username,
|
||||
member.age,
|
||||
team.id.as("teamId"),
|
||||
team.name.as("teamName")))
|
||||
.from(member)
|
||||
.leftJoin(member.team, team)
|
||||
.where(
|
||||
usernameEq(condition.getUsername()),
|
||||
teamNameEq(condition.getTeamName()),
|
||||
ageGoe(condition.getAgeGoe()),
|
||||
ageLoe(condition.getAgeLoe())
|
||||
)
|
||||
.fetch();
|
||||
}
|
||||
|
||||
public List<Member> searchMember(MemberSearchCondition condition) {
|
||||
return queryFactory
|
||||
.selectFrom(member)
|
||||
.leftJoin(member.team, team)
|
||||
.where(
|
||||
usernameEq(condition.getUsername()),
|
||||
teamNameEq(condition.getTeamName()),
|
||||
ageGoe(condition.getAgeGoe()),
|
||||
ageLoe(condition.getAgeLoe())
|
||||
)
|
||||
.fetch();
|
||||
}
|
||||
|
||||
private BooleanExpression ageBetween(int ageLoe, int ageGoe) {
|
||||
return ageLoe(ageLoe).and(ageGoe(ageGoe));
|
||||
}
|
||||
|
||||
private BooleanExpression usernameEq(String username) {
|
||||
return hasText(username) ? member.username.eq(username) : null;
|
||||
}
|
||||
|
||||
private BooleanExpression teamNameEq(String teamName) {
|
||||
return hasText(teamName) ? team.name.eq(teamName) : null;
|
||||
}
|
||||
|
||||
private BooleanExpression ageGoe(Integer ageGoe) {
|
||||
return ageGoe != null ? member.age.goe(ageGoe) : null;
|
||||
}
|
||||
|
||||
private BooleanExpression ageLoe(Integer ageLoe) {
|
||||
return ageLoe != null ? member.age.loe(ageLoe) : null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: local
|
||||
|
||||
datasource:
|
||||
url: jdbc:h2:tcp://localhost/~/Desktop/study/Study/querydsl/db
|
||||
username: sa
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.example.querydsl.repository;
|
||||
|
||||
import com.example.querydsl.dto.MemberSearchCondition;
|
||||
import com.example.querydsl.dto.MemberTeamDto;
|
||||
import com.example.querydsl.entity.Member;
|
||||
import com.example.querydsl.entity.Team;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
@Transactional
|
||||
class MemberJpaRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
EntityManager em;
|
||||
|
||||
@Autowired MemberJpaRepository memberJpaRepository;
|
||||
|
||||
@Test
|
||||
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();
|
||||
assertThat(result1).containsExactly(member);
|
||||
|
||||
List<Member> result2 = memberJpaRepository.findByUsername("member1");
|
||||
assertThat(result2).containsExactly(member);
|
||||
}
|
||||
|
||||
@Test
|
||||
void basicQuerydslTest() {
|
||||
Member member = new Member("member1", 10);
|
||||
memberJpaRepository.save(member);
|
||||
|
||||
List<Member> result1 = memberJpaRepository.findAll_Querydsl();
|
||||
assertThat(result1).containsExactly(member);
|
||||
|
||||
List<Member> result2 = memberJpaRepository.findByUsername_Querydsl("member1");
|
||||
assertThat(result2).containsExactly(member);
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchTest() {
|
||||
Team teamA = new Team("teamA");
|
||||
Team teamB = new Team("teamB");
|
||||
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);
|
||||
|
||||
MemberSearchCondition condition = new MemberSearchCondition();
|
||||
condition.setAgeGoe(35);
|
||||
condition.setAgeLoe(40);
|
||||
condition.setTeamName("teamB");
|
||||
|
||||
List<MemberTeamDto> result = memberJpaRepository.search(condition);
|
||||
|
||||
assertThat(result).extracting("username").containsExactly("member4");
|
||||
}
|
||||
}
|
||||
20
querydsl/src/test/resources/application.yml
Normal file
20
querydsl/src/test/resources/application.yml
Normal file
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: test
|
||||
|
||||
datasource:
|
||||
url: jdbc:h2:tcp://localhost/~/Desktop/study/Study/querydsl/db
|
||||
username: sa
|
||||
password:
|
||||
driver-class-name: org.h2.Driver
|
||||
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: create
|
||||
properties:
|
||||
hibernate:
|
||||
format_sql: true
|
||||
use_sql_comments: true
|
||||
|
||||
logging.level:
|
||||
org.hibernate.SQL: debug
|
||||
Reference in New Issue
Block a user