-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
좋았던 것(Liked)
- 없음
배운 것(Learned)
- 게시판 만들기
(0) 사전작업
- MySQL 웹서버 적재 작업
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/playdata_encore
spring.datasource.username=root
spring.datasource.password=sql4869
# create (서버 재실행 시) db 날림
# update (서버 재실행 시) db 유지
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
(1) 입력
- new.mustache
{{>layouts/header}}
<form class="container" action="/articles/create" method="post">
<div class="mb-3">
<label class="form-label">제목</label>
<input type="text" class="form-control" name="title">
</div>
<div class="mb-3">
<label class="form-label">내용</label>
<textarea class="form-control" rows="15" name="content"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<a href="/articles">Back</a>
</form>
{{>layouts/footer}}
@GetMapping("/articles/{id}/edit")
public String edit(@PathVariable Long id, Model model){
Article articleEntity = articleRepositoy.findById(id).orElse(null);
model.addAttribute("articles", articleEntity);
return "articles/edit";
}
(2) 게시판 전체 보이기
- index.mustache
{{>layouts/header}}
<table class="table">
<thread>
<tr>
<th scope="col">Id</th>
<th scope="col">Title</th>
<th scope="col">Content</th>
</tr>
</thread>
<tbody>
{{#articlelist}}
<tr>
<th>{{id}}</th>
<th><a href ="/articles/{{id}}">{{title}}</a></th>
<th>{{content}}</th>
</tr>
{{/articlelist}}
</tbody>
</table>
<a href="/articles/new">글쓰기</a>
{{>layouts/footer}}
@GetMapping("/articles")
public String index(Model model){
// 1. 모든 데이터 가져오기
List<Article> articleEntityList = (List<Article>) articleRepositoy.findAll();
// 2. 모델에 데이터 등록
model.addAttribute("articlelist", articleEntityList);
// 3. 뷰 전송
return "articles/index";
}
(3) 게시판 선택
- show.mustache
{{>layouts/header}}
<table class="table">
<thread>
<tr>
<th scope="col">Id</th>
<th scope="col">Title</th>
<th scope="col">Content</th>
</tr>
</thread>
<tbody>
<!--{{#articles}}{{/articles}}
#-> 시작
/-> 끝
시작과 끝 사이에 가져온 데이터를 지정
-->
{{#articles}}
<tr>
<th>{{id}}</th>
<th>{{title}}</th>
<th>{{content}}</th>
</tr>
{{/articles}}
</tbody>
</table>
<a href="/articles" class="btn btn-primary">메인</a>
<a href="/articles/{{articles.id}}/edit" class="btn btn-primary">수정</a>
<a href="/articles/{{articles.id}}/delete" class="btn btn-danger">삭제</a>
{{>layouts/footer}}
@GetMapping("/articles/{id}")
public String show(@PathVariable Long id, Model model){
log.info("id = " + id);
// id를 이용해서 데이터 조회
Article articleEntity = articleRepositoy.findById(id).orElse(null);
// id 값으로 데이터를 찾음 -> id가 없으면 null 리턴
// id 값이 있으면 articleEntity 변수에 값을 리턴
System.out.println(articleEntity);
// model에 데이터 등록
model.addAttribute("articles", articleEntity);
return "articles/show";
}
(2) 수정
- edit.mustache
{{>layouts/header}}
{{#articles}}
<form class="container" action="/articles/update" method="post">
<input name="id" type="hidden" value="{{id}}">
<div class="mb-3">
<label class="form-label">제목</label>
<input type="text" class="form-control" name="title" value="{{title}}">
</div>
<div class="mb-3">
<label class="form-label">내용</label>
<textarea class="form-control" rows="10" name="content">{{content}}</textarea>
</div>
<button type="submit" class="btn btn-primary">전송</button>
<a href="/articles/{{id}}" class="btn btn-primary">뒤로가기</a>
</form>
{{/articles}}
{{>layouts/footer}}
@PostMapping("/articles/update")
public String update(ArticleFrom from){
Article article = from.toEntity();
log.info(article.toString());
Article target = articleRepositoy.findById(article.getId()).orElse(null);
if(target != null){
articleRepositoy.save(article);
}
return "redirect:/articles/"+article.getId();
}
(3) 삭제
@GetMapping("/articles/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes rttr){
Article target = articleRepositoy.findById(id).orElse(null);
if(target != null){
articleRepositoy.delete(target);
rttr.addFlashAttribute("msg", "삭제완료");
}
return "redirect:/articles";
}
부족했던 것(Lacked)
- 없음
바라는 것(Longed for)
- 없음
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels