Back-End ์ž‘์—…์‹ค/Spring Framework

[Mybatis] Spring Boot์™€ Mybatis๋ฅผ ์ด์šฉํ•œ Paging ์ฒ˜๋ฆฌ์™€ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ๊ตฌํ˜„

์ฃผ๋‹ˆ์“ฐ๐Ÿง‘‍๐Ÿ’ป 2022. 6. 19. 06:29
728x90
๋ฐ˜์‘ํ˜•



Git Hub ์ฃผ์†Œ : https://github.com/junyharang-Development-Lab/mybatis-paging-search-test/tree/main

 

 

๐Ÿš€ Mybatis๋ฅผ ์ด์šฉํ•œ Paging ์ฒ˜๋ฆฌ์™€ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ๊ตฌํ˜„

 

 

 

๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ํ”„๋กœ๊ทธ๋ž˜๋ฐ:JDBC๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ์‰ฝ๊ณ  ๋น ๋ฅธ ์ž๋ฐ” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ”„๋ ˆ์ž„์›Œํฌ

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

    ๐Ÿ”ฝ  ์ดˆ๊ธฐ ๊ตฌ์„ฑ

 

DB Table ๋‚ด์šฉ

์ตœ์ดˆ DB์—๋Š” ์œ„์™€ ๊ฐ™์ด study๋ผ๋Š” DB์•ˆ์— board๋ผ๋Š” Table์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ์•ˆ์— ๋‚ด์šฉ์„ ๋„ฃ์–ด ์ฃผ์—ˆ์–ด์š”.

 

 

        ๐Ÿ“ฆ Project ๊ตฌ์„ฑ 

์ด๋ฒˆ ์‹ค์Šต์—์„œ ์ฃผ๋‹ˆํ•˜๋ž‘์€ Gradle๊ณผ ํ•จ๊ป˜ ์•„๋ž˜์™€ ๊ฐ™์ด ์˜์กด์„ฑ์„ ๊ตฌ์„ฑํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

plugins {
   id 'org.springframework.boot' version '2.7.0'
   id 'io.spring.dependency-management' version '1.0.11.RELEASE'
   id 'java'
}

group = 'com.junyharang.mybatis'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
   compileOnly {
      extendsFrom annotationProcessor
   }
}

repositories {
   mavenCentral()
}

dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-jdbc'
   implementation 'org.springframework.boot:spring-boot-starter-web'
   implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2'
    implementation 'org.springframework.boot:spring-boot-starter-test'
   implementation 'junit:junit:4.13.2'
   compileOnly 'org.projectlombok:lombok'
   developmentOnly 'org.springframework.boot:spring-boot-devtools'
   implementation group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.4.1'
   annotationProcessor 'org.projectlombok:lombok'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'

   // Page Helper
   // https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter
   implementation 'com.github.pagehelper:pagehelper-spring-boot-starter:1.4.2'

   // Mybatis Test
   // https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter-test
   testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:2.2.2'

   // SQL query show
   implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'
   
}

tasks.named('test') {
   useJUnitPlatform()
}


์ด๋ฒˆ ์‹ค์Šต์—์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด ๋ฐ”๋กœ Page Helper์ธ ๊ฒƒ์ด์—์š”.

Paging์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ํ•˜๋‚˜ํ•˜๋‚˜ ์ˆ˜์ž‘์—…์œผ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ , JPA์˜ Pageable๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์ฐพ์•„๋ณด๋˜ ์ค‘ ์•„์ฃผ ์ข‹์€ ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•˜์—ฌ ์‹ค์Šตํ•ด๋ณด๊ณ , ๋‚ด์šฉ์„ ์ •๋ฆฌ ํ•ด ๋ด…๋‹ˆ๋‹ค.

์ฃผ๋‹ˆํ•˜๋ž‘์€ DataBase๋ฅผ Maria DB๋ฅผ ์ด์šฉํ•˜๊ณ  ์žˆ์–ด์š”!

 

 

 

    ๐Ÿ”ฝ  Controller

        ๐Ÿ“ฆ MybatisPagingTestController

