• 【Java Web】论坛——我收到的赞


    • 重构点赞功能
      • 以用户为key,记录点赞数量
      • increment(key),decrement(key)
    • 开发个人主页
      • 以用户为key,查询点赞数量

    1. 重构LikeService

    redis事务管理,点赞时:

    • 如果已经点过赞,那么移除该用户的点赞,同时目标用户收到的赞减少;
    • 如果没点过赞,那么当前用户点赞成功,目标用户收到的赞增加。

    另外增加findUserLikeCount功能,用于查询某用户收到的赞。

    package com.nowcoder.community.service;
    
    import com.nowcoder.community.util.RedisKeyUtil;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.dao.DataAccessException;
    import org.springframework.data.redis.core.RedisOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.SessionCallback;
    import org.springframework.stereotype.Service;
    
    @Service
    public class LikeService {
        @Autowired
        private RedisTemplate redisTemplate;
    
        // 点赞
        public void like(int userId, int entityType, int entityId, int entityUserId){
    //        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType,entityId);
    //        boolean isMember = redisTemplate.opsForSet().isMember(entityLikeKey,userId);
    //        if(isMember){ // 已经点过赞
    //            redisTemplate.opsForSet().remove(entityLikeKey,userId);
    //        } else { // 没点过赞
    //            redisTemplate.opsForSet().add(entityLikeKey,userId);
    //        }
            redisTemplate.execute(new SessionCallback() {
                @Override
                public Object execute(RedisOperations operations) throws DataAccessException {
                    String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType,entityId);
                    String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId);
    
                    boolean isMember = operations.opsForSet().isMember(entityLikeKey,userId);
                    operations.multi();
                    if(isMember){ //移除该用户的点赞、目标用户收到的赞减少
                        operations.opsForSet().remove(entityLikeKey,userId);
                        operations.opsForValue().decrement(userLikeKey);
                    } else {  //该用户的点赞成功、目标用户收到的赞增加
                        operations.opsForSet().add(entityLikeKey,userId);
                        operations.opsForValue().increment(userLikeKey);
                    }
                    return operations.exec();
                }
            });
        }
    
        // 查询某个实体点赞的数量
        public long findEntityLikeCount(int entityType, int entityId){
            String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType,entityId);
            return redisTemplate.opsForSet().size(entityLikeKey);
        }
    
        // 查询某人某实体的点赞状态
        public int findEntityLikeStatus(int userId, int entityType, int entityId){
            String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType,entityId);
             return redisTemplate.opsForSet().isMember(entityLikeKey, userId) ? 1 : 0;
        }
    
        // 查询某用户收到的赞数量
        public int findUserLikeCount(int userId){
            String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);
            Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);
            return count == null ? 0 : count.intValue();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    2. 重构LikeController

    增加entityUserId参数

    package com.nowcoder.community.controller;
    
    import com.nowcoder.community.annotation.LoginRequired;
    import com.nowcoder.community.entity.User;
    import com.nowcoder.community.service.LikeService;
    import com.nowcoder.community.util.CommunityUtil;
    import com.nowcoder.community.util.HostHolder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @Controller
    public class LikeController {
    
        @Autowired
        private LikeService likeService;
    
        @Autowired
        private HostHolder hostHolder;
    
        @RequestMapping(path="/like",method = RequestMethod.POST)
        @ResponseBody
        public String like(int entityType, int entityId, int entityUserId){
            // 当前用户
            User user = hostHolder.getUser();
    
            // 点赞
            likeService.like(user.getId(), entityType, entityId, entityUserId);
    
            // 返回点赞数量
            long likeCount = likeService.findEntityLikeCount(entityType, entityId);
    
            // 点赞状态
            int likeStatus = likeService.findEntityLikeStatus(user.getId(), entityType, entityId);
    
            // 封装传给页面
            Map<String,Object> map = new HashMap<>();
            map.put("likeCount",likeCount);
            map.put("likeStatus",likeStatus);
            System.out.println(likeStatus);
    
            return CommunityUtil.getJSONString(0, null, map);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    3. profile.html

    <!doctype html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
    	<meta charset="utf-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    	<link rel="icon" th:href="@{/img/ucas.png}"/>
    	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" crossorigin="anonymous">
    	<link rel="stylesheet" th:href="@{/css/global.css}" />
    	<title>果壳网-个人主页</title>
    </head>
    <body>
    	<div class="nk-container">
    		<!-- 头部 -->
    		<header class="bg-dark sticky-top" th:replace="index::header">
    			<div class="container">
    				<!-- 导航 -->
    				<nav class="navbar navbar-expand-lg navbar-dark">
    					<!-- logo -->
    					<a class="navbar-brand" href="#"></a>
    					<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    						<span class="navbar-toggler-icon"></span>
    					</button>
    					<!-- 功能 -->
    					<div class="collapse navbar-collapse" id="navbarSupportedContent">
    						<ul class="navbar-nav mr-auto">
    							<li class="nav-item ml-3 btn-group-vertical">
    								<a class="nav-link" href="../index.html">首页</a>
    							</li>
    							<li class="nav-item ml-3 btn-group-vertical">
    								<a class="nav-link position-relative" href="letter.html">消息<span class="badge badge-danger">12</span></a>
    							</li>
    							<li class="nav-item ml-3 btn-group-vertical">
    								<a class="nav-link" href="register.html">注册</a>
    							</li>
    							<li class="nav-item ml-3 btn-group-vertical">
    								<a class="nav-link" href="login.html">登录</a>
    							</li>
    							<li class="nav-item ml-3 btn-group-vertical dropdown">
    								<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    									<img src="http://images.nowcoder.com/head/1t.png" class="rounded-circle" style="width:30px;"/>
    								</a>
    								<div class="dropdown-menu" aria-labelledby="navbarDropdown">
    									<a class="dropdown-item text-center" href="profile.html">个人主页</a>
    									<a class="dropdown-item text-center" href="setting.html">账号设置</a>
    									<a class="dropdown-item text-center" href="login.html">退出登录</a>
    									<div class="dropdown-divider"></div>
    									<span class="dropdown-item text-center text-secondary">nowcoder</span>
    								</div>
    							</li>
    						</ul>
    						<!-- 搜索 -->
    						<form class="form-inline my-2 my-lg-0" action="search.html">
    							<input class="form-control mr-sm-2" type="search" aria-label="Search" />
    							<button class="btn btn-outline-light my-2 my-sm-0" type="submit">搜索</button>
    						</form>
    					</div>
    				</nav>
    			</div>
    		</header>
    
    		<!-- 内容 -->
    		<div class="main">
    			<div class="container">
    				<!-- 选项 -->
    				<div class="position-relative">
    					<ul class="nav nav-tabs">
    						<li class="nav-item">
    							<a class="nav-link active" href="profile.html">个人信息</a>
    						</li>
    						<li class="nav-item">
    							<a class="nav-link" href="my-post.html">我的帖子</a>
    						</li>
    						<li class="nav-item">
    							<a class="nav-link" href="my-reply.html">我的回复</a>
    						</li>
    					</ul>
    				</div>
    				<!-- 个人信息 -->
    				<div class="media mt-5">
    					<img th:src="${user.headerUrl}" class="align-self-start mr-4 rounded-circle" alt="用户头像" style="width:50px;">
    					<div class="media-body">
    						<h5 class="mt-0 text-warning">
    							<span th:utext="${user.username}">nowcoder</span>
    							<button type="button" class="btn btn-info btn-sm float-right mr-5 follow-btn">关注TA</button>
    						</h5>
    						<div class="text-muted mt-3">
    							<span >注册于 <i class="text-muted" th:text="${#dates.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}">2015-06-12 15:20:12</i></span>
    						</div>
    						<div class="text-muted mt-3 mb-5">
    							<span>关注了 <a class="text-primary" href="followee.html">5</a></span>
    							<span class="ml-4">关注者 <a class="text-primary" href="follower.html">123</a></span>
    							<span class="ml-4">获得了 <i class="text-danger" th:text="${likeCount}">87</i> 个赞</span>
    						</div>
    					</div>
    				</div>
    			</div>
    		</div>
    
    		<!-- 尾部 -->
    		<footer class="bg-dark" th:replace="index::foot">
    			<div class="container">
    				<div class="row">
    					<!-- 二维码 -->
    					<div class="col-4 qrcode">
    						<img src="https://uploadfiles.nowcoder.com/app/app_download.png" class="img-thumbnail" style="width:136px;" />
    					</div>
    					<!-- 公司信息 -->
    					<div class="col-8 detail-info">
    						<div class="row">
    							<div class="col">
    								<ul class="nav">
    									<li class="nav-item">
    										<a class="nav-link text-light" href="#">关于我们</a>
    									</li>
    									<li class="nav-item">
    										<a class="nav-link text-light" href="#">加入我们</a>
    									</li>
    									<li class="nav-item">
    										<a class="nav-link text-light" href="#">意见反馈</a>
    									</li>
    									<li class="nav-item">
    										<a class="nav-link text-light" href="#">企业服务</a>
    									</li>
    									<li class="nav-item">
    										<a class="nav-link text-light" href="#">联系我们</a>
    									</li>
    									<li class="nav-item">
    										<a class="nav-link text-light" href="#">免责声明</a>
    									</li>
    									<li class="nav-item">
    										<a class="nav-link text-light" href="#">友情链接</a>
    									</li>
    								</ul>
    							</div>
    						</div>
    						<div class="row">
    							<div class="col">
    								<ul class="nav btn-group-vertical company-info">
    									<li class="nav-item text-white-50">
    										公司地址:北京市朝阳区大屯路东金泉时代3-2708北京牛客科技有限公司
    									</li>
    									<li class="nav-item text-white-50">
    										联系方式:010-60728802(电话)&nbsp;&nbsp;&nbsp;&nbsp;admin@nowcoder.com
    									</li>
    									<li class="nav-item text-white-50">
    										牛客科技©2018 All rights reserved
    									</li>
    									<li class="nav-item text-white-50">ICP14055008-4 &nbsp;&nbsp;&nbsp;&nbsp;
    										<img src="http://static.nowcoder.com/company/images/res/ghs.png" style="width:18px;" />
    										京公网安备 11010502036488</li>
    								</ul>
    							</div>
    						</div>
    					</div>
    				</div>
    			</div>
    		</footer>
    	</div>
    
    	<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
    	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" crossorigin="anonymous"></script>
    	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" crossorigin="anonymous"></script>
    	<script th:src="@{/js/global.js}"></script>
    	<script th:src="@{/js/profile.js}"></script>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
  • 相关阅读:
    数字化建设方案
    最大时延扩展、CP 和相干带宽
    2023大数据挑战赛全国六强团队获奖经验+ppt分享(二)
    矩阵分析与应用+张贤达
    Day41—— 343. 整数拆分 96.不同的二叉搜索树 (动规)
    实现两栏布局的五种方式
    opengl-shader学习笔记:varying变量
    Java —— String类
    实现分布式锁SchedulerLock
    腾讯云TCP认证考试知识点有哪些?腾讯云停车票认证证书含金量高吗?
  • 原文地址:https://blog.csdn.net/qq_42251120/article/details/132740143