【黑马头条】-day09用户行为-精度丢失-点赞收藏关注

2024-04-14 10:12

本文主要是介绍【黑马头条】-day09用户行为-精度丢失-点赞收藏关注,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


文章目录

  • 1 long类型精度丢失问题
    • 1.1 解决
    • 1.2 导入jackson序列化工具
    • 1.3 自定义注解
    • 1.4 原理
    • 1.5 测试
  • 2 用户行为要求
  • 3 创建微服务behavior
    • 3.1 微服务创建
    • 3.2 添加启动类
    • 3.3 创建bootstrap.yml
    • 3.4 在nacos中配置redis
    • 3.5 引入redis依赖
    • 3.6 更新minio
  • 4 跳过


1 long类型精度丢失问题

在这里插入图片描述

在数据库与网络传输的long类型的id传输中,long类型的id发生了精度丢失

1.1 解决

在这里插入图片描述

  • 当后端响应给前端的数据中包含了id或者特殊标识(可自定义)的时候,把当前数据进行转换为String类型
  • 当前端传递后后端的dto中有id或者特殊标识(可自定义)的时候,把当前数据转为Integer或Long类型。

特殊标识类说明:

IdEncrypt 自定义注解 作用在需要转换类型的字段属性上,用于非id的属性上 在model包下

1.2 导入jackson序列化工具

将jackson序列化工具导入heima-leadnews-common模块下的com.heima.common.jackson

在这里插入图片描述

因为是外来复制进来的,要想让spring进行管理,就需要将配置类导入src/main/resources/META-INF/spring.factories中

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.heima.common.exception.ExceptionCatch,\com.heima.common.aliyun.GreenTextScan,\com.heima.common.aliyun.GreenImageScan,\com.heima.common.tess4j.Tess4jClient,\com.heima.common.redis.CacheService,\com.heima.common.jackson.InitJacksonConfig

1.3 自定义注解

将配置导入spring后,发现有个注解找不到

在这里插入图片描述

需要导入自定义注解

导入自定义注解到heima-leadnews-model模块中的com.heima.model.common.annotation.IdEncrypt

@JacksonAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
public @interface IdEncrypt {
}

1.4 原理

在heima-leadnews-common模块下的com.heima.common.jackson.ConfusionSerializerModifier类中

public class ConfusionSerializerModifier extends BeanSerializerModifier {@Overridepublic List<BeanPropertyWriter> changeProperties(SerializationConfig config,BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {List<BeanPropertyWriter> newWriter = new ArrayList<>();for(BeanPropertyWriter writer : beanProperties){String name = writer.getType().getTypeName();if(null == writer.getAnnotation(IdEncrypt.class) && !writer.getName().equalsIgnoreCase("id")){newWriter.add(writer);} else {writer.assignSerializer(new ConfusionSerializer());newWriter.add(writer);}}return newWriter;}
}

if(null == writer.getAnnotation(IdEncrypt.class) && !writer.getName().equalsIgnoreCase("id"))

如果是被IdEncrypt注解标注的或者为id字段的则被自动转为字符串序列化。

1.5 测试

重启heima-leadnews-artcile微服务

在这里插入图片描述

id已经加上双引号,转为字符串也就不会有精度丢失了。

2 用户行为要求