package com.junyharang.mybatis.mybatispagingtest.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junyharang.mybatis.mybatispagingtest.CustomBaseResponse;
import com.junyharang.mybatis.mybatispagingtest.model.dto.request.BoardListSearchDTO;
import com.junyharang.mybatis.mybatispagingtest.model.dto.request.BoardWriteRequestDTO;
import com.junyharang.mybatis.mybatispagingtest.model.vo.BoardResponseVO;
import com.junyharang.mybatis.mybatispagingtest.service.MybatisPagingTestServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

MybatisPagingTestController

 

์ตœ์ดˆ Controller๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๊ณ , ์œ„์™€ ๊ฐ™์ด ๊ธ€ ๋“ฑ๋ก, ๋ชฉ๋ก ์กฐํšŒ API๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ์ฃผ์š” ์Ÿ์ ์€ Paging ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ๊ฐ–๋Š” ๋ชฉ๋ก ์กฐํšŒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ชฉ๋ก ์กฐํšŒ์— ๋Œ€ํ•œ ๋‚ด์šฉ๋งŒ ์ •๋ฆฌ๋ฅผ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

41 ~ 45๋ฒˆ์งธ ์ค„์—์„œ Client์—๊ฒŒ ์š”์ฒญ ๋ฐ›์€ ๋‚ด์šฉ ์ค‘ pageNum๊ณผ pageSize์— ๋Œ€ํ•œ ๊ฐ’์ด ์—†๋‹ค๋ฉด 400 Error๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.

API URI๋Š” /api/junyharang/board๋กœ ์„ค์ •์„ ํ•ด ์ฃผ์—ˆ๊ณ , HTTP Method๋Š” Get์œผ๋กœ ์„ค์ •์„ ํ•ด์ฃผ์—ˆ์–ด์š”.
๊ทธ๋ฆฌ๊ณ , ํ•ด๋‹น Method๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” BoardListSearchDTO Type์˜ ๊ฐ’์„ ๋ฐ›๋„๋ก ์„ค์ •์ด ๋˜์–ด ์žˆ์–ด์š”.

๊ทธ๋Ÿฐ ๋’ค PageHelper ์•ˆ์— startPage()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Client์˜ ์š”์ฒญ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•ด ์ฃผ๊ณ  ์žˆ์–ด์š”.

์ด๋ ‡๊ฒŒ ํ•ด ์ฃผ๊ฒŒ ๋˜๋ฉด ์‹œ์ž‘ Page์— ๋Œ€ํ•œ ๊ณ„์‚ฐ์„ ํ•ด ์ฃผ๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์ฃผ๋ชฉํ•ด์•ผ ๋  ๊ณณ์€ return์ด์—์š”. PageInfo๋Š” pagehelper-spring-boot-starter์— ์žˆ๋Š” Class์ธ๋ฐ, PageInfo๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ Filed ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์š”.

