Spring MySQL 이전글, 다음글 조회 기능 개발

MySQL 쿼리

이전글 조회 쿼리

<select id="getPrevContentId" parameterType="java.util.HashMap" resultType="java.util.HashMap">
    SELECT
        max(id) prevId
    FROM
        테이블명
    WHERE
        id <![CDATA[<]]> #{현재글Id}
    ORDER BY
        create_date DESC
    LIMIT 1
</select>

다음글 조회 쿼리

<select id="getNextContentId" parameterType="java.util.HashMap" resultType="java.util.HashMap">
    SELECT
        min(id) nextId
    FROM
        테이블명
    WHERE
        id <![CDATA[>]]> #{현재글Id}
    ORDER BY
        create_date DESC
    LIMIT 1
</select>

Controller

이전글 조회 예시

@ResponseBody
@PostMapping("/getPrevContentId")
public Map<String, Object> getPrevContentId(@RequestBody Map<String,Object> paramMap) {
    HashMap<String, Object> resultMap = new HashMap<>();

    try {
        resultMap.put("contentMap", contentService.getPrevContentId(paramMap));
        resultMap.put("success", true);
    }catch (Exception e) {
        resultMap.put("success", false);
    }

    return resultMap;
}

다음글 조회 예시

@ResponseBody
@PostMapping("/update/getNextContentId")
public Map<String, Object> getNextContentId(@RequestBody Map<String,Object> paramMap) {
    HashMap<String, Object> resultMap = new HashMap<>();

    try {
        resultMap.put("contentMap", contentService.getNextContentId(paramMap));
        resultMap.put("success", true);
    }catch (Exception e) {
        resultMap.put("success", false);
    }

    return resultMap;
}

Service

이전글 조회 예시

public Map<String, Object> getPrevContentId(Map<String, Object> paramMap) throws Exception {
    return contentMapper.getPrevContentId(paramMap);
}

다음글 조회 예시

public Map<String, Object> getNextContentId(Map<String, Object> paramMap) throws Exception {
    return contentMapper.getNextContentId(paramMap);
}

Mapper

이전글 조회 예시

Map<String, Object> getPrevContentId(Map<String, Object> paramMap) throws Exception;

다음글 조회 예시

Map<String, Object> getNextContentId(Map<String, Object> paramMap) throws Exception;

Javascript

이전글 조회 후 이동 처리

function goPrevPage() {
    const errMsg = "이전 페이지로 이동할 수 없습니다. 관리자에게 문의해주세요.";

    const pathArray = window.location.pathname.split('/');

    fetch('/content/getPrevContentId', {
        method: 'POST',
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            "id": pathArray[pathArray.length-1]
        })
    })
    .then((response) => response.json())
    .then((data) => {
        if(data.success == true) {
            // 정상 처리
            contentMap = data.contentMap;

            if(contentMap == null) {
                alert("이전 페이지가 없습니다.");
                return;
            }

            // 이전 페이지 이동
            location.href = "/content/update/" + contentMap.prevId;

        }else {
            // 에러 처리
            alert(errMsg);
        }
    })
    .catch((error) => {
        // 에러 처리
        alert(errMsg);
    })
}

다음글 조회 후 이동 처리

function goNextPage() {
    const errMsg = "다음 페이지로 이동할 수 없습니다. 관리자에게 문의해주세요.";

    const pathArray = window.location.pathname.split('/');

    fetch('/content/update/getNextContentId', {
        method: 'POST',
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            "id": pathArray[pathArray.length-1]
        })
    })
    .then((response) => response.json())
    .then((data) => {
        if(data.success == true) {
            // 정상 처리
            contentMap = data.contentMap;

            if(contentMap == null) {
                alert("다음 페이지가 없습니다.");
                return;
            }

            location.href = "/content/update/" + contentMap.nextId;

        }else {
            // 에러 처리
            alert(errMsg);
        }
    })
    .catch((error) => {
        // 에러 처리
        alert(errMsg);
    })
}

Leave a comment