[Spring Boot] 페이징 처리 - Criteria, PageDTO (tistory.com)
Spring boot, URI 쉽게 만들기 ( with UriComponentsBuilder ) (tistory.com)
dto는 일단 이렇게 만들고 변수선언함
package com.phyho.web;
import lombok.Data;
@Data
public class IndexDTO{
private int pageNum; // 현재 페이지 번호
private int amount; // 페이지당 글수
private int total; // 전체 글수
private int pageCount; // 페이지 사이즈 (10)
private int startPage; // 하단에 보여질 시작페이지
private int endPage; // 하단에 보여질 끝페이지
private int lastPage; // 최종 페이지
private boolean prev, next; // 이전페이지 존재여부 (이전페이지가 1이면 false, 아니면 true)
private IndexDTO dto;
//private String type, keyword;
public IndexDTO() {
this.pageNum = 1;
this.amount = 10;
}
public IndexDTO(int pageNum, int amount) {
this.pageNum = pageNum;
this.amount = amount;
}
public IndexDTO(int total, int pageCount, IndexDTO dto) {
this.total = total;
this.pageCount = pageCount;
this.dto = dto;
this.endPage = (int) (Math.ceil(pageNum*1.0 / pageCount) * pageCount);
this.startPage = endPage - (pageCount-1);
lastPage = ((int) Math.ceil(total*1.0 / pageCount)) + 1; // 찐막페이지 (글수가 174개라면 18) ?????
// 마지막 페이지면
if(endPage > lastPage) {
}
prev = startPage > 1; // true
next = endPage < lastPage; // true
}
}
컨드롤러에서 uri를 생성해봤다.
@GetMapping("/")
public String boardList(Model model, IndexDTO dto) {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("cate", "2");
map.add("bno", "10");
map.add("pageNum", String.valueOf(dto.getPageNum()));
UriComponents pageUri
= UriComponentsBuilder.fromUriString("http://localhost:8080/board")
.queryParams(map).build();
System.out.println(pageUri);
// http://localhost:8080/board?cate=2&bno=10&pageNum=1
List<Map<Object, String>> list = indexService.boardList();
//System.out.println(list);
model.addAttribute("list", list);
return "board2";
}
일단 하드코딩으로 값을 때려박고 pageNum 만 dto에서 가져와 봤더니 아래처럼 uri가 만들어진다.
http://localhost:8080/board?cate=2&bno=10&pageNum=1
public String getListLink(IndexDTO dto) {
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
builder.queryParam("pageNum", dto.getPageNum());
builder.queryParam("amount", dto.getAmount());
return "builder.toUriString()";
}
이런 방법도 있단다.
첫 번째 방식 (MultiValueMap 사용) 장단점:
장점:
더 많은 쿼리 매개변수를 쉽게 추가할 수 있습니다. 여러 개의 add 호출을 사용하여 쿼리 매개변수를 추가할 수 있으므로, 많은 매개변수를 가진 URI를 쉽게 작성할 수 있습니다.
코드의 가독성이 높을 수 있습니다. MultiValueMap을 사용하여 쿼리 매개변수를 그룹화하고, queryParams 메서드를 호출하여 URI를 빌드하는 방식은 코드의 의도를 명확하게 나타낼 수 있습니다.
단점:
복잡한 쿼리 매개변수 구조를 다루는 데에는 한계가 있을 수 있습니다. 특히 중첩된 구조의 쿼리 매개변수를 다루기 어려울 수 있습니다.
두 번째 방식 (UriComponentsBuilder 직접 사용) 장단점:
장점:
더 유연한 방식으로 쿼리 매개변수를 작성할 수 있습니다. UriComponentsBuilder의 메서드를 직접 호출하여 매개변수를 추가할 때, 복잡한 구조의 쿼리 매개변수를 다루기에 더 적합할 수 있습니다.
특정 매개변수를 동적으로 추가 또는 제거하는 데 용이합니다.
단점:
코드가 복잡해질 수 있습니다. UriComponentsBuilder 메서드를 직접 호출하면 코드가 상대적으로 길어질 수 있으며, 가독성이 감소할 수 있습니다.
많은 매개변수를 가진 URI를 작성하는 데에는 추가 노력이 필요할 수 있습니다.
어떤 방식을 선택할지는 프로젝트의 요구 사항, 코드의 가독성, 복잡성, 유지 보수 가능성에 따라 다를 수 있습니다. 간단한 경우에는 두 가지 방식 중 어느 것을 선택해도 상관없지만, 복잡한 쿼리 구조를 다루거나 동적으로 쿼리 매개변수를 추가해야 하는 경우에는 두 번째 방식이 더 유용할 수 있습니다.
by 지피티
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 46; columnNumber: 15; The content of elements must consist of well-formed character data or markup.
이런 mapper 문법오류가 계속 났었는데
<select id="mnextPage" parameterType="Map" resultType="Map">
<![CDATA[
SELECT *, (SELECT COUNT(*) FROM boardview) AS count
FROM(
SELECT CAST(@ROWNUM:=@ROWNUM+1 AS UNSIGNED) AS rowNum,
b.* FROM boardview b, (SELECT @ROWNUM:= 0) AS R
ORDER BY b.bno DESC) bb
WHERE bno < #{lastbno}
ORDER BY bb.bno DESC
LIMIT 0, 10
]]>
</select>
이렇게 쿼리문에 부등호가 포함되어 있으면 위아래에 <![CDATA[ ]]> 이렇게 넣어줘야 한단다. 까먹지 말자 제발
'국비과정 > 팀프로젝트' 카테고리의 다른 글
[SellAS] 검색 (0) | 2023.11.13 |
---|---|
[팀] 프로미스 함수 (0) | 2023.11.13 |
[팀] 스크롤페이징 (0) | 2023.11.03 |
[SellAS] 멀티보드 정리 (0) | 2023.10.27 |
[팀] 스크롤 관련 자료 (0) | 2023.10.20 |