// ํ˜„์žฌ ํŽ˜์ด์ง€
private int pageNum ;
// ํŽ˜์ด์ง€๋‹น ์ˆ˜๋Ÿ‰
private int pageSize ;
// ํ˜„์žฌ ํŽ˜์ด์ง€ ์ˆ˜
private int size ;
// ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์žˆ๋Š” ์ค„ ๋ฒˆํ˜ธ
private long startRow ;
// ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์žˆ๋Š” ์ค„ ๋ฒˆํ˜ธ
private long endRow ;
// ์ „์ฒด ํŽ˜์ด์ง€ ์ˆ˜
private int pages ;
// ์ด์ „ ํŽ˜์ด์ง€
private int prePage ;
// ๋‹ค์Œ ํŽ˜์ด์ง€
private int nextPage ;
// ์ฒซ ํŽ˜์ด์ง€์ธ์ง€ ์—ฌ๋ถ€
private boolean isFirstPage = false ;
// ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€์ธ์ง€ ์—ฌ๋ถ€
private boolean isLastPage = false ;
// ์ด์ „ ํŽ˜์ด์ง€๊ฐ€ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€
private boolean hasPreviousPage = false ;
// ๋‹ค์Œ ํŽ˜์ด์ง€๊ฐ€ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€
private boolean hasNextPage = false ;
// ๋‚ด๋น„๊ฒŒ์ด์…˜ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ
private int navigatePages ;
// ๋ชจ๋“  ๋‚ด๋น„๊ฒŒ์ด์…˜ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ
private int [ ] navigatepageNums ;
// ๋‚ด๋น„๊ฒŒ์ด์…˜ ๋ฐ”์˜ ์ฒซ ํŽ˜์ด์ง€
private int navigateFirstPage ;
// ๋‚ด๋น„๊ฒŒ์ด์…˜ ๋ฐ”์˜ ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€
private int navigateLastPage ;


์ด๋ ‡๊ฒŒ PageInfo๋Š” Paging ์ฒ˜๋ฆฌ์™€ ๊ด€๋ จ๋œ ์ •๋ณด์™€ Paging ์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋œ Data List๋ฅผ ๋ฐ˜ํ™˜ํ•ด ์ฃผ๊ฒŒ ๋˜์–ด ์žˆ์–ด์š”.

์ด๋ฒˆ์—” BoardListSearchDTO๋ฅผ ํ•œ๋ฒˆ ๋งŒ๋‚˜๋Ÿฌ ๊ฐ€๋ด์š”!


 

 

 

    ๐Ÿ”ฝ  RequestDTO

        ๐Ÿ“ฆ BoardListSearchDTO

 

BoardListSearchDTO

 

Client๋Š” ๋ชฉ๋ก ์กฐํšŒ๋ฅผ ์œ„ํ•ด API์—๊ฒŒ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ, ํ˜„์žฌ ์ด์šฉ์ž์˜ Page ์œ„์น˜ ๋ฒˆํ˜ธ์™€ ํ•œ Page ๋‹น ์ถœ๋ ฅ๋  ๊ฒŒ์‹œ๊ธ€ ๊ฐœ์ˆ˜๋ฅผ ๋ฌด์กฐ๊ฑด ๋ณด๋‚ด์ค˜์•ผ ํ•ด์š”. ์•ˆ ๊ทธ๋Ÿผ Exception์ด ํ„ฐ์ง€๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค!

๊ทธ๋ฆฌ๊ณ , ๋ชฉ๋ก ์กฐํšŒ์™€ ํ•จ๊ป˜ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ๊ตฌํ˜„์„ ์œ„ํ•ด ๊ฒ€์ƒ‰ Type๊ณผ ๊ฒ€์ƒ‰์–ด๋ฅผ ํ•จ๊ป˜ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก DTO๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ์ฃผ์—ˆ์–ด์š”.

Mybatis๋Š” JPA์™€ ๋‹ค๋ฅด๊ฒŒ ๊ตฌ๋™ํ•  ๋•Œ ์ฐธ์กฐํ•  ๊ฐ์ฒด์— Getter์™€ Setter๋ฅผ ๋ชจ๋‘ ํ•„์š”๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— @Data๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

JPA๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด Entity์— Setter๋ฅผ ๋„ฃ์œผ๋ฉด ์•ˆ๋˜๋Š” ๊ฒƒ์ด์—์š”.

์ด๋ฒˆ์—๋Š” Service ์ฆ‰, Businees Logic์„ ๋งŒ๋‚˜๋Ÿฌ ๊ฐ€๋ณผ๊ฒŒ์š”!





    ๐Ÿ”ฝ  Businees Logic

        ๐Ÿ“ฆ MybatisPagingTestService

MybatisPagingTestService Interface

 

MybatisPagingTestServiceImpl

 

