IT/development

[mybatis] 검색(Oracle)

알 수 없는 사용자 2023. 5. 6. 16:56
반응형
<!--게시글 목록 조회-->
<select id="selectBoards" parameterType="BoardVo" resultType="BoardVo">
    /* 게시글 목록 조회 */
    SELECT ROWNUM AS no
         , T1.boardNo
         , T1.title
         , T1.content
         , T1.writer
         , T1.createDate
         , T1.updateDate
      FROM
          (SELECT BOARD_NO AS boardNo
                , TITLE AS title
                , CONTENT AS content
                , WRITER AS writer
                , TO_CHAR(CREATE_DATE, 'YYYY-MM-DD') AS createDate
                , TO_CHAR(UPDATE_DATE, 'YYYY-MM-DD') AS updateDate
             FROM BOARD
            WHERE 1=1
              AND USE_YN = 'Y'
              <include refid="searchParam"/>
            ORDER BY CREATE_DATE DESC
          )T1
     WHERE 1=1
</select>
<!-- 검색 parameter값 -->
<sql id="searchParam">
    <!-- 제목 -->
    <if test="type == 'T'.toString()">
        AND TITLE LIKE '%' || #{keyword} || '%'
    </if>
    <!-- 내용 -->
    <if test="type == 'C'.toString()">
        AND CONTENT LIKE '%' || #{keyword} || '%'
    </if>
    <!-- 작성자 -->
    <if test="type == 'W'.toString()">
        AND WRITER LIKE '%' || #{keyword} || '%'
    </if>
</sql>
package study.example.thboard.vo;

import lombok.Data;

@Data
public class BoardVo extends CommonVo{

    private int no;
    private int boardNo;
    private String keyword;
    private String type;
    private String title;
    private String content;
    private String writer;
    private String useYn;
}
CREATE TABLE "BOARD" 
   ("BOARD_NO" NUMBER(10,0) NOT NULL ENABLE, 
	"TITLE" VARCHAR2(50) NOT NULL ENABLE, 
	"CONTENT" VARCHAR2(4000) NOT NULL ENABLE, 
	"WRITER" VARCHAR2(20) NOT NULL ENABLE, 
	"CREATE_DATE" DATE NOT NULL ENABLE, 
	"UPDATE_DATE" DATE DEFAULT SYSDATE, 
	"USE_YN" CHAR(1) DEFAULT 'Y', 
	 CONSTRAINT "BOARD_PK" PRIMARY KEY ("BOARD_NO")
);
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layouts/default_layout}">

    <script layout:fragment="script" th:inline="javascript" type="text/javascript">
        $(document).ready(function () {
            //글쓰기
            $("#btnSave").on("click", function () {
                $("#frm").attr("action", "/regForm").submit();
            });

            //검색
            $("#btnSearch").on("click", function () {
                $("#frm").submit();
            });
        });

        //삭제
        function del(no) {
            let boardNo = $("<input>").attr("type", "hidden").attr("name", "boardNo").val(no);
            $("#frm").attr("method", "post")
                     .attr("action", "del")
                     .append(boardNo)
                     .submit();
        }
    </script>

<!-- Content -->
<div layout:fragment="content">
      <form id="frm" th:object="${boardVo}" action="" method="get">
      <main>
        <div class="container-fluid px-4">
          <h1>게시글 관리</h1>

            <div class="input-group mb3">
                <select name="type" th:field="*{type}">
                    <option value="">--</option>
                    <option value="W">작성자</option>
                    <option value="T">제목</option>
                    <option value="C">내용</option>
                </select>
                <input type="text" name="keyword" th:field="*{keyword}">
                <button type="button" id="btnSearch" class="btn btn-secondary">검색</button>
            </div>

            <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>순번</th>
                                            <th>제목</th>
                                            <th>내용</th>
                                            <th>작성자</th>
                                            <th>등록일</th>
                                            <th>수정일</th>
                                            <th></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr th:if="${#lists.size(list) >0}" th:each="list : ${list}">
                                            <td th:text="${list.no}"></td>
                                            <td><a th:href="@{/detail(boardNo=${list.boardNo})}" th:text="${list.title}"></a></td>
                                            <td th:text="${list.content}"></td>
                                            <td th:text="${list.writer}"></td>
                                            <td th:text="${list.createDate}"></td>
                                            <td th:text="${list.updateDate}"></td>
                                            <td><a href="#" th:href="'javascript:del('+${list.boardNo}+')'" class="btn btn-danger">삭제</a></td>
                                        </tr>
                                    </tbody>
                                </table>
        </div>

          <button type="button" id="btnSave" class="btn btn-info">글쓰기</button>

      </main>
      </form>
</div>
</html>
    /**
     * 게시글 목록 조회
     * @return
     */
    @GetMapping(value = "")
    public ModelAndView main(@ModelAttribute BoardVo boardVo) {
        ModelAndView mv = new ModelAndView("pages/index");
        List<BoardVo> boardList = null;

        try {
            boardList = boardService.getBoards(boardVo);
            mv.addObject("list", boardList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
반응형