πνκ²½
IntelliJ Ultimate
Java 17
Spring boot 3.2.3
Gradle - Groovy
Dependencies:
Spring Web
Thymeleaf
Spring Data JPA
lombok
MariaDB 10.11
Spring Dev tool
πνλ¦
κ²μκΈ κ²μ κΈ°λ₯μ ν¬ν¨νμ¬ νμ΄μ§νλ€κ³ κ°μ νμ λ, νμ¬ μμ²νλ νμ΄μ§, νμ΄μ§λΉ κ²μκΈ μ, κ²μ νμ , κ²μ ν€μλ, λ§ν¬λ₯Ό μμ²νλ μμ² DTO κ°μ²΄μ νμ¬ νμ΄μ§, νμ΄μ§λΉ κ²μκΈ μ, μ΄μ /λ€μ νμ΄μ§ μ‘΄μ¬ μ¬λΆ, κ²μκΈλ€μ λ΄μ listλ₯Ό ν¬ν¨ν μλ΅ DTO κ°μ²΄κ° νμνλ€.
package org.zerock.b02.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageRequestDTO {
@Builder.Default
private int page = 1;
@Builder.Default
private int size = 10;
// κ²μ μΉ΄ν
κ³ λ¦¬: t(title), c(content), w(writer), tc, tw, twc
private String type;
private String keyword;
private String link;
public String[] getType(){
if(type == null || type.isEmpty()){
return null;
}
return type.split("");
}
public Pageable getPageable(String... props){
// PageRequest.of(νμ΄μ§ λ²νΈ, μ¬μ΄μ¦, Sort)
return PageRequest.of(this.page - 1, this.size, Sort.by("bno").descending());
}
public String getLink(){
if(link == null){
StringBuilder sb = new StringBuilder();
sb.append("page=").append(this.page).append("&size=").append(this.size);
// typeμ΄ λΉμ΄μμΌλ©΄
if(type == null && type.isEmpty()){
sb.append("&type=").append(type);
}
// keywordκ° nullμ΄λ©΄
if(keyword != null){
try {
sb.append("&keyword").append(URLEncoder.encode(keyword, "UTF-8"));
} catch (UnsupportedEncodingException e) {
}
}
// ex) page=1&type=tcw&keyword=μλ
νμΈμ
link = sb.toString();
}
return link;
}
}
package org.zerock.b02.dto;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;
import java.util.List;
@Getter
@ToString
public class PageResponseDTO<E> {
private int page;
private int size;
private int total;
// μμ νμ΄μ§ λ²νΈ
private int start;
// λ νμ΄μ§ λ²νΈ
private int end;
// μ΄μ /λ€μ νμ΄μ§ μ¬λΆ
private boolean prev;
private boolean next;
private List<E> dtoList;
@Builder(builderMethodName = "withAll")
public PageResponseDTO(PageRequestDTO pageRequestDTO, List<E> dtoList, int total){
// κ²μκΈ κ°μκ° 0μ΄λ©΄ νμ΄μ§ μ²λ¦¬νμ§ μμ
if(total <= 0){
return;
}
// νμ¬ λͺ νμ΄μ§?
this.page = pageRequestDTO.getPage();
// νμ΄μ§ λΉ κΈ κ°μ?
this.size = pageRequestDTO.getSize();
// κ²μκΈ λ΄κΈ΄ list
this.dtoList = dtoList;
// μ΄ κ²μκΈ μ
this.total = total;
// νλ©΄μμ λ§μ§λ§ λ²νΈ
// 10μΌλ‘ λλλ κ² μλλΌ (double)sizeλ‘ λλ μΌνλ κ±° μλκ°...? κ³±νλ κ²λ λ§μ°¬κ°μ§
this.end = (int)(Math.ceil(this.page / 10.0)) * 10;
// νλ©΄μμ μμ λ²νΈ
// μ¬κΈ°κ° 9κ° μλλΌ size - 1μ΄μ΄μΌ νλ κ±° μλκ°?
this.start = this.end - 9;
// λ°μ΄ν° κ°μλ₯Ό κ³μ°ν λ§μ§λ§ νμ΄μ§ λ²νΈ
int last = (int)(Math.ceil((total / (double)size)));
this.end = Math.min(end, last);
// μμ λ²νΈκ° 1λ³΄λ€ ν¬λ©΄ μ΄μ νμ΄μ§κ° μ‘΄μ¬ν¨
this.prev = this.start > 1;
// κ²μκΈ μ΄ κ°μκ° (λ§μ§λ§ λ²νΈ * νμ΄μ§ μ¬μ΄μ¦)λ³΄λ€ ν¬λ©΄ λ€μ νμ΄μ§κ° μ‘΄μ¬ν¨
this.next = total > this.end * this.size;
}
}
νμ΄μ§λΉ 보μ¬μ£Όλ κ²μκΈ κ°μ(size)κ° λ¬λΌμ§ μλ μκΈ° λλ¬Έμ endμ start λΆλΆμ μμ μ΄ νμν΄λ³΄μΈλ€.
μΌλ¨ 10κ°λ‘ κ³ μ λμ΄μλ€λ κ°μ νμ μμ±νλλ‘ νλ€.
@Test
public void listTest(){
PageRequestDTO pageRequestDTO = PageRequestDTO.builder()
.type("tcw") // μ λͺ©, λ΄μ©, μμ±μ λͺ¨λ κ²μν¨
.keyword("1") // 1μ κ²μν¨
.page(1) // 1νμ΄μ§
.size(10) // νμ΄μ§λΉ 10κ°μ©
.build();
// νμ΄μ§ μμ² κ°μ²΄λ‘ νμ΄μ§ μλ΅ κ°μ²΄λ₯Ό λ°μμ΄
PageResponseDTO<BoardDTO> pageResponseDTO = boardService.list(pageRequestDTO);
log.info("[pageResponseDTO] " + pageResponseDTO);
log.info("board list: " + pageResponseDTO.getDtoList());
log.info("total page: " + pageResponseDTO.getTotal());
log.info("current page: " + pageResponseDTO.getPage());
log.info("start: " + pageResponseDTO.getStart());
log.info("end: " + pageResponseDTO.getEnd());
}
Hibernate:
select
b1_0.bno,
b1_0.content,
b1_0.moddate,
b1_0.regdate,
b1_0.title,
b1_0.writer
from
board b1_0
where
(
b1_0.title like ? escape '!'
or b1_0.content like ? escape '!'
or b1_0.writer like ? escape '!'
)
and b1_0.bno>?
order by
b1_0.bno desc
limit
?, ?
Hibernate:
select
count(b1_0.bno)
from
board b1_0
where
(
b1_0.title like ? escape '!'
or b1_0.content like ? escape '!'
or b1_0.writer like ? escape '!'
)
and b1_0.bno>?
2024-03-13T10:46:49.375+09:00 INFO 66142 --- [b02] [ Test worker] o.zerock.b02.service.BoardServiceTests : [pageResponseDTO] PageResponseDTO(page=1, size=10, total=18, start=1, end=2, prev=false, next=false, dtoList=[BoardDTO(bno=91, title=title...91, content=content... 91, writer=user1, regDate=2024-03-12T17:36:01.847149, modDate=2024-03-12T17:36:01.847149), BoardDTO(bno=81, title=title...81, content=content... 81, writer=user1, regDate=2024-03-12T17:36:01.831171, modDate=2024-03-12T17:36:01.831171), BoardDTO(bno=71, title=title...71, content=content... 71, writer=user1, regDate=2024-03-12T17:36:01.812196, modDate=2024-03-12T17:36:01.812196), BoardDTO(bno=61, title=title...61, content=content... 61, writer=user1, regDate=2024-03-12T17:36:01.785425, modDate=2024-03-12T17:36:01.785425), BoardDTO(bno=51, title=title...51, content=content... 51, writer=user1, regDate=2024-03-12T17:36:01.747887, modDate=2024-03-12T17:36:01.747887), BoardDTO(bno=41, title=title...41, content=content... 41, writer=user1, regDate=2024-03-12T17:36:01.712196, modDate=2024-03-12T17:36:01.712196), BoardDTO(bno=31, title=title...31, content=content... 31, writer=user1, regDate=2024-03-12T17:36:01.663344, modDate=2024-03-12T17:36:01.663344), BoardDTO(bno=21, title=title...21, content=content... 21, writer=user1, regDate=2024-03-12T17:36:01.600227, modDate=2024-03-12T17:36:01.600227), BoardDTO(bno=19, title=title...19, content=content... 19, writer=user9, regDate=2024-03-12T17:36:01.589458, modDate=2024-03-12T17:36:01.589458), BoardDTO(bno=18, title=title...18, content=content... 18, writer=user8, regDate=2024-03-12T17:36:01.585534, modDate=2024-03-12T17:36:01.585534)])
2024-03-13T10:46:49.375+09:00 INFO 66142 --- [b02] [ Test worker] o.zerock.b02.service.BoardServiceTests : board list: [BoardDTO(bno=91, title=title...91, content=content... 91, writer=user1, regDate=2024-03-12T17:36:01.847149, modDate=2024-03-12T17:36:01.847149), BoardDTO(bno=81, title=title...81, content=content... 81, writer=user1, regDate=2024-03-12T17:36:01.831171, modDate=2024-03-12T17:36:01.831171), BoardDTO(bno=71, title=title...71, content=content... 71, writer=user1, regDate=2024-03-12T17:36:01.812196, modDate=2024-03-12T17:36:01.812196), BoardDTO(bno=61, title=title...61, content=content... 61, writer=user1, regDate=2024-03-12T17:36:01.785425, modDate=2024-03-12T17:36:01.785425), BoardDTO(bno=51, title=title...51, content=content... 51, writer=user1, regDate=2024-03-12T17:36:01.747887, modDate=2024-03-12T17:36:01.747887), BoardDTO(bno=41, title=title...41, content=content... 41, writer=user1, regDate=2024-03-12T17:36:01.712196, modDate=2024-03-12T17:36:01.712196), BoardDTO(bno=31, title=title...31, content=content... 31, writer=user1, regDate=2024-03-12T17:36:01.663344, modDate=2024-03-12T17:36:01.663344), BoardDTO(bno=21, title=title...21, content=content... 21, writer=user1, regDate=2024-03-12T17:36:01.600227, modDate=2024-03-12T17:36:01.600227), BoardDTO(bno=19, title=title...19, content=content... 19, writer=user9, regDate=2024-03-12T17:36:01.589458, modDate=2024-03-12T17:36:01.589458), BoardDTO(bno=18, title=title...18, content=content... 18, writer=user8, regDate=2024-03-12T17:36:01.585534, modDate=2024-03-12T17:36:01.585534)]
2024-03-13T10:46:49.375+09:00 INFO 66142 --- [b02] [ Test worker] o.zerock.b02.service.BoardServiceTests : board count: 10
2024-03-13T10:46:49.375+09:00 INFO 66142 --- [b02] [ Test worker] o.zerock.b02.service.BoardServiceTests : total page: 18
2024-03-13T10:46:49.375+09:00 INFO 66142 --- [b02] [ Test worker] o.zerock.b02.service.BoardServiceTests : current page: 1
2024-03-13T10:46:49.375+09:00 INFO 66142 --- [b02] [ Test worker] o.zerock.b02.service.BoardServiceTests : start: 1
2024-03-13T10:46:49.375+09:00 INFO 66142 --- [b02] [ Test worker] o.zerock.b02.service.BoardServiceTests : end: 2
μ λμνλ κ²μ νμΈν μ μλ€.
πμ°Έκ³
μλ° μΉ κ°λ° μν¬λΆ - ꡬλ©κ°κ² μ½λ©λ¨
'Java > Spring' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[SpringBoot] μ€νλ§ λΆνΈ 3.2.3μμ Swagger(springdoc) μ¬μ©νκΈ°! (0) | 2024.03.13 |
---|---|
[SpringBoot] Swagger μ¬μ© μ μ€λ₯ (0) | 2024.03.13 |
[SpringBoot/JPA] ModelMapperλ‘ Entityμ DTO λ³ννκΈ° (0) | 2024.03.13 |
[SpringBoot/JPA] Querydslμ μ¬μ©ν λμ 쿼리 (0) | 2024.03.12 |
[SpringBoot/JPA] 쿼리 λ©μλλ₯Ό λ체νλ @Query (0) | 2024.03.12 |