日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

微博不能關(guān)注怎么回事20566 微博不能關(guān)注人了是怎么回事( 二 )

springboot配置如下:
server:port: 7004 # 端口spring:application:name: ms-follow # 應用名# 數(shù)據(jù)庫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, 存儲二進制字節(jié)碼, 所以自定義序列化類** @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的序列化規(guī)則redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);redisTemplate.afterPropertiesSet();return redisTemplate;}}REST配置類:

微博不能關(guān)注怎么回事20566 微博不能關(guān)注人了是怎么回事


關(guān)注/取關(guān)實現(xiàn)業(yè)務邏輯
微博不能關(guān)注怎么回事20566 微博不能關(guān)注人了是怎么回事


Mapper實現(xiàn)Mapper比較簡單主要是查詢關(guān)注信息、添加關(guān)注信息、取關(guān)或者再次關(guān)注 。
微博不能關(guān)注怎么回事20566 微博不能關(guān)注人了是怎么回事


Service層實現(xiàn)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;/** * 關(guān)注/取關(guān)業(yè)務邏輯層 * @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;/*** 關(guān)注/取關(guān)** @param followUserId 關(guān)注的食客ID* @param isFollowed是否關(guān)注 1=關(guān)注 0=取關(guān)* @param accessToken登錄用戶token* @param path訪問地址* @return*/public ResultInfo follow(Integer followUserId, int isFollowed,String accessToken, String path) {// 是否選擇了關(guān)注對象AssertUtil.isTrue(followUserId == null || followUserId < 1,"請選擇要關(guān)注的人");// 獲取登錄用戶信息 (封裝方法)SignInUserInfo dinerInfo = loadSignInDinerInfo(accessToken);// 獲取當前登錄用戶與需要關(guān)注用戶的關(guān)注信息Follow follow = followMapper.selectFollow(dinerInfo.getId(), followUserId);// 如果沒有關(guān)注信息,且要進行關(guān)注操作 -- 添加關(guān)注if (follow == null && isFollowed == 1) {// 添加關(guān)注信息int count = followMapper.save(dinerInfo.getId(), followUserId);// 添加關(guān)注列表到 Redisif (count == 1) {addToRedisSet(dinerInfo.getId(), followUserId);}return ResultInfoUtil.build(ApiConstant.SUCCESS_CODE,"關(guān)注成功", path, "關(guān)注成功");}// 如果有關(guān)注信息,且目前處于關(guān)注狀態(tài),且要進行取關(guān)操作 -- 取關(guān)關(guān)注if (follow != null && follow.getIsValid() == 1 && isFollowed == 0) {// 取關(guān)int count = followMapper.update(follow.getId(), isFollowed);// 移除 Redis 關(guān)注列表if (count == 1) {removeFromRedisSet(dinerInfo.getId(), followUserId);}return ResultInfoUtil.build(ApiConstant.SUCCESS_CODE,"成功取關(guān)", path, "成功取關(guān)");}// 如果有關(guān)注信息,且目前處于取關(guān)狀態(tài),且要進行關(guān)注操作 -- 重新關(guān)注if (follow != null && follow.getIsValid() == 0 && isFollowed == 1) {// 重新關(guān)注int count = followMapper.update(follow.getId(), isFollowed);// 添加關(guān)注列表到 Redisif (count == 1) {addToRedisSet(dinerInfo.getId(), followUserId);}return ResultInfoUtil.build(ApiConstant.SUCCESS_CODE,"關(guān)注成功", path, "關(guān)注成功");}return ResultInfoUtil.buildSuccess(path, "操作成功");}/*** 添加關(guān)注列表到 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 關(guān)注列表** @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;}}

推薦閱讀