์ฃผ๋‹ˆํ•˜๋ž‘์€ SOLID์˜ OCP (Open/Closed Principle) : ๊ฐœ๋ฐฉ - ํ์‡„ ์›์น™์„ ์ค€์ˆ˜ํ•˜๊ธฐ ์œ„ํ•ด Interface์™€ ๊ตฌํ˜„์ฒด๋ฅผ ๋‚˜๋ˆ„์–ด ์ฃผ์—ˆ์–ด์š”.

๊ตฌํ˜„์ฒด์— getPaging()์„ ๋ณด๊ฒŒ ๋˜๋ฉด Client์—๊ฒŒ ์ „๋‹ฌ ๋ฐ›์€ Paging ์ฒ˜๋ฆฌ์— ํ•„์š”ํ•œ ๊ฐ’๊ณผ ๊ฒ€์ƒ‰ ์ฒ˜๋ฆฌ์— ํ•„์š”ํ•œ ๊ฐ’์„ ๋ฐ›๊ณ , ์ด๋ฅผ Mybatis๋ฅผ ์ด์šฉํ•ด DB ์กฐํšŒ๋ฅผ ์œ„ํ•ด Mapper์˜ findByPagingAndSearch()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•ด ์ฃผ๊ณ , ์กฐํšŒ๋œ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์— ๊ฒฐ๊ณผ๊ฐ’์„ List๋กœ ์ „๋‹ฌ์„ ํ•˜๋Š”๋ฐ, BoardResponseVO Type์— ๋งž๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด Return Type์„ ์ง€์ •ํ•ด ์ฃผ์—ˆ์–ด์š”.





    ๐Ÿ”ฝ  DataBase Query

        ๐Ÿ“ฆ MybatisPagingTestMapper

 

MybatisPagingTestMapper


์ด ์นœ๊ตฌ๋Š” ์‹ค์ œ๋กœ Annotation์„ ํ†ตํ•ด Query๋ฅผ ์ ์–ด์ค„ ์ˆ˜๋„ ์žˆ๋Š” ๊ณณ์ด๊ณ , ๋”ฐ๋กœ xml์„ ๋งŒ๋“ค์–ด ๋™์  Query๋ฅผ ๋งŒ๋“ค์–ด Mappingํ•  ์ˆ˜ ์žˆ๋Š” Mapper Class์—์š”.

24๋ฒˆ์งธ ์ค„์— Client์—๊ฒŒ ์˜จ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ ๋ฐ›๊ณ  ์žˆ๊ณ , Service ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ DB์—์„œ ์กฐํšŒ๋œ ๊ฒฐ๊ณผ๊ฐ’์„ List๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, BoardResponseVO์— ๋งž๋Š” ๊ฐ’์„ ์ „๋‹ฌ ํ•ด ์ฃผ๊ณ  ์žˆ์–ด์š”.



๋ฐ˜์‘ํ˜•

 

 

        ๐Ÿ“ฆ MybatisPagingTestMapper.xml

 

MybatisPagingTestMapper.xml


์‹ค์ œ Query๋ฅผ ์ž‘์„ฑํ•˜๋Š” xml File๋‚ด์šฉ์ด์—์š”.

