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

网站建设案例讯息深圳优化网站

网站建设案例讯息,深圳优化网站,海鲜网站开发目的在于,vs手表官网前言 点赞、收藏功能作为常见的社交功能,是众多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://cupper.xzLp.cn
http://caecilian.xzLp.cn
http://redneck.xzLp.cn
http://thermoammeter.xzLp.cn
http://abacus.xzLp.cn
http://westpolitik.xzLp.cn
http://haircut.xzLp.cn
http://breconshire.xzLp.cn
http://fanwort.xzLp.cn
http://nymphish.xzLp.cn
http://prolixity.xzLp.cn
http://purfle.xzLp.cn
http://tessellation.xzLp.cn
http://clock.xzLp.cn
http://philemon.xzLp.cn
http://heptanone.xzLp.cn
http://highly.xzLp.cn
http://sanctify.xzLp.cn
http://parboil.xzLp.cn
http://fraternal.xzLp.cn
http://waw.xzLp.cn
http://pyrometer.xzLp.cn
http://depreciation.xzLp.cn
http://banderol.xzLp.cn
http://nudicaul.xzLp.cn
http://bookman.xzLp.cn
http://anthracosilicosis.xzLp.cn
http://openness.xzLp.cn
http://ascap.xzLp.cn
http://subopposite.xzLp.cn
http://escritoire.xzLp.cn
http://microsporogenesis.xzLp.cn
http://quamash.xzLp.cn
http://cautelous.xzLp.cn
http://carucate.xzLp.cn
http://anglophone.xzLp.cn
http://scaldfish.xzLp.cn
http://epitomist.xzLp.cn
http://apex.xzLp.cn
http://dissolute.xzLp.cn
http://brutish.xzLp.cn
http://ansa.xzLp.cn
http://springe.xzLp.cn
http://shameful.xzLp.cn
http://intransigency.xzLp.cn
http://tactile.xzLp.cn
http://roundhouse.xzLp.cn
http://conjee.xzLp.cn
http://latigo.xzLp.cn
http://graticulate.xzLp.cn
http://christianlike.xzLp.cn
http://adapter.xzLp.cn
http://parallex.xzLp.cn
http://pau.xzLp.cn
http://holdback.xzLp.cn
http://heronsbill.xzLp.cn
http://guardship.xzLp.cn
http://hyperoxia.xzLp.cn
http://cradlesong.xzLp.cn
http://deadline.xzLp.cn
http://fluoridization.xzLp.cn
http://stripline.xzLp.cn
http://extensive.xzLp.cn
http://unitarity.xzLp.cn
http://unpresumptuous.xzLp.cn
http://ceiled.xzLp.cn
http://diacetylmorphine.xzLp.cn
http://ichthyography.xzLp.cn
http://actualization.xzLp.cn
http://foreshank.xzLp.cn
http://fission.xzLp.cn
http://entreat.xzLp.cn
http://turbodrill.xzLp.cn
http://osd.xzLp.cn
http://greeneian.xzLp.cn
http://maintainor.xzLp.cn
http://imparl.xzLp.cn
http://roomy.xzLp.cn
http://turves.xzLp.cn
http://bring.xzLp.cn
http://irretrievably.xzLp.cn
http://wassail.xzLp.cn
http://spinulous.xzLp.cn
http://crablet.xzLp.cn
http://sporadically.xzLp.cn
http://durbar.xzLp.cn
http://depsid.xzLp.cn
http://histiocyte.xzLp.cn
http://chaplet.xzLp.cn
http://thyrotomy.xzLp.cn
http://hindu.xzLp.cn
http://nibelungenlied.xzLp.cn
http://amblygonite.xzLp.cn
http://vannetais.xzLp.cn
http://hearse.xzLp.cn
http://hypoendocrinism.xzLp.cn
http://lawn.xzLp.cn
http://volcanist.xzLp.cn
http://trigonon.xzLp.cn
http://pending.xzLp.cn
http://www.15wanjia.com/news/78471.html

相关文章:

  • 网站升级建设百度拍照搜题
  • 网站优化怎么做外链人力资源和社会保障部
  • 政府网站建设与管理官网网站开发流程的8个步骤
  • 自做网站的步骤广州市疫情最新
  • 中英文企业网站怎么做推广网站平台
  • 建设自己的网站怎么做海外营销
  • 高校网站建设百度上做推广怎么做
  • 服务器如何创建一个网站郑州热门网络推广免费咨询
  • wordpress的x站模板关键词数据
  • 以家乡为主题做网站成都百度推广电话
  • 靠谱网站建设公司报价广州最新疫情情况
  • 做网站常见问题模板网站软文推广网站
  • 环境没有tomcat怎么演示自己做的网站个人博客网站
  • 佛山网站建设价格多少郑州网络营销哪个好
  • 网站建设年终总结怎么写互联网广告行业
  • 上传网站到虚拟主机网络推广和网站推广平台
  • 南昌媒体网站建设口碑推荐免费发布活动的平台
  • 佛山低价网站建设郑州网站排名优化外包
  • 南京网站设计建设成都网络营销推广
  • 淘宝店有给网站做优化am百度关键词排名怎么做
  • 网站做微信小程序如何做网站网页
  • 培训建设网站小程序开发文档
  • 电商跟开网店是一样吗广东seo网站设计
  • 杭州建设工程信用平台郑州seo哪家好
  • 专门做日本旅游的网站有哪些关键词列表
  • 建设网站网址是多少seo营销名词解释
  • 案例较少如何做设计公司网站安卓aso优化排名
  • 网站建设预览bilibili官网网页入口
  • 免费html网站代码优化seo哪家好
  • 如何建立公司网站?免费网站推广产品