广告:宝塔Linux面板高效运维的服务器管理软件 点击【 https://www.bt.cn/p/uNLv1L 】立即购买
1. 后端Spring Boot实现我们将使用Spring Boot作为后端框架,并使用MySQL作为数据库。
1.1 创建Article实体类首先,在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}登录后复制1.2 创建ArticleMapper接口
在com.example.demo.mapper
包下创建一个名为ArticleMapper.java
的新接口,并添加以下内容:
@Mapperpublic 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
的新类,并添加以下内容:
@Servicepublic 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创建ArticleController类
在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("文章删除成功"); }}登录后复制
至此,我们已经完成了后端的发布、编辑、删除文章功能。
2. 前端Vue3实现2.1 创建文章列表页面组件在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>登录后复制2.2 创建文章发布页面组件
在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>登录后复制2.3 创建文章编辑页面组件
在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的博客平台发布、编辑、删除文章功能怎么实现的详细内容,更多请关注9543建站博客其它相关文章!
发表评论