18๋ฒˆ์งธ์ค„ ๋ถ€ํ„ฐ ๋ชฉ๋ก ์กฐํšŒ๋ฅผ ์œ„ํ•œ Query๋ฌธ์„ ์ž‘์„ฑํ•œ ๊ณณ์ธ๋ฐ, board Table์— ๋ชจ๋“  ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ, ๊ฒ€์ƒ‰ Type์ด null์ด ์•„๋‹ˆ๊ณ , ๊ณต๋ฐฑ์ด ์•„๋‹ˆ๊ณ , ๊ฒ€์ƒ‰ Type์ด title์ธ๋ฐ, ๊ฒ€์ƒ‰์–ด๊ฐ€ ์ž…๋ ฅ์ด ์•ˆ ๋˜์—ˆ์„ ๋•Œ Error๋ฅผ ๋ฐฉ์ง€ ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ๋‚ด์šฉ์„ ๋ฐ˜ํ™˜ํ•˜๋ ค๊ณ  ํ•˜์˜€์–ด์š”. ๊ทธ๋ž˜์„œ 26๋ฒˆ์งธ ์ค„์— ๊ฒ€์ƒ‰์–ด๊ฐ€ ๋น„์–ด ์žˆ๊ฑฐ๋‚˜, ๊ณต๋ฐฑ์ด๋ฉด ๋ชจ๋“  ๋‚ด์šฉ์„ ์กฐํšŒํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•ด ์ฃผ์—ˆ๊ณ , ๋น„์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด 30๋ฒˆ์งธ ์ค„์— ํ•ด๋‹น๋˜์–ด์„œ ๊ฒ€์ƒ‰์–ด์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ์„ DB์—์„œ ์กฐํšŒํ•˜๋„๋ก ๋˜์–ด ์žˆ์–ด์š”.

๋งŒ์•ฝ ๊ฒ€์ƒ‰ Type์ด contents๋ผ๋ฉด ๋‚ด์šฉ์„ ๊ฒ€์ƒ‰ํ•˜๋„๋ก ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ๋ฐ˜ํ™˜ Type์€ BoardResponseVO ๊ฐ์ฒด๋กœ ํ•ด์ฃผ์—ˆ๊ณ , ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” BoardListSearchDTO๋กœ ํ•ด์ฃผ์—ˆ์–ด์š”.

๊ทธ๋Ÿฐ๋ฐ, ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์€ SQL์„ ํ†ตํ•ด Paging ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด์—์š”.
์ฆ‰, LIMIT Query๋ฌธ์ด ์—†๋Š” ๊ฒƒ์ด ๋ณด์ด์‹œ๋‚˜์š”?

์ด๊ฒƒ์€ Page Helper๋ผ๋Š” ์นœ๊ตฌ๊ฐ€ SQL์— ํž˜์„ ๋นŒ๋ฆฌ์ง€ ์•Š๊ณ , Code ์ƒ์—์„œ Paging์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค๋Š” ์žฅ์ ์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”.





    ๐Ÿ”ฝ  Response DTO

        ๐Ÿ“ฆ BoardResponseVO

 

BoardResponseVO

DB์—์„œ ์กฐํšŒ๊ฐ€ ๋๋‚˜๋ฉด ์œ„ VO ๊ฐ์ฒด์— ๋งž๊ฒŒ id, title, contents๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก VO๋ฅผ ๊ตฌ์„ฑํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.







    ๐Ÿ”ฝ  ์ •์ƒ ์ž‘๋™ ๊ฒ€์‚ฌ

 

POSTMAN TEST

๋จผ์ € POSTMAN์„ ํ†ตํ•ด ํ˜„์žฌ ์ด์šฉ์ž ์œ„์น˜ Page๋ฅผ 1 Page(pageNum = 1)๋กœ ์ฃผ๊ณ , ํ•œ Page ๋‹น ์ถœ๋ ฅ๋  ๊ฒŒ์‹œ๊ธ€ ๊ฐœ์ˆ˜๋ฅผ 10(pageSize)๋กœ ์„ค์ •์„ ํ•ด ์ฃผ์—ˆ์–ด์š”.

