We will use Spring Boot as the backend framework and MySQL as the database.
First, create a new class named Article.java under the com.example.demo.entity package and add the following content:
public class Article { private Integer id; private String title; private String content; private Integer authorId; // Getter and Setter methods }
Create a new interface named ArticleMapper.java
under the com.example.demo.mapper
package and add the following content:
@Mapper public interface ArticleMapper { List<Article> findAll(); Article findById(Integer id); void insert(Article article); void update(Article article); void delete(Integer id); }
Create a new class named ArticleServiceImpl.java
under the com.example.demo.service.impl
package and add the following content:
@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); } }
Create a new class named ArticleController.java
under the com.example.demo.controller
package and add the following Content:
@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("文章删除成功"); } }
At this point, we have completed the back-end publishing, editing, and deletion of article functions.
Create an article named ArticleList in the
src/views directory. A new component of vue
, and add the following content:
<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>
Create a file named ## in the src/views
directory #CreateArticle.vue's new component, and add the following content:
<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 directory Create a new component for
EditArticle.vue and add the following:
<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>
The above is the detailed content of How to implement the functions of publishing, editing and deleting articles on a blog platform based on SpringBoot and Vue3. For more information, please follow other related articles on the PHP Chinese website!