티스토리 뷰

pageable을 사용하면 페이징 작업을 좀 더 간편하게 개발할 수 있다.

 

해당 프로젝트는 restcontroller - service - serviceimpl - mapper.java - mapper.xml 기준이다.

 

 

- xml dependency 추가한다.

<dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-commons</artifactId>
       <version>2.2.9.RELEASE</version>
</dependency>

 

 

- restController.java

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;


@RequestMapping(value ="/rest/List")
@ResponseBody
public Map<String, Object> list(@RequestParam Map<String, Object> paramMap, @PageableDefault(value = 20) Pageable page){
Map<String,Object> resultMap =new HashMap<String, Object>();
Page<Map<String, Object>> result = ListService.list(paramMap, page);
resultMap.put("pages", result);
resultMap.put("size", page.getPageSize());
return resultMap;
}

 

 

- service.java

public interface ListService {
    Page<Map<String, Object>> list (Map<String, Object> paramMap, Pageable page);
}

 

- serviceImpl.java

@Override
public Page<Map<String, Object>> list(Map<String, Object> paramMap, Pageable page) {

paramMap.put("offset", page.getOffset());
paramMap.put("pageSize", page.getPageSize());

List<Map<String, Object>> contents = listMapper.list(paramMap);
int count = listMapper.listCnt(paramMap);
return new PageImpl<>(contents, page, count);
}

 

- mapper.java

@Mapper
public interface ListMapper {
    int listCnt (Map<String, Object> paramMap);
    List<Map<String, Object>> list (Map<String, Object> paramMap);
}

 

- mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yj.media.list.mapper.ListMapper">

  <select id="listCnt" parameterType="map" resultType="int">
        /* ListMapper.listCnt*/
        SELECT COUNT(SEQ)
        FROM LIST_TBL
        WHERE MEMBER_ID = #{memberId}
  </select>

  <select id="list" parameterType="map" resultType="cMap">
         /* ListMapper.list*/
        SELECT *
        FROM LIST_TBL
        ORDER BY SORT_COL DESC
        LIMIT ${offset} , ${pageSize}
  </select>
</mapper>

 

 

- return value ( JAVA )

 

- return value ( JS )

'Development > JAVA' 카테고리의 다른 글

[JAVA] Bearer token Authorization  (0) 2021.08.22
[JAVA] URL 한글 인코딩  (0) 2021.08.20
[JAVA] ElasticSearch 키워드 검색  (0) 2021.08.11
[JAVA] XSS Filter Config  (0) 2021.08.03
Spring Boot security OAuth2 연동  (1) 2021.07.27
댓글
링크
최근에 올라온 글
Total
Today
Yesterday