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

七台河北京网站建设app 微商城网站建设

七台河北京网站建设,app 微商城网站建设,投资公司注册经营范围,伪静态 网站如何扫描前言 点赞、收藏功能作为常见的社交功能#xff0c;是众多Web应用中必不可少的功能之一。而redis作为一个基于内存的高性能key-value存储数据库#xff0c;可以用来实现这些功能。 本文将介绍如何使用spring boot整合redis实现点赞、收藏功能#xff0c;并提供前后端页面的…前言 点赞、收藏功能作为常见的社交功能是众多Web应用中必不可少的功能之一。而redis作为一个基于内存的高性能key-value存储数据库可以用来实现这些功能。 本文将介绍如何使用spring boot整合redis实现点赞、收藏功能并提供前后端页面的编写代码。 准备工作 在开始之前您需要进行以下准备工作 安装JDK安装Redis并启动Redis服务安装Node.js和Vue.js以便我们能够开发前端页面 后端实现 在后端中我们需要使用spring boot来整合redis并进行相关的接口设计和实现。下面是实现点赞和收藏的核心代码。 相关依赖 首先在pom.xml文件中添加redis相关依赖。 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId /dependency dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactIdversion2.10.0/version /dependencyRedis配置 接下来我们需要配置Redis连接信息可以在application.yml中进行配置。 spring:redis:host: localhostport: 6379点赞功能实现 下面是点赞功能的接口实现代码。 RestController RequestMapping(/like) public class LikeController {Autowiredprivate RedisTemplateString, 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 RedisTemplateString, 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 SetString listFavorite(RequestParam String userId) {String key favorite: userId;return redisTemplate.opsForSet().members(key);} }前端实现 在前端中我们使用Vue.js来编写页面并调用后端提供的接口。下面是点赞、收藏功能的页面实现代码。 点赞 点赞功能页面代码 templatedivbutton clickaddLike点赞/buttonbutton clickdeleteLike取消点赞/buttonspan点赞数{{likeCount}}/span/div /templatescript import axios from axiosexport 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收藏 收藏功能页面代码 templatedivbutton clickaddFavorite收藏/buttonbutton clickdeleteFavorite取消收藏/buttonspan收藏数{{favoriteCount}}/spanulli v-foritem in favoriteList :keyitem{{item}}/li/ul/div /templatescript import axios from axiosexport 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://www.hkea.cn/news/14518047/

相关文章:

  • php网站建设与管理微信群推广平台有哪些
  • 销售网站建设常遇到的问题公司app开发多少钱
  • 网站制作手机版进入官方网站
  • 建立网站要钱吗哪里网站可以有做那个的女人
  • 微网站开发平台系统软件网站分析表怎么做的
  • 东莞网站推广行者seo08大型网站建站
  • 个人服务器网站备案智慧团建手机版登录注册入口
  • 建筑焊工证查询网站官方网西安企业建站价格
  • 一 建设茶叶网站前的市场分析怎么查询网站是否收录
  • 宝安高端网站建设公司重庆建设工程交易中心官网
  • 网站运营论文江苏建设准考证打印在哪个网站
  • 手机网站开发平台学装修设计
  • 汕头公众号建设网站四川营销
  • 给客户做网站 赚钱吗淄博亿泰信息技术有限公司
  • 写网站论文怎么做的网站建设网站维护的具体内容是什么
  • 目前做哪些网站致富wordpress文章目录在哪里
  • 琼海市建设局网站如何做电商直播
  • 网站规划对网站建设起到什么作用做网站一般把宽度做多少
  • 网站做虚假广告wordpress搬家 打开404
  • rails 网站开发西安网站建设问问q778925409耍強
  • 网站如何启用gzip压缩超级外链
  • 手机网站可以做公众号朋友圈海报用什么网站做的
  • 珠海网站建设官网炫酷的移动端网站
  • 怎么快速建设小型外贸网站十大it公司排名
  • 做海鲜团购网站成都网站设计成功柚v米科技
  • 关于服装店网站建设的策划方案企业网站一定要备案吗
  • 网站编辑培训学校深圳浪尖工业设计公司
  • php网站开发试卷图片页面设计
  • 如何给网站做seowordpress 被攻击
  • 做淘宝客网站骗钱网络服务的服务提供者包括( )