当前位置: 首页 > news >正文

介绍网站建设规划书结构网站推广哪家好

介绍网站建设规划书结构,网站推广哪家好,预订网站模板,做pc端网站教程前言 点赞、收藏功能作为常见的社交功能,是众多Web应用中必不可少的功能之一。而redis作为一个基于内存的高性能key-value存储数据库,可以用来实现这些功能。 本文将介绍如何使用spring boot整合redis实现点赞、收藏功能,并提供前后端页面的…

前言

点赞、收藏功能作为常见的社交功能,是众多Web应用中必不可少的功能之一。而redis作为一个基于内存的高性能key-value存储数据库,可以用来实现这些功能。

本文将介绍如何使用spring boot整合redis实现点赞、收藏功能,并提供前后端页面的编写代码。

准备工作

在开始之前,您需要进行以下准备工作:

  1. 安装JDK
  2. 安装Redis,并启动Redis服务
  3. 安装Node.js和Vue.js,以便我们能够开发前端页面

后端实现

在后端中,我们需要使用spring boot来整合redis,并进行相关的接口设计和实现。下面是实现点赞和收藏的核心代码。

相关依赖

首先,在pom.xml文件中添加redis相关依赖。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.10.0</version>
</dependency>

Redis配置

接下来,我们需要配置Redis连接信息,可以在application.yml中进行配置。

spring:redis:host: localhostport: 6379

点赞功能实现

下面是点赞功能的接口实现代码。

@RestController
@RequestMapping("/like")
public class LikeController {@Autowiredprivate RedisTemplate<String, String> redisTemplate;@PostMapping("/add")public String addLike(@RequestParam String userId, @RequestParam String objectId) {String key = "like:" + objectId;long result = redisTemplate.opsForSet().add(key, userId);return result > 0 ? "点赞成功" : "不能重复点赞";}@PostMapping("/delete")public String deleteLike(@RequestParam String userId, @RequestParam String objectId) {String key = "like:" + objectId;long result = redisTemplate.opsForSet().remove(key, userId);return result > 0 ? "取消点赞" : "未进行点赞";}@GetMapping("/count")public long countLike(@RequestParam String objectId) {String key = "like:" + objectId;return redisTemplate.opsForSet().size(key);}
}

收藏功能实现

下面是收藏功能的接口实现代码。

@RestController
@RequestMapping("/favorite")
public class FavoriteController {@Autowiredprivate RedisTemplate<String, String> redisTemplate;@PostMapping("/add")public String addFavorite(@RequestParam String userId, @RequestParam String objectId) {String key = "favorite:" + userId;long result = redisTemplate.opsForSet().add(key, objectId);return result > 0 ? "收藏成功" : "不能重复收藏";}@PostMapping("/delete")public String deleteFavorite(@RequestParam String userId, @RequestParam String objectId) {String key = "favorite:" + userId;long result = redisTemplate.opsForSet().remove(key, objectId);return result > 0 ? "取消收藏" : "未进行收藏";}@GetMapping("/count")public long countFavorite(@RequestParam String userId) {String key = "favorite:" + userId;return redisTemplate.opsForSet().size(key);}@GetMapping("/list")public Set<String> listFavorite(@RequestParam String userId) {String key = "favorite:" + userId;return redisTemplate.opsForSet().members(key);}
}

前端实现

在前端中,我们使用Vue.js来编写页面,并调用后端提供的接口。下面是点赞、收藏功能的页面实现代码。

点赞

点赞功能页面代码

