Springboot+Vue+axios實(shí)現(xiàn)文章收藏功能
最近在做畢設(shè),也是第一次使用前后分離框架我就邊學(xué)邊用springboot+vue做了一個(gè)博客文章的收藏功能,寫得不好見(jiàn)諒,算是一個(gè)學(xué)習(xí)筆記吧,給大家分享一下,后面可能還會(huì)做一個(gè)關(guān)注/粉絲的模塊。
那就進(jìn)入正題:
咱們就先從數(shù)據(jù)庫(kù)出發(fā)
id_blog主要就是關(guān)聯(lián)對(duì)應(yīng)的文章,id_user就是是誰(shuí)對(duì)這個(gè)文章收藏了,這樣后續(xù)利于用戶查詢自己收藏的文章列表,create_time可以加上添加時(shí)間,這個(gè)字段后續(xù)可進(jìn)行按照時(shí)間排序。
數(shù)據(jù)庫(kù)創(chuàng)建好后,就寫實(shí)體類
@Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class BlogCollection implements Serializable { ? ? private static final long serialVersionUID = 1L; ? ? @TableId(value = "id", type = IdType.AUTO) ? ? private Integer id; ? ? private Integer idBlog; ? ? private Integer idUser; ? ? @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") ? ? @TableField(fill = FieldFill.INSERT) ? ? private LocalDateTime createTime; }
Mapper
public interface BlogCollectionMapper extends BaseMapper<BlogCollection> { ? ? @Insert("insert into 表名 ?values(字段名)") ? ? void addCollection(BlogCollection bc); ? ? //可以用mybatisPlus插入數(shù)據(jù)方便 }
Service
public interface BlogCollectionService extends IService<BlogCollection> { ? ? void addCollection(BlogCollection bc); }
serviceImpl
public class BlogCollectionServiceImpl extends ServiceImpl<BlogCollectionMapper, BlogCollection> implements BlogCollectionService { ? ? @Autowired ? ? BlogCollectionMapper blogCollectionMapper; ? ? @Override ? ? public void addCollection(BlogCollection bc) { ? ? ? ? blogCollectionMapper.addCollection(bc); ? ? } }
Controller
@RestController @RequestMapping("/BlogCollection") public class BlogCollectionController { ? ? @Resource ? ? BlogCollectionService blogCollectionService; ? ? @Resource ? ?BlogCollectionMapper BlogCollectionMapper; ?? ?//收藏 ? ? @PostMapping("/addBlogCollection") ? ? public Result<?> addBlog(@RequestBody BlogCollection blogCollection) { ? ? ? ? blogCollectionService.addCollection(blogCollection); ? ? ? ? return Result.success(); ? ? } }
以上就是添加收藏的部分代碼,然后就是寫前端調(diào)用及渲染到頁(yè)面上
<div class="button_content" style="flex: 1;line-height: 60px"> ? ?<el-button @click="addCollection" v-if="collectionState===false" type="text" style="margin-left: 30px"> ? ? ? <el-icon style="font-size:20px" color="#999aaa"><StarFilled /></el-icon> ? ? ? {{collectionCount }} ? ? ? ? ? </el-button> ? ? ? ? ? <el-button @click="delCollection" v-if="collectionState===true" type="text" style="margin-left: 30px"> ? ? ? ? ? ? <el-icon style="font-size:20px" color="#999aaa"><StarFilled /></el-icon> ? ? ? ? ? ? {{collectionCount }} ? ? ? ? ? </el-button> ? ? ? ? ? <el-button type="text" @click="" style="margin-left: 30px"> ? ? ? ? ? ? <el-icon style="font-size:20px" color="#999aaa"> <ChatDotRound /></el-icon> ? ? ? ? ? ? {{ messageCount }} ? </el-button> </div>
Js部分
?data(){ ? ? return{ ? ? collectionIds:{}, ? ? collectionState:false,//默認(rèn)是false則是可收藏,true的話就是已收藏 ? ? } ? ? }, ? ? methods:{ ? ? add(){ ? ? ? this.collectionIds.idBlog=this.$route.query.id //當(dāng)前文章ID ? ? ? this.collectionIds.idUser=this.user.id //當(dāng)前用戶ID ? ? request.post("/BlogCollection/addBlogCollection",this.collectionIds).then(res=>{ ? ? ? ? if (res.code === '0') { ? ? ? ? ? this.$message({ ? ? ? ? ? ? message: "收藏成功", ? ? ? ? ? ? type: "success" ? ? ? ? ? }); ? ? ? ? ? this.collectionState=true ? ? ? ? ? console.log(this.collectionState) ? ? ? ? } else { ? ? ? ? ? this.$message({ ? ? ? ? ? ? message: res.msg, ? ? ? ? ? ? type: "error" ? ? ? ? ? }); ? ? ? ? } ? ? ? }) ?? ?} ? ? }
在頁(yè)面加載時(shí)獲取該用戶判斷是否收藏該文章
getState(){ ? ? ? let userJson=sessionStorage.getItem("user") ? ? ? let userid=JSON.parse(userJson).id ? ? ? request.get("/user/getState/"+userid).then(res=>{ ? ? ? ? if(res.data!=null){?? ??? ?//從表中查詢是否有記錄,不為空把collectionState設(shè)置true ? ? ? ? ? this.collectionState=true ? ? ? ? } ? ? ? ? if(res.data.length){ ?//獲取結(jié)果集如果存在則把collectionState設(shè)置true,防止重復(fù)收藏 ? ? ? ? ? this.collectionState=true ? ? ? ? } ? ? ? }) ? ? },
獲取用戶收藏狀態(tài)需要在頁(yè)面加載時(shí)調(diào)用,需要在created里進(jìn)行調(diào)用,其次就是取消收藏功能也是跟這個(gè)邏輯一樣的,在點(diǎn)擊取消收藏后將collectionState設(shè)置為false即可,后臺(tái)的話就通過(guò)用戶id對(duì)收藏表查詢刪除就可以啦!奉上效果圖:
未收藏狀態(tài)
已收藏狀態(tài)
補(bǔ)充:request是axios封裝的一個(gè)工具,大家也可以使用原axios進(jìn)行對(duì)后臺(tái)接口調(diào)用
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊(cè)中心的方法
本文主要介紹了SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊(cè)中心,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04關(guān)于logback日志級(jí)別動(dòng)態(tài)切換的四種方式
這篇文章主要介紹了關(guān)于logback日志級(jí)別動(dòng)態(tài)切換的四種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08MyBatis游標(biāo)Cursor的正確使用和百萬(wàn)數(shù)據(jù)傳輸?shù)膬?nèi)存測(cè)試
這篇文章主要介紹了MyBatis游標(biāo)Cursor的正確使用和百萬(wàn)數(shù)據(jù)傳輸?shù)膬?nèi)存測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01如何使用JFrame完成動(dòng)態(tài)模擬時(shí)鐘
本文介紹了如何使用JFrame完成動(dòng)態(tài)模擬時(shí)鐘,需要的朋友可以參考下2015-08-08SpringBoot @FixMethodOrder 如何調(diào)整單元測(cè)試順序
這篇文章主要介紹了SpringBoot @FixMethodOrder 調(diào)整單元測(cè)試順序方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09