์ƒˆ์†Œ์‹

๐Ÿ“šStudy/ETC

[java] Page Interceptor

  • -

 

 

ํŽ˜์ด์ง€ ์ฒ˜๋ฆฌํ•˜๊ธฐ

์›น ํ”„๋กœ์ ํŠธ๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ์œผ๋ฉด, ๋ฐ์ดํ„ฐ ์ฆ๊ฐ€๋กœ ์ธํ•ด ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์„ ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์—†๋‹ค.

ํŠน์ •ํ•œ ๊ฐœ์ˆ˜๋งŒํผ ๋ณด์—ฌ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋…์„ฑ๋„ ๋†’์„ ๋ฟ๋”๋Ÿฌ ์ด์— ๋”ฐ๋ฅธ ์ข‹์€ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค.(์ ์€ ๋ฐ์ดํ„ฐ ํ˜ธ์ถœ๋กœ ์ธํ•œ ํผํฌ๋จผ์Šค ํ–ฅ์ƒ)

 

๊ณ ์ „์ ์ธ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ

legacy ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณด๋‹ค๋ณด๋ฉด ๊ฐ Controller ๋˜๋Š” Service์—์„œ page ์— ๋Œ€ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐœ๋ฐœํ•ด๋†“์•˜๋‹ค.

//legacy ํ”„๋กœ์ ํŠธ์—์„œ ๋™์ž‘ ์ค‘์ด๋˜ ํŽ˜์ด์ง€ ํ˜ธ์ถœ Fun
final int boardSize = 10; // ํ•œ ํŽ˜์ด์ง€์— ๊ฒŒ์‹œ๊ธ€ ์ถœ๋ ฅ ๊ฐœ์ˆ˜

String pageNumber = request.getParameter("pageNumber");
if (pageNumber == null) {
    pageNumber = "1";
}

int startRow = (pageNumber - 1) * boardSize + 1;
int endRow = pageNumber * boardSize;

//LogAspect.logger.info(LogAspect.logMsg + " ํŽ˜์ด์ง€๋ฒˆํ˜ธ : " + pageNumber + ",์‹œ์ž‘๋ฒˆํ˜ธ : " + startRow + ",๋๋ฒˆํ˜ธ : " + endRow);

// ๊ฒ€์ƒ‰ ์Šคํƒ€ํŠธ
int count = boardDao.getBoardCount(searchType,searchData,sDate,eDate,company_seq,progress_status,ptn_comp_seq); // ์ „์ฒด ์นด์šดํŠธ
LogAspect.logger.info(LogAspect.logMsg + " ๋ฆฌ์ŠคํŠธ ๊ฐœ์ˆ˜ : " + count);

int real_count = 0;
List<BoardDto> boardList = null;

if (count > 0) {
    endRow = 10;
    boardList = boardDao.boardList(startRow,endRow,searchType,searchData,sDate,eDate,company_seq,progress_status,ptn_comp_seq);
    LogAspect.logger.info(LogAspect.logMsg + " boardList ๊ฒฐ๊ณผ ๊ฐ’ : " + boardList.size() + "\n");
    mav.addObject("boardListSize",boardList.size());
    ...
}
mav.addObject("count",count); //์ „์ฒด ๊ฑด์ˆ˜
mav.addObject("boardSize",boardSize); // ํ•œ ํŽ˜์ด์ง€์— ๊ฒŒ์‹œ๊ธ€ ์ถœ๋ ฅ ๊ฐœ์ˆ˜
mav.addObject("pageNumber",currentPage); //ํ˜„์žฌ ํŽ˜์ด์ง€

legacy project ไธญ

 

์ „์—ญ ์„ค์ •์— ๋Œ€ํ•œ ๊ณ ๋ฏผ

 ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ๋Š” ํ•œ ๊ณณ์—์„œ๋งŒ ์“ฐ์ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— '์žฌํ™œ์šฉ์ด ๊ฐ€๋Šฅ'ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์— ๋ชฉํ‘œ๋ฅผ ๋‘์—ˆ๋‹ค.

 

๋ฌผ๋ก  Controller ๋‚˜ Service ์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก Util ํด๋ž˜์Šค๋ฅผ ๋‘์–ด ๋ณ„๋„๋กœ ๊ตฌํ˜„ํ•ด๋„ ์œ„ ๋ชฉํ‘œ์— ์ ํ•ฉํ•˜์ง€๋งŒ, Spring ์—์„œ๋Š” ์ธํ„ฐ์…‰ํ„ฐ(Interceptor) ๋ผ๋Š” ๋Œ€์•ˆ์ด ์žˆ์–ด ์ด๋ฅผ ํ™œ์šฉํ•ด์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

Page Helper

์ด ๊ธ€์—์„œ๋Š” com.github.pagehelper ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์‚ฌ์šฉํ•ด์„œ ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค.

maven dependency

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.3</version>
</dependency>

 

 

๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ง„ํ–‰

    @Test
    void paging(){
        int pageSize = 10;      //NOTE: ํŽ˜์ด์ง€ ๋‹น ๊ฐœ์ˆ˜
        PageHelper.startPage(1, pageSize);  //DESC: 1 ํŽ˜์ด์ง€์˜ 10๊ฐœ
        Page<UserVO> p = (Page<UserVO>) groupMapper.userList();
        log.info("์ „์ฒด ๋ฐ์ดํ„ฐ: {}", p.getTotal());
        log.info("์ „์ฒด ํŽ˜์ด์ง€: {}", p.getPages());
        log.info("ํ˜„์žฌ ํŽ˜์ด์ง€: {}", p.getPageNum());
    }

 

๊ฒฐ๊ณผ ๊ฐ’

๊ฒฐ๊ณผ ๊ฐ’์—์„œ ๋ณด๋“ฏ ์ „์ฒด์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ๊ฐฏ์ˆ˜์™€ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋”ฐ๋ฅธ ํŽ˜์ด์ง€๋ฅผ ๋ฏธ๋ฆฌ ๊ตฌํ•˜๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

(Page Helper ์—์„œ ์ฟผ๋ฆฌ ๋‚ด LIMIT ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๊ธฐ ์ „, ์ „์ฒด ์ฟผ๋ฆฌ์˜ count๋ฅผ ์กฐํšŒํ•ด ์ „์ฒด ํŽ˜์ด์ง€ ์ •๋ณด์™€ ๋ณด์—ฌ์ค„ ํŽ˜์ด์ง€๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹)

์œ„ ์ฝ”๋“œ ์‹คํ–‰ ์‹œ ์ง„ํ–‰๋˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

-- ์ „์ฒด ๊ฐฏ์ˆ˜์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌํ•œ๋‹ค.
Connection.prepareStatement: 
SELECT count(0) 
FROM TB_USER

-- ํŽ˜์ด์ง€์— ํ•ด๋‹นํ•˜๋Š” ์ž๋ฃŒ๋ฅผ ์กฐํšŒํ•œ๋‹ค. (setInt์˜ ๊ฐ’์„ ์ฃผ์–ด์ง„ ์ดˆ๊ธฐ๊ฐ’์— ๋”ฐ๋ผ ๊ณ„์‚ฐํ•ด์„œ ์ ์šฉ)
PreparedStatement.setInt(1, 10): 
SELECT TG.GROUP_NAME AS GROUPNAME
FROM TB_USER 
ORDER BY TU.REG_DATE DESC 
LIMIT 10 //setInt ๋œ ๋‚ด์šฉ์€ ์ด ๊ณณ์— ์ ์šฉ๋œ๋‹ค.
๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.