{
    "errorCode": 0,
    "title": "",
    "message": "",
    "data": {
        "total": 7,
        "list": [
            {
                "id": 1,
                "title": "์ œ๋ชฉ ์ž…๋‹ˆ๋‹ค!",
                "contents": "๋‚ด์šฉ ์ž…๋‹ˆ๋‹ค!"
            },
            {
                "id": 2,
                "title": "ํ•˜์ด์—ผ!",
                "contents": "์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค!"
            },
            {
                "id": 3,
                "title": "์ฝ”๋”ฉ์€ ์žฌ๋ฐŒ์–ด์š”!",
                "contents": "๋„˜๋‚˜ ์žฌ๋ฐŒ์–ด์š”!!"
            },
            {
                "id": 4,
                "title": "Full Stack!",
                "contents": "Front๋„ ์—ด์‹ฌํžˆ!"
            },
            {
                "id": 5,
                "title": "JAVA ๋งŒ์„ธ!",
                "contents": "JAVA๊ฐ€ ์ œ์ผ ํŽธํ•ด์š”!"
            },
            {
                "id": 6,
                "title": "Spring์ด ์ข‹์•„!",
                "contents": "๋„˜๋‚˜๋„˜๋‚˜!"
            },
            {
                "id": 7,
                "title": "mariadb ์ตœ๊ณ !!",
                "contents": "์ข‹์•„์š”!"
            }
        ],
        "pageNum": 1,
        "pageSize": 10,
        "size": 7,
        "startRow": 1,
        "endRow": 7,
        "pages": 1,
        "prePage": 0,
        "nextPage": 0,
        "isFirstPage": true,
        "isLastPage": true,
        "hasPreviousPage": false,
        "hasNextPage": false,
        "navigatePages": 8,
        "navigatepageNums": [
            1
        ],
        "navigateFirstPage": 1,
        "navigateLastPage": 1
    },
    "success": true
}


์ฐธ๊ณ ๋กœ ์ฃผ๋‹ˆํ•˜๋ž‘์€ CustomBaseResponse๋ผ๋Š” ์‘๋‹ต ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.
์ด์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ์ฃผ๋‹ˆํ•˜๋ž‘ Git Hub์„ ์ฐธ๊ณ ํ•ด ์ฃผ์‹œ๊ณ , ์ด ์„ค์ •๋“ค์— ๋”ฐ๋ผ ์œ„์™€ ๊ฐ™์ด ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ’์ด ์กฐ๊ธˆ์€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์–ด์š”.


์ด๋ฒˆ์—๋Š” ์ด์šฉ์ž๊ฐ€ 1 Page์— ์žˆ๊ณ , ์ด 5๊ฐœ์˜ ๊ฒŒ์‹œ๊ธ€์„ ๊ฐ€์ ธ์˜ค๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.

{
    "errorCode": 0,
    "title": "",
    "message": "",
    "data": {
        "total": 7,
        "list": [
            {
                "id": 1,
                "title": "์ œ๋ชฉ ์ž…๋‹ˆ๋‹ค!",
                "contents": "๋‚ด์šฉ ์ž…๋‹ˆ๋‹ค!"
            },
            {
                "id": 2,
                "title": "ํ•˜์ด์—ผ!",
                "contents": "์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค!"
            },
            {
                "id": 3,
                "title": "์ฝ”๋”ฉ์€ ์žฌ๋ฐŒ์–ด์š”!",
                "contents": "๋„˜๋‚˜ ์žฌ๋ฐŒ์–ด์š”!!"
            },
            {
                "id": 4,
                "title": "Full Stack!",
                "contents": "Front๋„ ์—ด์‹ฌํžˆ!"
            },
            {
                "id": 5,
                "title": "JAVA ๋งŒ์„ธ!",
                "contents": "JAVA๊ฐ€ ์ œ์ผ ํŽธํ•ด์š”!"
            }
        ],
        "pageNum": 1,
        "pageSize": 5,
        "size": 5,
        "startRow": 1,
        "endRow": 5,
        "pages": 2,
        "prePage": 0,
        "nextPage": 2,
        "isFirstPage": true,
        "isLastPage": false,
        "hasPreviousPage": false,
        "hasNextPage": true,
        "navigatePages": 8,
        "navigatepageNums": [
            1,
            2
        ],
        "navigateFirstPage": 1,
        "navigateLastPage": 2
    },
    "success": true
}




