써먹는 웹개발
[Elasticsearch] Java API - JPA 연동 조회 방법 본문
728x90
반응형
Spring Data Jpa에서는 Custom Repository를 JpaRepository 상속 클래스에서 사용할 수 있도록 기능을 지원합니다.
실제로 소스를 사용하면,
0. 모델 생성
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
|
@Data
@Entity
@Table(name = "ACADEMY")
public class Academy {
@Id
@Column(name = "BASE_INFO_SEQ")
@ApiModelProperty(value = "seq")
private Long baseInfoSeq;
@ApiModelProperty(value = "직업")
private String job;
// baseInfo 관련 컬럼
@Transient
@ApiModelProperty(value = "영문명")
private String name;
@Transient
@ApiModelProperty(value = "한글명")
private String nameKr;
@Transient
@ApiModelProperty(value = "풀네임")
private String fullName;
@Transient
@ApiModelProperty(value = "생년월일")
private String birthDate;
@Transient
@ApiModelProperty(value = "성별")
private String gender;
}
|
cs |
1. jpa 관련 인터페이스 생성
1 2 3 4 5 6 |
@Repository public interface AcademyRepositoryExtra { Page<Academy> findAllByQueryParams(Optional<QueryParams> queryParams) throws ParseException; List<Academy> findAllListByQueryParams(Optional<QueryParams> queryParams) throws ParseException; List<Academy> findByBaseInfoSeq(int seq) throws ParseException; } Colored by Color Scripter |
cs |
2. JpaRepository를 상속받은 인터페이스 생성
※ 여기에는 단순한 조회(or 쿼리문) 관련 소스를 호출하기도 함
1
2
3
|
@Repository
public interface AcademyRepository extends JpaRepository<Academy, Long>, AcademyRepositoryExtra {
}
|
cs |
3. Jpa 관련 소스 생성
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
public class AcademyRepositoryImpl extends QuerydslRepositorySupport implements AcademyRepositoryExtra {
public AcademyRepositoryImpl() {
super(Academy.class);
}
@PersistenceContext
private EntityManager em;
@Override
public Page<Academy> findAllByQueryParams(Optional<QueryParams> queryParams) {
Pageable pageable = queryParams.isPresent() ? queryParams.get().getPageable() : Pageable.unpaged();
JPQLQuery query = getQuery(queryParams);
long totalCount = query.fetchCount();
final List<Academy> Academy = getQuerydsl().applyPagination(pageable, query).fetch();
return new PageImpl(Academy, pageable, totalCount);
}
@Override
public List<Academy> findAllListByQueryParams(Optional<QueryParams> queryParams) throws ParseException {
JPQLQuery query = getQuery(queryParams);
return query.fetch();
}
public JPQLQuery getQuery(Optional<QueryParams> queryParams) {
QBaseInfo qBaseInfo = QBaseInfo.baseInfo;
QAcademy qAcademy = QAcademy.academy;
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
JPQLQuery query = queryFactory
.select(Projections.fields(Academy.class, qBaseInfo.seq, qAcademy.baseInfoSeq, qBaseInfo.name, qBaseInfo.nameKr,
qBaseInfo.name.concat(" (").concat(qBaseInfo.nameKr).concat(")").as("fullName"), qBaseInfo.birthDate, qBaseInfo.gender,
qAcademy.job
)
).from(qBaseInfo)
.rightJoin(qAcademy)
.on(qBaseInfo.seq.eq(qAcademy.baseInfoSeq))
.orderBy(qBaseInfo.regDate.desc()); // 등록일 역순으로 정렬
// Name filter
if (queryParams.isPresent() && queryParams.get().getName().isPresent() &&
!isEmpty(queryParams.get().getName().get())) {
// contains '단어'는 like '%단어%'와 같다
query.where(qBaseInfo.name.upper().contains(queryParams.get().getName().get().toUpperCase(Locale.ROOT))
.or(qBaseInfo.nameKr.contains(queryParams.get().getName().get())));
}
// From/To birthDate filter
if (queryParams.isPresent() && queryParams.get().getBirthDateFrom().isPresent() &&
!isEmpty(queryParams.get().getBirthDateFrom().get())) {
if (!queryParams.get().getBirthDateTo().isPresent()) {
query.where(qBaseInfo.birthDate.eq(queryParams.get().getBirthDateFrom().get()));
} else {
query.where(qBaseInfo.birthDate.between(queryParams.get().getBirthDateFrom().get(),
queryParams.get().getBirthDateTo().get()));
}
}
return query;
}
@Override
public List<Academy> findByBaseInfoSeq(int seq) {
QBaseInfo qBaseInfo = QBaseInfo.baseInfo;
QAcademy qAcademy = QAcademy.academy;
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
JPQLQuery query = queryFactory
.select(Projections.fields(Academy.class, qBaseInfo.seq, qAcademy.baseInfoSeq, qBaseInfo.name, qBaseInfo.nameKr,
qBaseInfo.name.concat(" (").concat(qBaseInfo.nameKr).concat(")").as("fullName"), qBaseInfo.birthDate, qBaseInfo.gender,
qAcademy.job
)
).from(qBaseInfo)
.rightJoin(qAcademy)
.on(qBaseInfo.seq.eq(qAcademy.baseInfoSeq))
.orderBy(qBaseInfo.regDate.desc()); // 등록일 역순으로 정렬
// Seq filter
query.where(qAcademy.baseInfoSeq.eq(Long.valueOf(seq)));
return query.fetch();
}
}
|
cs |
4. 조회 호출
1
2
3
4
5
6
|
/**
* 아카데미 목록 조회
*/
public Page<Academy> listAcademy(Optional<QueryParams> queryParams) throws ParseException {
return AcademyRepository.findAllByQueryParams(queryParams);
}
|
cs |
5. 페이징
1
2
3
4
5
6
7
8
9
10
11
12
|
boolean pageable = true;
public Pageable getPageable() {
if (pageable) {
final int currentPage = page.orElse(1);
final int pageSize = size.orElse(10);
return PageRequest.of(currentPage - 1, pageSize);
} else {
return Pageable.unpaged();
}
}
|
cs |
728x90
반응형
'Study > Elasticsearch' 카테고리의 다른 글
[ES] logstash 테스트 중 버전 문제로 수정 (0) | 2022.10.23 |
---|---|
[ElasticSearch] Index생성에서 에러난 이유 (0) | 2021.11.11 |
[Elasticsearch] Query DSL이란 (0) | 2021.11.07 |
[elasticsearch] _id 기준으로 데이터 수정 (0) | 2021.08.26 |
[Elasticsearch] 자바에서 10000번째를 초과하는 인덱스를 조회하고 싶을때 쓰는 명령어 (0) | 2021.08.21 |
Comments