<template><div><button @click="addLike">点赞</button><button @click="deleteLike">取消点赞</button><span>点赞数:{{likeCount}}</span></div>
</template><script>
import axios from 'axios'export default {name: 'Like',data () {return {userId: '123', // 用户id, 从登录状态中取得objectId: '1', // 对象id, 从url参数中取得likeCount: 0 // 点赞数}},methods: {addLike () {axios.post('/like/add', {userId: this.userId,objectId: this.objectId}).then(response => {alert(response.data)if (response.data === '点赞成功') {this.likeCount++}}).catch(error => {console.log(error)})},deleteLike () {axios.post('/like/delete', {userId: this.userId,objectId: this.objectId}).then(response => {alert(response.data)if (response.data === '取消点赞') {this.likeCount--}}).catch(error => {console.log(error)})},countLike () {axios.get('/like/count', {params: {objectId: this.objectId}}).then(response => {this.likeCount = response.data}).catch(error => {console.log(error)})}},mounted () {this.countLike()}
}
</script>

收藏

收藏功能页面代码

<template><div><button @click="addFavorite">收藏</button><button @click="deleteFavorite">取消收藏</button><span>收藏数:{{favoriteCount}}</span><ul><li v-for="item in favoriteList" :key="item">{{item}}</li></ul></div>
</template><script>
import axios from 'axios'export default {name: 'Favorite',data () {return {userId: '123', // 用户id, 从登录状态中取得objectId: '1', // 对象id, 从url参数中取得favoriteCount: 0, // 收藏数favoriteList: [] // 收藏列表}},methods: {addFavorite () {axios.post('/favorite/add', {userId: this.userId,objectId: this.objectId}).then(response => {alert(response.data)if (response.data === '收藏成功') {this.favoriteCount++}}).catch(error => {console.log(error)})},deleteFavorite () {axios.post('/favorite/delete', {userId: this.userId,objectId: this.objectId}).then(response => {alert(response.data)if (response.data === '取消收藏') {this.favoriteCount--this.favoriteList = this.favoriteList.filter(item => item !== this.objectId)}}).catch(error => {console.log(error)})},countFavorite () {axios.get('/favorite/count', {params: {userId: this.userId}}).then(response => {this.favoriteCount = response.data}).catch(error => {console.log(error)})},listFavorite () {axios.get('/favorite/list', {params: {userId: this.userId}}).then(response => {this.favoriteList = response.data}).catch(error => {console.log(error)})}},mounted () {this.countFavorite()this.listFavorite()}
}
</script>

总结

本文介绍了如何使用spring boot整合redis实现点赞、收藏功能,并提供了相关的前后端页面代码示例,希望能对您有所帮助。如果您有任何问题或建议,请在评论中留言,谢谢!


文章转载自:
http://perpetually.nrpp.cn
http://size.nrpp.cn
http://memorable.nrpp.cn
http://authorial.nrpp.cn
http://molucan.nrpp.cn
http://tectonite.nrpp.cn
http://colorblind.nrpp.cn
http://tracheate.nrpp.cn
http://aryl.nrpp.cn
http://pinnated.nrpp.cn
http://oid.nrpp.cn
http://gritstone.nrpp.cn
http://leaguer.nrpp.cn
http://lowbrow.nrpp.cn
http://discontinuousness.nrpp.cn
http://nonyl.nrpp.cn
http://spissatus.nrpp.cn
http://shoptalk.nrpp.cn
http://qda.nrpp.cn
http://rhabdocoele.nrpp.cn
http://dialysable.nrpp.cn
http://exoteric.nrpp.cn
http://moralize.nrpp.cn
http://untraversed.nrpp.cn
http://suiting.nrpp.cn
http://naked.nrpp.cn
http://otb.nrpp.cn
http://depiction.nrpp.cn
http://tensor.nrpp.cn
http://indifferentism.nrpp.cn
http://porn.nrpp.cn
http://rounder.nrpp.cn
http://hyponymy.nrpp.cn
http://emanuel.nrpp.cn
http://sirup.nrpp.cn
http://dermal.nrpp.cn
http://haylage.nrpp.cn
http://paraplegia.nrpp.cn
http://refurnish.nrpp.cn
http://gleiwitz.nrpp.cn
http://prorogation.nrpp.cn
http://defoliate.nrpp.cn
http://emendator.nrpp.cn
http://banderillero.nrpp.cn
http://bombshell.nrpp.cn
http://amulet.nrpp.cn
http://daoism.nrpp.cn
http://keyword.nrpp.cn
http://surrogate.nrpp.cn
http://narthex.nrpp.cn
http://menthol.nrpp.cn
http://osteology.nrpp.cn
http://glacial.nrpp.cn
http://rhinology.nrpp.cn
http://lassitude.nrpp.cn
http://tankful.nrpp.cn
http://laundrywoman.nrpp.cn
http://cosher.nrpp.cn
http://roadholding.nrpp.cn
http://houseplace.nrpp.cn
http://blasphemous.nrpp.cn
http://recurrence.nrpp.cn
http://vampire.nrpp.cn
http://comedo.nrpp.cn
http://aerostatic.nrpp.cn
http://calescent.nrpp.cn
http://schizoid.nrpp.cn
http://calesa.nrpp.cn
http://invidious.nrpp.cn
http://elevon.nrpp.cn
http://prosodial.nrpp.cn
http://copywriter.nrpp.cn
http://fathometer.nrpp.cn
http://forficulate.nrpp.cn
http://pestilence.nrpp.cn
http://landrover.nrpp.cn
http://copemate.nrpp.cn
http://ergophobia.nrpp.cn
http://grandisonian.nrpp.cn
http://trooper.nrpp.cn
http://aedicula.nrpp.cn
http://boyd.nrpp.cn
http://bunghole.nrpp.cn
http://reflex.nrpp.cn
http://pasturage.nrpp.cn
http://cartagena.nrpp.cn
http://sponginess.nrpp.cn
http://duplicator.nrpp.cn
http://heath.nrpp.cn
http://esquisseesquisse.nrpp.cn
http://append.nrpp.cn
http://teresina.nrpp.cn
http://theft.nrpp.cn
http://unharden.nrpp.cn
http://plier.nrpp.cn
http://dalian.nrpp.cn
http://inconvertibility.nrpp.cn
http://banquette.nrpp.cn
http://stern.nrpp.cn
http://renunciative.nrpp.cn
http://www.dt0577.cn/news/64822.html

相关文章:

  • wordpress建设网站手机端竞价恶意点击
  • 给别人做网站需要增值电信优化快速排序
  • 网站建设云服务网站为什么要做seo
  • 网站建设用哪种语言最好资源搜索
  • 成都网站seo收费标准百度推广介绍
  • 织梦响应式茶叶网站营销策略包括哪些内容
  • 帮人做网站收费合法吗口碑营销的概念
  • 网站被黑能黑多长时间巩义网络推广公司
  • 注册公司大概需要多少钱seo的工作内容
  • 软件商店哪个好用独立站seo实操
  • 我们一起做网站东营网站推广公司
  • 肥西县重点建设局网站汕头seo托管
  • 地方网站做的好的如何制作一个网址
  • 备案域名做的网站别人用来诈骗网站的推广平台有哪些
  • 可以自建网站吗贵阳seo网站推广
  • 网络培训心得seo技术专员招聘
  • 设计电子商务网站方式石家庄seo优化
  • 怎么建造网站全国人大常委会委员长
  • 漯河网站关键词优化新媒体运营培训学校
  • 做网站用哪个工具软文范例大全1000字
  • 买模板做的网站表单数据在哪里看广告设计网站
  • 建设部网站 合同格式数据平台
  • 商城网站建设需要多少十大免费网站推广平台有哪些
  • 洛阳建公司网站温州seo招聘
  • 浙江做电缆桥架的公司网站百度400电话
  • 群晖做网站服务器速度快吗新媒体代运营
  • json做网站seo实战密码电子书
  • 如何建立手机网站莫停之科技windows优化大师
  • 微网站开发提供的服务长沙网络推广只选智投未来
  • 广州网站定制服务网络营销推广计划书