본문 바로가기
백엔드(Back-End)/Spring Boot

[STS4-Spring Boot] 게시판 검색 기능

by 기딩 2024. 2. 12.
728x90

Criteria

package com.board.paging;

import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

public class Criteria {
	private int currentPageNo;
	private int recordsPerPage;
	private int pageSize;
	private String searchKeyword;
	private String searchType;
	
	public Criteria() {
		this.currentPageNo = 1;
		this.recordsPerPage = 10;
		this.pageSize = 10;
	}
	
	public String makeQueryString(int pageNo) {
		UriComponents uriComponents = UriComponentsBuilder.newInstance()
				.queryParam("currentPageNo", pageNo)
				.queryParam("recordsPerPage", recordsPerPage)
				.queryParam("pageSize", pageSize)
				.queryParam("searchType", searchType)
				.queryParam("searchKeyword", searchKeyword)
				.build()
				.encode();
		
		return uriComponents.toUriString();
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public int getRecordsPerPage() {
		return recordsPerPage;
	}

	public void setRecordsPerPage(int recordsPerPage) {
		this.recordsPerPage = recordsPerPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getSearchKeyword() {
		return searchKeyword;
	}

	public void setSearchKeyword(String searchKeyword) {
		this.searchKeyword = searchKeyword;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	@Override
	public String toString() {
		return "Criteria [currentPageNo=" + currentPageNo + ", recordsPerPage=" + recordsPerPage + ", pageSize="
				+ pageSize + ", searchKeyword=" + searchKeyword + ", searchType=" + searchType + "]";
	}
}
package com.board.util;

import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;

import com.board.constant.Method;
import com.board.paging.Criteria;

@Controller
public class UiUtils {
	
	// 혹시 안 쓰일 수도 있으니 false
	public String showMessageWithRedirect(@RequestParam(value = "message", required = false) String message,
										  @RequestParam(value = "redirectUri", required = false) String redirectUri,
										  @RequestParam(value = "method", required = false) Method method,
										  @RequestParam(value = "params", required = false) Map<String, Object> params,
										  Model model) {
		
		model.addAttribute("message", message);
		model.addAttribute("redirectUri", redirectUri);
		model.addAttribute("method", method);
		model.addAttribute("params", params);
		
		return "utils/message-redirect";
	}
	
	public Map<String, Object> getPagingParams(Criteria criteria) {
		
		Map<String, Object> params = new LinkedHashMap<>();
		params.put("currentPageNo", criteria.getCurrentPageNo());
		params.put("recordsPerPage", criteria.getRecordsPerPage ());
		params.put("pageSize", criteria.getPageSize());
		params.put("searchType", criteria.getSearchType());
		params.put("searchKeyword", criteria.getSearchKeyword());
		
		return params;
	}
}

 

 

Common Mapper 

<?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="CommonMapper">

	<sql id="paging">
		LIMIT
			#{paginationInfo.firstRecordIndex}, #{recordsPerPage}
	</sql>
	
	<sql id="search">
	<!-- 검색 키워드 있을 때 -->
		<if test="searchKeyword != null and searchKeyword != ''">
			<choose>
				<!-- 검색 유형 있을 때 -->
				<when test="searchType != null and searchType != ''">
					<choose>
						<when test="'title'.equals(searchType)">
							AND title LIKE CONCAT('%', #{searchKeyword}, '%')
						</when>
						<when test="'content'.equals(searchType)">
							AND content LIKE CONCAT('%', #{searchKeyword}, '%')
						</when>
						<when test="'writer'.equals(searchType)">
							AND writer LIKE CONCAT('%', #{searchKeyword}, '%')
						</when>
					</choose>
				</when>
				<!-- 검색 유형 없을 때 -->
				<otherwise>
					AND
						(
							   title LIKE CONCAT('%', #{searchKeyword}, '%')
							OR content LIKE CONCAT('%', #{searchKeyword}, '%')
							OR writer LIKE CONCAT('%', #{searchKeyword}, '%')
						)
				</otherwise>
			</choose>
		</if>
	</sql>

</mapper>

 

게시판 Mapper - select문 수정

<select id="selectBoardList" parameterType="BoardDTO" resultType="BoardDTO">
		SELECT
			<include refid="boardColumns" />
		FROM
			board
		WHERE
			delete_yn = 'N'
			<include refid="CommonMapper.search" />
		ORDER BY
			notice_yn ASC,
			idx DESC,
			insert_time DESC
			<include refid="CommonMapper.paging" />
	</select>
	
	<select id="selectBoardTotalCount" parameterType="BoardDTO" resultType="int">
		SELECT
			COUNT(*)
		FROM
			board
		WHERE
			delete_yn = 'N'
		<include refid="CommonMapper.search" />
	</select>
<th:block layout:fragment="script">
		<script th:inline="javascript"> 
			/* <![CDATA[ */
				function movePage(uri, queryString) {
					location.href = uri + queryString;
				}
				
				function searchBoard(form) {
					/*[- 드롭다운이 아닌 메인 검색 키워드로 검색했을 때 -]*/
					if (isEmpty(form) == true) {
						var searchKeyword = document.getElementById("mainSearchKeyword");
						if (isEmpty(searchKeyword.value) == true) {
							alert("키워드를 입력해 주세요.");
							searchKeyword.focus();
							return false;
						}
						
						form = document.getElementById("searchForm");
						form.searchKeyword.value = searchKeyword.value;
						form.submit();
					}
					
					if (isEmpty(form.searchKeyword.value) == true) {
						alert("키워드를 입력해 주세요.");
						form.searchKeyword.focus();
						return false;
					}
				}
			/* ]]> */
		</script>
	</th:block>

728x90