📅2024. 01. 29 36일차
@Autowired
private ArticleService articleService;
public UsrArticleController() {
}
// 액션 메서드
@RequestMapping("/usr/article/getArticle")
@ResponseBody
public Object getArticleAction(int id) {
Article article = articleService.getArticle(id);
if (article == null) {
return id + "번 글은 존재하지 않습니다.";
}
return article;
}
@RequestMapping("/usr/article/doModify")
@ResponseBody
public Object doModify(int id, String title, String body) {
Article article = articleService.getArticle(id);
if (article == null) {
return id + "번 글은 존재하지 않습니다.";
}
articleService.modifyArticle(id, title, body);
return article;
}
@RequestMapping("/usr/article/doDelete")
@ResponseBody
public String doDelete(int id) {
Article article = articleService.getArticle(id);
if (article == null) {
return id + "번 글은 존재하지 않습니다.";
}
articleService.deleteArticle(id);
return id + "번 글이 삭제 되었습니다";
}
@RequestMapping("/usr/article/doWrite")
@ResponseBody
public Article doWrite(String title, String body) {
Article article = articleService.writeArticle(title, body);
return article;
}
@RequestMapping("/usr/article/getArticles")
@ResponseBody
public List<Article> getArticles() {
return articleService.getArticles();
}
}
public interface ArticleRepository {
@Insert("""
INSERT INTO
article SET
regDate = NOW(),
updateDate = NOW(),
title = #{title}, `body` = #{body}
""")
public void writeArticle(String title, String body);
@Select("SELECT LAST_INSERT_ID()")
public int getLastInsertId();
@Select("SELECT * FROM article WHERE id = #{id}")
public Article getArticle(int id);
@Delete("DELETE FROM article WHERE id = #{id}")
public void deleteArticle(int id);
@Update("UPDATE article SET updateDate = NOW(), title = #{title}, `body` = #{body} WHERE id = #{id}")
public void modifyArticle(int id, String title, String body);
@Select("SELECT * FROM article ORDER BY id DESC")
public List<Article> getArticles();
}
<update id="modifyArticle">
UPDATE article
<set>
<if test="title != null and title != ''">title = #{title},</if>
<if test="body != null and body != ''">`body` = #{body},</if>
updateDate = NOW()
</set>
WHERE id = #{id}
</update>