最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

那就進(jìn)入正題:

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

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

數(shù)據(jù)庫創(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)用及渲染到頁面上

<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"
? ? ? ? ? });
? ? ? ? }
? ? ? })
?? ?}
? ? }

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

未收藏狀態(tài)

已收藏狀態(tài)

補(bǔ)充:request是axios封裝的一個(gè)工具,大家也可以使用原axios進(jìn)行對(duì)后臺(tái)接口調(diào)用

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

相關(guān)文章

  • 基于@RestControllerAdvice與@ControllerAdvice的區(qū)別說明

    基于@RestControllerAdvice與@ControllerAdvice的區(qū)別說明

    這篇文章主要介紹了@RestControllerAdvice與@ControllerAdvice的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring Boot jar可執(zhí)行原理的徹底分析

    Spring Boot jar可執(zhí)行原理的徹底分析

    這篇文章主要給大家介紹了關(guān)于Spring Boot jar可執(zhí)行原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • SpringBoot無法連接redis的解決方案

    SpringBoot無法連接redis的解決方案

    這篇文章主要介紹了SpringBoot無法連接redis的解決方案,文中通過代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • Java中的Semaphore原理解析

    Java中的Semaphore原理解析

    這篇文章主要介紹了Java中的Semaphore原理解析,Semaphore(信號(hào)量)是用來控制同時(shí)訪問特定資源的線程數(shù)量,它通過協(xié)調(diào)各個(gè)線程,以保證合理的使用公共資源,需要的朋友可以參考下
    2024-01-01
  • Java static關(guān)鍵字詳細(xì)介紹與用法總結(jié)

    Java static關(guān)鍵字詳細(xì)介紹與用法總結(jié)

    這篇文章主要介紹了Java中static關(guān)鍵字的作用和用法詳細(xì)介紹,主要講了靜態(tài)方法、靜態(tài)變量、靜態(tài)類、static和final一塊用等內(nèi)容。需要的朋友可以參考下
    2017-04-04
  • Java多線程鎖機(jī)制相關(guān)原理實(shí)例解析

    Java多線程鎖機(jī)制相關(guān)原理實(shí)例解析

    這篇文章主要介紹了Java多線程鎖機(jī)制相關(guān)原理實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 詳解Java編程中對(duì)線程的中斷處理

    詳解Java編程中對(duì)線程的中斷處理

    這篇文章主要介紹了Java編程中對(duì)線程的中斷處理,特別講解了中斷的時(shí)機(jī)與中斷狀態(tài)的管理,需要的朋友可以參考下
    2015-11-11
  • Spring Security 將用戶數(shù)據(jù)存入數(shù)據(jù)庫

    Spring Security 將用戶數(shù)據(jù)存入數(shù)據(jù)庫

    這篇文章主要介紹了Spring Security 如何將用戶數(shù)據(jù)存入數(shù)據(jù)庫,幫助大家更好的理解和學(xué)習(xí)Spring Security,感興趣的朋友可以了解下
    2020-09-09
  • SpringBoot實(shí)現(xiàn)過濾敏感詞的示例代碼

    SpringBoot實(shí)現(xiàn)過濾敏感詞的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用SpringBoot實(shí)現(xiàn)過濾敏感詞功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下
    2022-08-08
  • IDEA SSM框架整合配置及步驟詳解

    IDEA SSM框架整合配置及步驟詳解

    這篇文章主要介紹了IDEA SSM框架整合配置以及步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03

最新評(píng)論

清远市| 芦山县| 高尔夫| 冀州市| 南宁市| 安康市| 泾阳县| 清流县| 安化县| 弋阳县| 巫山县| 灵台县| 绍兴市| 岑巩县| 彭山县| 莱州市| 龙门县| 镇远县| 福海县| 基隆市| 万源市| 张家口市| 宣化县| 汨罗市| 微山县| 高碑店市| 灵川县| 循化| 昂仁县| 库尔勒市| 周口市| 桐庐县| 海原县| 南川市| 合川市| 康保县| 杨浦区| 米脂县| 莒南县| 武义县| 平顶山市|