Mapper 처리 및 테스트 -> Service 처리 및 테스트 -> Controller 처리 -> View 

 

 

 

[ RecipeMapper ]

/* 게시판 수정 */
public int modify(RecipeVO board);

 

 

[ RecipeMapper.xml ]

수정 할 내용들만 넣기

<!-- 게시판 수정 -->
<update id="modify">
    update recipe set title = #{title}, ingredient = #{ingredient}, content = #{content}, updatedate = now() 
    where id = #{id}
</update>

MySQL 에는 updatedate = now()

ORACLE 에는 updateDate = sysdate 

 

 

 

Mapper 테스트하기

xml에서 업데이트 해야하는 변수들 가져오기

/* 게시판 수정 */
RecipeVO board = new RecipeVO();
board.setId(1);
board.setTitle("수정 된 제목");
board.setIngredient("수정 된 재료");
board.setContent("수정 된 내용");

int result = recipeMapper.modify(board);

log.info("결과 : "+result);

→ true니까 1 이 값으로 나옴

 

 

DB에도 수정 된 걸 볼 수 있다.

 

 

 


 

[ RecipeService = RecipeMpper ]

/* 게시판 수정 */
public int modify(RecipeVO board);

 

[ ServiceImpl ]

@Override
public int modify(RecipeVO board) {
   return recipeMapper.modify(board);
}

 


Controller (get매핑, post매핑)

//게시판 글 수정
@GetMapping("/modify")
public String recipeModifyGet(@RequestParam("id") int id, Model model) {
    model.addAttribute("board", recipeService.getPage(id));
    return "modify";
}
@PostMapping("/modify")
public String recipeModifyPost(RecipeVO boardVo,RedirectAttributes attr) {
   recipeService.modify(boardVo);
   attr.addFlashAttribute("message", "수정 성공!");
   return "redirect:/recipe/board/list";
}

 


modify 뷰페이지 만들기

[ modify.html ]

enroll 뷰페이지 복붙해서 수정

<div class="card-header">
    <h4 class="font-weight-bolder">게시판 수정</h4>
    <p class="mb-0">게시물을 수정하세요</p>
</div>
<div class="card-body bg-white">
    <form role="form" th:action="@{/recipe/board/enroll}" method="post" th:object="${board}">

        <div class="form-group col-md-6" is-filled>
            <label for="inputEmail4">제목</label>
            <input type="text" class="form-control" id="inputEmail4" style="display: block;" th:field="*{title}" required />
        </div>  
        <div class="form-group col-md-6">
            <div for="inputEmail4" class="input-group input-group-dynamic">
            <textarea th:field="*{ingredient}" class="form-control" rows="3" placeholder="재료를 적어주세요." spellcheck="false" required ></textarea>
            </div>
        </div>
        <div class="form-group col-md-6">
            <div for="inputEmail4" class="input-group input-group-dynamic">
            <textarea th:field="*{content}" class="form-control" rows="5" placeholder="내용을 적어주세요." spellcheck="false" required ></textarea>
            </div>
        </div>

        <div class="form-group col-md-6" is-filled>
            <label class="form-label">작가</label>
            <input type="text" class="form-control" th:field="*{writer}" readonly />
        </div>
        <div class="text-center">
            <button type="submit" class="btn btn-lg bg-primary">수정하기</button>
        </div>
    </form>
</div>

* is-filled : 제목표시.. 겹치지않게
* readonly : 수정안됨 읽기만

제목, 작가에 css로 is-filled 추가하기  (무슨 차이인지 잘 모르겠지만..)
 
작가(writer) 는 수정 안하니까 requried -> readonly 로 바꾸기
 

 

 

!! 수정하기 버튼 누를때 수정페이지로 

 

[ get.html ]

파리미터는 괄호안에!

 <a th:href="@{/recipe/board/modify(id=${board.id})}" class="btn btn-secondary">수정</a>

 

 

수정 취소하기 버튼도 추가하자!

<a th:href="@{/recipe/board/list}" class="btn btn-lg btn-danger">수정 취소</a>

 

 


수정 했는데 수정이 X , 새로 만들어짐

☆ modify.html 에 id를 넣어줘야 함!!

 <input type="hidden" th:field="*{id}">

 

근데도 또 새로 만들어진다?? 

코드를 다시 살펴보았더니... form의 보내는 곳이 enroll로 돼 있었다..   

<form role="form" th:action="@{/recipe/board/modify}" 로 수정.!!

 
해결. 수정하면 수정됨!!!
 

 

               

 

반응형
LIST

+ Recent posts