์ด๋ฒˆ์—๋Š” ์ด์šฉ์ž๊ฐ€ 2 Page์— ์žˆ๊ณ , 5๊ฐœ์˜ ๊ฒŒ์‹œ๊ธ€์„ ๊ฐ€์ ธ์˜ค๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.

{
    "errorCode": 0,
    "title": "",
    "message": "",
    "data": {
        "total": 7,
        "list": [
            {
                "id": 6,
                "title": "Spring์ด ์ข‹์•„!",
                "contents": "๋„˜๋‚˜๋„˜๋‚˜!"
            },
            {
                "id": 7,
                "title": "mariadb ์ตœ๊ณ !!",
                "contents": "์ข‹์•„์š”!"
            }
        ],
        "pageNum": 2,
        "pageSize": 5,
        "size": 2,
        "startRow": 6,
        "endRow": 7,
        "pages": 2,
        "prePage": 1,
        "nextPage": 0,
        "isFirstPage": false,
        "isLastPage": true,
        "hasPreviousPage": true,
        "hasNextPage": false,
        "navigatePages": 8,
        "navigatepageNums": [
            1,
            2
        ],
        "navigateFirstPage": 1,
        "navigateLastPage": 2
    },
    "success": true
}


Controller์—์„œ Client๊ฐ€ pageNum์ด๋‚˜, pageSize๋ฅผ ๋ณด๋‚ด์ง€ ์•Š์•˜๋‹ค๋ฉด 400 Error๊ฐ€ ํ„ฐ์ง€๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.
์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด ํ•œ๋ฒˆ Test ํ•ด ๋ณผ๊ฒŒ์š”.


๋จผ์ € pageNum์— ๊ฐ’์ด ๋“ค์–ด์˜ค์ง€ ์•Š์•˜์„ ๋•Œ ์œ„์™€ ๊ฐ™์ด 400 Error๊ฐ€ ํ„ฐ์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


๋งˆ์ฐฌ๊ฐ€์ง€๋กœ pageSize๋„ ๋“ค์–ด์˜ค์ง€ ์•Š๋Š”๋‹ค๋ฉด 400 Error๊ฐ€ ๋ฐ˜ํ™˜๋˜๋„๋ก ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ์—” ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์ด ์ œ๋Œ€๋กœ ๊ตฌํ˜„ ๋˜๋Š”์ง€ ํ•œ๋ฒˆ ํ™•์ธ ํ•ด ๋ณผ๊ฒŒ์š”.


DB Table ๋‚ด์šฉ


์—ฌ๊ธฐ์„œ ์ œ๋ชฉ์ด JAVA ๋งŒ์„ธ๋ผ๋Š” ๋‚ด์šฉ์„ ๊ฒ€์ƒ‰ํ•ด ๋ณผ๊ฒŒ์š”.


JAVA๋ผ๋Š” ๊ฐ’๋งŒ ๋„ฃ์–ด์„œ ๋‹ค์‹œ ๊ฒ€์ƒ‰์„ ํ•ด ๋ณผ๊นŒ์š”?


์ด๋ ‡๊ฒŒ JAVA๋งŒ ๋„ฃ์—ˆ๋Š”๋ฐ๋„ JAVA ๋งŒ์„ธ๊ฐ€ ๊ฒ€์ƒ‰๋˜๋Š” ์ด์œ ๋Š” SQL์„ ์ ์€ XML์— CONCAT('%', #{~~}, '%')๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ, ์ด ๊ฒƒ์€ ์•ž,๋’ค์— ์ค‘๊ฐ„ ๋‚ด์šฉ์„ ํฌํ•จํ•˜๋Š” ๋ชจ๋“  ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ค๋ผ๋Š” ์˜๋ฏธ์—์š”.

 

 

 

 

 

 

๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ํ”„๋กœ๊ทธ๋ž˜๋ฐ:JDBC๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ์‰ฝ๊ณ  ๋น ๋ฅธ ์ž๋ฐ” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ”„๋ ˆ์ž„์›Œํฌ

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•