亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Springboot+Vue+axios實現(xiàn)文章收藏功能

 更新時間:2022年08月31日 09:38:11   作者:小黑ya_  
這篇文章主要為大家詳細介紹了Springboot+Vue+axios實現(xiàn)文章收藏功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近在做畢設(shè),也是第一次使用前后分離框架我就邊學(xué)邊用springboot+vue做了一個博客文章的收藏功能,寫得不好見諒,算是一個學(xué)習(xí)筆記吧,給大家分享一下,后面可能還會做一個關(guān)注/粉絲的模塊。

那就進入正題:

咱們就先從數(shù)據(jù)庫出發(fā)

id_blog主要就是關(guān)聯(lián)對應(yīng)的文章,id_user就是是誰對這個文章收藏了,這樣后續(xù)利于用戶查詢自己收藏的文章列表,create_time可以加上添加時間,這個字段后續(xù)可進行按照時間排序。

數(shù)據(jù)庫創(chuàng)建好后,就寫實體類

@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)用及渲染到頁面上

<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,//默認是false則是可收藏,true的話就是已收藏
? ? }
? ? },
? ? methods:{
? ? add(){
? ? ? this.collectionIds.idBlog=this.$route.query.id //當前文章ID
? ? ? this.collectionIds.idUser=this.user.id //當前用戶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"
? ? ? ? ? });
? ? ? ? }
? ? ? })
?? ?}
? ? }

在頁面加載時獲取該用戶判斷是否收藏該文章

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)需要在頁面加載時調(diào)用,需要在created里進行調(diào)用,其次就是取消收藏功能也是跟這個邏輯一樣的,在點擊取消收藏后將collectionState設(shè)置為false即可,后臺的話就通過用戶id對收藏表查詢刪除就可以啦!奉上效果圖:

未收藏狀態(tài)

已收藏狀態(tài)

補充:request是axios封裝的一個工具,大家也可以使用原axios進行對后臺接口調(diào)用

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java遞歸算法遍歷部門代碼示例

    Java遞歸算法遍歷部門代碼示例

    這篇文章主要介紹了Java遞歸算法遍歷部門代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • SpringBoot項目改為SpringCloud項目使用nacos作為注冊中心的方法

    SpringBoot項目改為SpringCloud項目使用nacos作為注冊中心的方法

    本文主要介紹了SpringBoot項目改為SpringCloud項目使用nacos作為注冊中心,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 教你在JNA中將本地方法映射到JAVA代碼中的示例

    教你在JNA中將本地方法映射到JAVA代碼中的示例

    對于JNI來說,我們可以使用native關(guān)鍵字來定義本地方法。那么在JNA中有那些在JAVA代碼中定義本地方法的方式呢?對JNA本地方法映射JAVA代碼的相關(guān)知識感興趣的朋友一起看看吧
    2022-04-04
  • 關(guān)于logback日志級別動態(tài)切換的四種方式

    關(guān)于logback日志級別動態(tài)切換的四種方式

    這篇文章主要介紹了關(guān)于logback日志級別動態(tài)切換的四種方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • MyBatis游標Cursor的正確使用和百萬數(shù)據(jù)傳輸?shù)膬?nèi)存測試

    MyBatis游標Cursor的正確使用和百萬數(shù)據(jù)傳輸?shù)膬?nèi)存測試

    這篇文章主要介紹了MyBatis游標Cursor的正確使用和百萬數(shù)據(jù)傳輸?shù)膬?nèi)存測試,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 快速了解JAVA垃圾回收機制

    快速了解JAVA垃圾回收機制

    這篇文章主要介紹了有關(guān)Java垃圾回收機制的知識,文中實例簡單易懂,方便大家更好的學(xué)習(xí),有興趣的朋友可以了解下
    2020-06-06
  • 如何使用JFrame完成動態(tài)模擬時鐘

    如何使用JFrame完成動態(tài)模擬時鐘

    本文介紹了如何使用JFrame完成動態(tài)模擬時鐘,需要的朋友可以參考下
    2015-08-08
  • Java打亂數(shù)組元素簡單代碼例子

    Java打亂數(shù)組元素簡單代碼例子

    在Java編程中,我們經(jīng)常需要對數(shù)組進行亂序操作(即將數(shù)組中的元素隨機打亂順序),這篇文章主要給大家介紹了關(guān)于Java打亂數(shù)組元素的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-03-03
  • SpringBoot @FixMethodOrder 如何調(diào)整單元測試順序

    SpringBoot @FixMethodOrder 如何調(diào)整單元測試順序

    這篇文章主要介紹了SpringBoot @FixMethodOrder 調(diào)整單元測試順序方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java實現(xiàn)下載文件的6種方式

    Java實現(xiàn)下載文件的6種方式

    本文主要介紹了Java實現(xiàn)下載文件的6種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06

最新評論