基于SpringBoot和Vue3的博客平臺(tái)發(fā)布、編輯、刪除文章功能實(shí)現(xiàn)
在上一個(gè)教程中,我們已經(jīng)實(shí)現(xiàn)了基于Spring Boot和Vue3的用戶注冊(cè)與登錄功能。本教程將繼續(xù)引導(dǎo)您實(shí)現(xiàn)博客平臺(tái)的發(fā)布、編輯、刪除文章功能。
在這個(gè)博客教程中,我們實(shí)現(xiàn)了基于Spring Boot和Vue3的發(fā)布、編輯、刪除文章功能。整個(gè)實(shí)現(xiàn)過程可以分為以下幾個(gè)步驟:
- 后端Spring Boot實(shí)現(xiàn) 1.1. 創(chuàng)建Article實(shí)體類:定義文章的數(shù)據(jù)結(jié)構(gòu)。 1.2. 創(chuàng)建ArticleMapper接口:定義與數(shù)據(jù)庫交互的操作。 1.3. 創(chuàng)建ArticleService接口及其實(shí)現(xiàn):提供具體的業(yè)務(wù)邏輯,如查詢、創(chuàng)建、更新和刪除文章。 1.4. 創(chuàng)建ArticleController類:定義RESTful API接口,處理HTTP請(qǐng)求和響應(yīng)。
- 前端Vue3實(shí)現(xiàn) 2.1. 創(chuàng)建文章列表頁面組件:展示所有文章列表,以及編輯和刪除文章的操作按鈕。 2.2. 創(chuàng)建文章發(fā)布頁面組件:提供一個(gè)表單供用戶輸入文章標(biāo)題和內(nèi)容,以發(fā)布新文章。 2.3. 創(chuàng)建文章編輯頁面組件:提供一個(gè)表單顯示選定文章的標(biāo)題和內(nèi)容,允許用戶編輯并更新文章。
在這個(gè)過程中,我們首先實(shí)現(xiàn)了后端Spring Boot應(yīng)用程序,負(fù)責(zé)處理數(shù)據(jù)庫操作和提供API接口。然后,我們實(shí)現(xiàn)了前端Vue3應(yīng)用程序,提供用戶界面以展示文章列表、發(fā)布新文章和編輯現(xiàn)有文章。通過這種方式,我們實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的博客平臺(tái),具有發(fā)布、編輯和刪除文章的功能。
1. 后端Spring Boot實(shí)現(xiàn)
我們將使用Spring Boot作為后端框架,并使用MySQL作為數(shù)據(jù)庫。
1.1 創(chuàng)建Article實(shí)體類
首先,在com.example.demo.entity包下創(chuàng)建一個(gè)名為Article.java的新類,并添加以下內(nèi)容:
public class Article { private Integer id; private String title; private String content; private Integer authorId; // Getter and Setter methods }
1.2 創(chuàng)建ArticleMapper接口
在com.example.demo.mapper
包下創(chuàng)建一個(gè)名為ArticleMapper.java
的新接口,并添加以下內(nèi)容:
@Mapper public interface ArticleMapper { List<Article> findAll(); Article findById(Integer id); void insert(Article article); void update(Article article); void delete(Integer id); }
在com.example.demo.service.impl
包下創(chuàng)建一個(gè)名為ArticleServiceImpl.java
的新類,并添加以下內(nèi)容:
@Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleMapper articleMapper; @Override public List<Article> findAll() { return articleMapper.findAll(); } @Override public Article findById(Integer id) { return articleMapper.findById(id); } @Override public void create(Article article) { articleMapper.insert(article); } @Override public void update(Article article) { articleMapper.update(article); } @Override public void delete(Integer id) { articleMapper.delete(id); } }
1.3創(chuàng)建ArticleController類
在com.example.demo.controller
包下創(chuàng)建一個(gè)名為ArticleController.java
的新類,并添加以下內(nèi)容:
@RestController @RequestMapping("/api/article") public class ArticleController { @Autowired private ArticleService articleService; @GetMapping public List<Article> list() { return articleService.findAll(); } @GetMapping("/{id}") public Article detail(@PathVariable Integer id) { return articleService.findById(id); } @PostMapping public Result create(@RequestBody Article article) { articleService.create(article); return Result.success("文章發(fā)布成功"); } @PutMapping("/{id}") public Result update(@PathVariable Integer id, @RequestBody Article article) { article.setId(id); articleService.update(article); return Result.success("文章更新成功"); } @DeleteMapping("/{id}") public Result delete(@PathVariable Integer id) { articleService.delete(id); return Result.success("文章刪除成功"); } }
至此,我們已經(jīng)完成了后端的發(fā)布、編輯、刪除文章功能。
2. 前端Vue3實(shí)現(xiàn)
2.1 創(chuàng)建文章列表頁面組件
在src/views
目錄下創(chuàng)建一個(gè)名為ArticleList.vue
的新組件,并添加以下內(nèi)容:
<template> <div> <el-table :data="articles"> <el-table-column prop="id" label="ID" width="80"></el-table-column> <el-table-column prop="title" label="標(biāo)題"></el-table-column> <el-table-column label="操作" width="180"> <template v-slot="{ row }"> <el-button @click="editArticle(row.id)">編輯</el-button> <el-button type="danger" @click="deleteArticle(row.id)">刪除</el-button> </template> </el-table-column> </el-table> </div> </template> <script> import { ref } from "vue"; import axios from "axios"; export default { setup() { const articles = ref([]); const fetchArticles = async () => { const response = await axios.get("/api/article"); articles.value = response.data; }; const editArticle = (id) => { // 跳轉(zhuǎn)到編輯頁面 }; const deleteArticle = async (id) => { await axios.delete(`/api/article/${id}`); fetchArticles(); }; fetchArticles(); return { articles, editArticle, deleteArticle }; }, }; </script>
2.2 創(chuàng)建文章發(fā)布頁面組件
在src/views
目錄下創(chuàng)建一個(gè)名為CreateArticle.vue
的新組件,并添加以下內(nèi)容:
<template> <el-form ref="form" :model="form" label-width="80px"> <el-form-item label="標(biāo)題" prop="title"> <el-input v-model="form.title"></el-input> </el-form-item> <el-form-item label="內(nèi)容" prop="content"> <el-input type="textarea" v-model="form.content"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm('form')">發(fā)布文章</el-button> </el-form-item> </el-form> </template> <script> import { reactive } from "vue"; import axios from "axios"; export default { setup() { const form = reactive({ title: "", content: "" }); const submitForm = async () => { try { await axios.post("/api/article", form); alert("文章發(fā)布成功"); } catch (error) { alert("文章發(fā)布失敗"); } }; return { form, submitForm }; }, }; </script>
2.3 創(chuàng)建文章編輯頁面組件
在src/views
目錄下創(chuàng)建一個(gè)名為EditArticle.vue
的新組件,并添加以下內(nèi)容:
<template> <el-form ref="form" :model="form" label-width="80px"> <el-form-item label="標(biāo)題" prop="title"> <el-input v-model="form.title"></el-input> </el-form-item> <el-form-item label="內(nèi)容" prop="content"> <el-input type="textarea" v-model="form.content"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm">更新文章</el-button> </el-form-item> </el-form> </template> <script> import { ref, reactive, onMounted } from "vue"; import axios from "axios"; export default { props: { id: { type: Number, required: true, }, }, setup(props) { const form = reactive({ title: "", content: "" }); const fetchArticle = async () => { const response = await axios.get(`/api/article/${props.id}`); form.title = response.data.title; form.content = response.data.content; }; const submitForm = async () => { try { await axios.put(`/api/article/${props.id}`, form); alert("文章更新成功"); } catch (error) { alert("文章更新失敗"); } }; onMounted(fetchArticle); return { form, submitForm }; }, }; </script>
這段代碼定義了一個(gè)名為EditArticle.vue的新組件,該組件需要一個(gè)名為id的屬性。組件加載時(shí),會(huì)調(diào)用fetchArticle函數(shù)獲取文章信息并填充到表單中。點(diǎn)擊"更新文章"按鈕時(shí),會(huì)調(diào)用submitForm函數(shù),將表單數(shù)據(jù)發(fā)送到后端以更新文章。
現(xiàn)在,您已經(jīng)完成了基于Spring Boot和Vue3的發(fā)布、編輯、刪除文章功能的實(shí)現(xiàn)。您可以根據(jù)需要對(duì)這些功能進(jìn)行進(jìn)一步的優(yōu)化和擴(kuò)展。希望本教程對(duì)您有所幫助!
以上就是基于Spring Boot和Vue3的博客平臺(tái)發(fā)布、編輯、刪除文章功能實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot Vue3平臺(tái)文章發(fā)布、編輯、刪除的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Eclipse項(xiàng)目有紅感嘆號(hào)的解決方法
這篇文章主要為大家詳細(xì)介紹了Eclipse項(xiàng)目有紅感嘆號(hào)的解決方法,給出了Eclipse項(xiàng)目有紅感嘆號(hào)的原因,以及如何解決?,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Java實(shí)現(xiàn)泡泡堂對(duì)戰(zhàn)版游戲的示例代碼
本文將利用Java制作經(jīng)典游戲《泡泡堂》,文中使用了MVC模式,分離了模型、視圖和控制器,使得項(xiàng)目結(jié)構(gòu)清晰易于擴(kuò)展,感興趣的可以了解一下2022-04-04Springmvc如何實(shí)現(xiàn)向前臺(tái)傳遞數(shù)據(jù)
這篇文章主要介紹了Springmvc如何實(shí)現(xiàn)向前臺(tái)傳遞數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07使用IntelliJ IDEA2020.2.2 x64 新建java項(xiàng)目并且輸出Hello World
這篇文章主要介紹了使用IntelliJ IDEA2020.2.2 x64 新建java項(xiàng)目并且輸出Hello World,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11JFileChooser實(shí)現(xiàn)對(duì)選定文件夾內(nèi)圖片自動(dòng)播放和暫停播放實(shí)例代碼
這篇文章主要介紹了JFileChooser實(shí)現(xiàn)對(duì)選定文件夾內(nèi)圖片自動(dòng)播放和暫停播放實(shí)例代碼,需要的朋友可以參考下2017-04-04java使用htmlparser提取網(wǎng)頁純文本例子
這篇文章主要介紹了java使用htmlparser提取網(wǎng)頁純文本例子,需要的朋友可以參考下2014-04-04spring配置文件解析失敗報(bào)”cvc-elt.1: 找不到元素 ''''beans'''' 的聲明”異常解決
這篇文章主要給大家介紹了關(guān)于spring配置文件解析失敗報(bào)”cvc-elt.1: 找不到元素 'beans' 的聲明”異常的解決方法,需要的朋友可以參考下2020-08-08