微博不能关注怎么回事20566 微博不能关注人了是怎么回事( 二 )

springboot配置如下:
server:port: 7004 # 端口spring:application:name: ms-follow # 应用名# 数据库datasource:driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: rooturl: jdbc:mysql://127.0.0.1:3306/seckill?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false# Redisredis:port: 6379host: localhosttimeout: 3000password: 123456database: 2# Swaggerswagger:base-package: com.zjq.followtitle: 好用功能微服务API接口文档# 配置 Eureka Server 注册中心eureka:instance:prefer-ip-address: trueinstance-id: ${spring.cloud.client.ip-address}:${server.port}client:service-url:defaultZone: http://localhost:7000/eureka/service:name:ms-oauth-server: http://ms-oauth2-server/ms-diners-server: http://ms-users/mybatis:configuration:map-underscore-to-camel-case: true # 开启驼峰映射logging:pattern:console: '%d{HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n'添加配置类redis配置类:
package com.zjq.seckill.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.script.DefaultRedisScript;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;/** * RedisTemplate配置类 * @author zjq */@Configurationpublic class RedisTemplateConfiguration {/*** redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类** @param redisConnectionFactory* @return*/@Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);// 使用Jackson2JsonRedisSerialize 替换默认序列化Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper = new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);// 设置key和value的序列化规则redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);redisTemplate.afterPropertiesSet();return redisTemplate;}}REST配置类:

微博不能关注怎么回事20566 微博不能关注人了是怎么回事


关注/取关实现业务逻辑
微博不能关注怎么回事20566 微博不能关注人了是怎么回事


Mapper实现Mapper比较简单主要是查询关注信息、添加关注信息、取关或者再次关注 。
微博不能关注怎么回事20566 微博不能关注人了是怎么回事


Service层实现package com.zjq.seckill.service;import cn.hutool.core.bean.BeanUtil;import com.zjq.commons.constant.ApiConstant;import com.zjq.commons.constant.RedisKeyConstant;import com.zjq.commons.exception.ParameterException;import com.zjq.commons.model.domain.ResultInfo;import com.zjq.commons.model.pojo.Follow;import com.zjq.commons.model.vo.SignInUserInfo;import com.zjq.commons.utils.AssertUtil;import com.zjq.commons.utils.ResultInfoUtil;import com.zjq.seckill.mapper.FollowMapper;import org.springframework.beans.factory.annotation.Value;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;import java.util.LinkedHashMap;/** * 关注/取关业务逻辑层 * @author zjq */@Servicepublic class FollowService {@Value("${service.name.ms-oauth-server}")private String oauthServerName;@Value("${service.name.ms-diners-server}")private String dinersServerName;@Resourceprivate RestTemplate restTemplate;@Resourceprivate FollowMapper followMapper;@Resourceprivate RedisTemplate redisTemplate;/*** 关注/取关** @param followUserId 关注的食客ID* @param isFollowed是否关注 1=关注 0=取关* @param accessToken登录用户token* @param path访问地址* @return*/public ResultInfo follow(Integer followUserId, int isFollowed,String accessToken, String path) {// 是否选择了关注对象AssertUtil.isTrue(followUserId == null || followUserId < 1,"请选择要关注的人");// 获取登录用户信息 (封装方法)SignInUserInfo dinerInfo = loadSignInDinerInfo(accessToken);// 获取当前登录用户与需要关注用户的关注信息Follow follow = followMapper.selectFollow(dinerInfo.getId(), followUserId);// 如果没有关注信息,且要进行关注操作 -- 添加关注if (follow == null && isFollowed == 1) {// 添加关注信息int count = followMapper.save(dinerInfo.getId(), followUserId);// 添加关注列表到 Redisif (count == 1) {addToRedisSet(dinerInfo.getId(), followUserId);}return ResultInfoUtil.build(ApiConstant.SUCCESS_CODE,"关注成功", path, "关注成功");}// 如果有关注信息,且目前处于关注状态,且要进行取关操作 -- 取关关注if (follow != null && follow.getIsValid() == 1 && isFollowed == 0) {// 取关int count = followMapper.update(follow.getId(), isFollowed);// 移除 Redis 关注列表if (count == 1) {removeFromRedisSet(dinerInfo.getId(), followUserId);}return ResultInfoUtil.build(ApiConstant.SUCCESS_CODE,"成功取关", path, "成功取关");}// 如果有关注信息,且目前处于取关状态,且要进行关注操作 -- 重新关注if (follow != null && follow.getIsValid() == 0 && isFollowed == 1) {// 重新关注int count = followMapper.update(follow.getId(), isFollowed);// 添加关注列表到 Redisif (count == 1) {addToRedisSet(dinerInfo.getId(), followUserId);}return ResultInfoUtil.build(ApiConstant.SUCCESS_CODE,"关注成功", path, "关注成功");}return ResultInfoUtil.buildSuccess(path, "操作成功");}/*** 添加关注列表到 Redis** @param dinerId* @param followUserId*/private void addToRedisSet(Integer dinerId, Integer followUserId) {redisTemplate.opsForSet().add(RedisKeyConstant.following.getKey() + dinerId, followUserId);redisTemplate.opsForSet().add(RedisKeyConstant.followers.getKey() + followUserId, dinerId);}/*** 移除 Redis 关注列表** @param dinerId* @param followUserId*/private void removeFromRedisSet(Integer dinerId, Integer followUserId) {redisTemplate.opsForSet().remove(RedisKeyConstant.following.getKey() + dinerId, followUserId);redisTemplate.opsForSet().remove(RedisKeyConstant.followers.getKey() + followUserId, dinerId);}/*** 获取登录用户信息** @param accessToken* @return*/private SignInUserInfo loadSignInDinerInfo(String accessToken) {// 必须登录AssertUtil.mustLogin(accessToken);String url = oauthServerName + "user/me?access_token={accessToken}";ResultInfo resultInfo = restTemplate.getForObject(url, ResultInfo.class, accessToken);if (resultInfo.getCode() != ApiConstant.SUCCESS_CODE) {throw new ParameterException(resultInfo.getMessage());}SignInUserInfo dinerInfo = BeanUtil.fillBeanWithMap((LinkedHashMap) resultInfo.getData(),new SignInUserInfo(), false);return dinerInfo;}}

推荐阅读