我們將使用Spring Boot作為後端框架,並使用MySQL作為資料庫。
首先,在com.example.demo.entity套件下建立一個名為Article.java的新類,並新增以下內容:
public class Article { private Integer id; private String title; private String content; private Integer authorId; // Getter and Setter methods }
在com.example.demo.mapper
套件下建立一個名為ArticleMapper.java
的新接口,並加入以下內容:
@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
套件下建立一個名為ArticleServiceImpl.java
的新類,並新增以下內容:
@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); } }
在com.example.demo.controller
套件下建立一個名為ArticleController.java
的新類,並新增以下內容:
@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("文章发布成功"); } @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("文章删除成功"); } }
至此,我們已經完成了後端的發布、編輯、刪除文章功能。
在src/views
目錄下建立一個名為ArticleList. vue
的新元件,並新增以下內容:
<template> <div> <el-table :data="articles"> <el-table-column prop="id" label="ID" width="80"></el-table-column> <el-table-column prop="title" label="标题"></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) => { // 跳转到编辑页面 }; const deleteArticle = async (id) => { await axios.delete(`/api/article/${id}`); fetchArticles(); }; fetchArticles(); return { articles, editArticle, deleteArticle }; }, }; </script>
在src/views
目錄下建立一個名為CreateArticle.vue
的新元件,並新增以下內容:
<template> <el-form ref="form" :model="form" label-width="80px"> <el-form-item label="标题" prop="title"> <el-input v-model="form.title"></el-input> </el-form-item> <el-form-item label="内容" 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')">发布文章</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("文章发布成功"); } catch (error) { alert("文章发布失败"); } }; return { form, submitForm }; }, }; </script>
在src/views
目錄下建立一個名為EditArticle.vue
的新元件,並新增以下內容:
<template> <el-form ref="form" :model="form" label-width="80px"> <el-form-item label="标题" prop="title"> <el-input v-model="form.title"></el-input> </el-form-item> <el-form-item label="内容" 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>
定義了一個元件名為EditArticle.vue的新元件,需要一個名為id的屬性。 fetchArticle函數會在元件載入時被調用,以取得文章資訊並將其填入表單中。點擊"更新文章"按鈕時,會呼叫submitForm函數,將表單資料傳送到後端以更新文章。
以上是基於SpringBoot和Vue3的部落格平台發布、編輯、刪除文章功能怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!