  • 所有的行为数据,都存储到redis中
  • 点赞、阅读、不喜欢需要专门创建一个微服务来处理数据,新建模块:heima-leadnews-behavior
  • 关注需要在heima-leadnews-user微服务中实现
  • 收藏与文章详情数据回显在heima-leadnews-article微服务中实现

3 创建微服务behavior

3.1 微服务创建

创建微服务heima-leadnews-behavior

在这里插入图片描述

3.2 添加启动类

@SpringBootApplication
public class BehaviorAppliction {public static void main(String[] args) {SpringApplication.run(BehaviorAppliction.class, args);}
}

3.3 创建bootstrap.yml

创建bootstrap.yml配置nacos

server:port: 51806
spring:application:name: leadnews-behaviorcloud:nacos:discovery:server-addr: 192.168.204.129:8848config:server-addr: 192.168.204.129:8848file-extension: yml

3.4 在nacos中配置redis

在nacos中创建leadnews-behavior配置中心,配置redis

因为文章相应的点赞收藏需要操作数据库,所以也需要配置数据库信息。

spring:redis:host: 192.168.204.129password: leadnewsport: 6379datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/leadnews_admin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=falseusername: rootpassword: 123sjbsjb

3.5 引入redis依赖

为heima-leadnews-behavior模块引入redis依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis依赖commons-pool 这个依赖一定要添加 -->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId>
</dependency>

因为在之间已经将redis的工具类CacheService放入heima-leadnews-common的com.heima.common.redis下,并且交由spring管理,所以其他微服务可以直接注入CacheService使用redis。

3.6 更新minio

因为要进行回显,所以需要对静态html的文件在每次更新后进行重新生成静态文件。

在heima-leadnews-article模块中的com.heima.article.test中编写一个ArticleFreemarkerTest测试类,用于重新加载所有文件的minio的静态地址

@SpringBootTest(classes= ArticleApplication.class)
@RunWith(SpringRunner.class)
public class ArticleFreemarkerTest {@Autowiredprivate ApArticleContentMapper apArticleContentMapper;@Autowiredprivate Configuration configuration;@Autowiredprivate FileStorageService fileStorageService;@Autowiredprivate ApArticleService apArticleService;@Testpublic void testAllFreemarker() throws Exception {//1.获取所有文章idApArticle apArticle = new ApArticle();List<ApArticle> apArticles = apArticleService.list();for (ApArticle article : apArticles) {//2.获取文章内容ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, article.getId()));if(apArticleContent!=null&& StringUtils.isNotBlank(apArticleContent.getContent())){//3.文章内容通过freemarker生成静态html页面Template template = configuration.getTemplate("article.ftl");//3.1 创建模型Map<String,Object> content=new HashMap();content.put("content", JSONArray.parseArray(apArticleContent.getContent()));//3.2 输出流StringWriter writer = new StringWriter();//3.3 合成方法template.process(content,writer);//4.把静态页面上传到minio//4.1 文件流InputStream inputStream = new ByteArrayInputStream(writer.toString().getBytes());String path = fileStorageService.uploadHtmlFile("",apArticleContent.getArticleId()+".html",inputStream);//5.把静态页面的路径保存到数据库apArticleService.update(Wrappers.<ApArticle>lambdaUpdate().eq(ApArticle::getId,apArticleContent.getArticleId()).set(ApArticle::getStaticUrl,path));}}}
}

在这里插入图片描述

4 跳过

黑马头条的前端有问题,点点赞和不喜欢还有关注没反应的,查看前端请求url也没有捕捉到,收藏和评论按钮也没有,估计前端写的有问题,没有前端源码,懒得研究。day9直接跳过。

这篇关于【黑马头条】-day09用户行为-精度丢失-点赞收藏关注的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/902749

相关文章

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

MySQL 用户创建与授权最佳实践

《MySQL用户创建与授权最佳实践》在MySQL中,用户管理和权限控制是数据库安全的重要组成部分,下面详细介绍如何在MySQL中创建用户并授予适当的权限,感兴趣的朋友跟随小编一起看看吧... 目录mysql 用户创建与授权详解一、MySQL用户管理基础1. 用户账户组成2. 查看现有用户二、创建用户1. 基

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错

电脑提示xlstat4.dll丢失怎么修复? xlstat4.dll文件丢失处理办法

《电脑提示xlstat4.dll丢失怎么修复?xlstat4.dll文件丢失处理办法》长时间使用电脑,大家多少都会遇到类似dll文件丢失的情况,不过,解决这一问题其实并不复杂,下面我们就来看看xls... 在Windows操作系统中,xlstat4.dll是一个重要的动态链接库文件,通常用于支持各种应用程序

MySQL启动报错:InnoDB表空间丢失问题及解决方法

《MySQL启动报错:InnoDB表空间丢失问题及解决方法》在启动MySQL时,遇到了InnoDB:Tablespace5975wasnotfound,该错误表明MySQL在启动过程中无法找到指定的s... 目录mysql 启动报错:InnoDB 表空间丢失问题及解决方法错误分析解决方案1. 启用 inno

Mysql中的用户管理实践

《Mysql中的用户管理实践》:本文主要介绍Mysql中的用户管理实践,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录13. 用户管理13.1 用户 13.1.1 用户信息 13.1.2 创建用户 13.1.3 删除用户 13.1.4 修改用户

电脑提示Winmm.dll缺失怎么办? Winmm.dll文件丢失的多种修复技巧

《电脑提示Winmm.dll缺失怎么办?Winmm.dll文件丢失的多种修复技巧》有时电脑会出现无法启动程序,因为计算机中丢失winmm.dll的情况,其实,winmm.dll丢失是一个比较常见的问... 在大部分情况下出现我们运行或安装软件,游戏出现提示丢失某些DLL文件或OCX文件的原因可能是原始安装包

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

Spring Boot 事务详解(事务传播行为、事务属性)

《SpringBoot事务详解(事务传播行为、事务属性)》SpringBoot提供了强大的事务管理功能,通过@Transactional注解可以方便地配置事务的传播行为和属性,本文将详细介绍Spr... 目录Spring Boot 事务详解引言声明式事务管理示例编程式事务管理示例事务传播行为1. REQUI

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.