【黑马头条】-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

相关文章

mss32.dll文件丢失怎么办? 电脑提示mss32.dll丢失的多种修复方法

《mss32.dll文件丢失怎么办?电脑提示mss32.dll丢失的多种修复方法》最近,很多电脑用户可能遇到了mss32.dll文件丢失的问题,导致一些应用程序无法正常启动,那么,如何修复这个问题呢... 在电脑常年累月的使用过程中,偶尔会遇到一些问题令人头疼。像是某个程序尝试运行时,系统突然弹出一个错误提

电脑提示找不到openal32.dll文件怎么办? openal32.dll丢失完美修复方法

《电脑提示找不到openal32.dll文件怎么办?openal32.dll丢失完美修复方法》openal32.dll是一种重要的系统文件,当它丢失时,会给我们的电脑带来很大的困扰,很多人都曾经遇到... 在使用电脑过程中,我们常常会遇到一些.dll文件丢失的问题,而openal32.dll的丢失是其中比较

电脑win32spl.dll文件丢失咋办? win32spl.dll丢失无法连接打印机修复技巧

《电脑win32spl.dll文件丢失咋办?win32spl.dll丢失无法连接打印机修复技巧》电脑突然提示win32spl.dll文件丢失,打印机死活连不上,今天就来给大家详细讲解一下这个问题的解... 不知道大家在使用电脑的时候是否遇到过关于win32spl.dll文件丢失的问题,win32spl.dl

mysql删除无用用户的方法实现

《mysql删除无用用户的方法实现》本文主要介绍了mysql删除无用用户的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 1、删除不用的账户(1) 查看当前已存在账户mysql> select user,host,pa

电脑提示msvcp90.dll缺少怎么办? MSVCP90.dll文件丢失的修复方法

《电脑提示msvcp90.dll缺少怎么办?MSVCP90.dll文件丢失的修复方法》今天我想和大家分享的主题是关于在使用软件时遇到的一个问题——msvcp90.dll丢失,相信很多老师在使用电脑时... 在计算机使用过程中,可能会遇到 MSVCP90.dll 丢失的问题。MSVCP90.dll 是 Mic

电脑开机提示krpt.dll丢失怎么解决? krpt.dll文件缺失的多种解决办法

《电脑开机提示krpt.dll丢失怎么解决?krpt.dll文件缺失的多种解决办法》krpt.dll是Windows操作系统中的一个动态链接库文件,它对于系统的正常运行起着重要的作用,本文将详细介绍... 在使用 Windows 操作系统的过程中,用户有时会遇到各种错误提示,其中“找不到 krpt.dll”

电脑报错cxcore100.dll丢失怎么办? 多种免费修复缺失的cxcore100.dll文件的技巧

《电脑报错cxcore100.dll丢失怎么办?多种免费修复缺失的cxcore100.dll文件的技巧》你是否也遇到过“由于找不到cxcore100.dll,无法继续执行代码,重新安装程序可能会解... 当电脑报错“cxcore100.dll未找到”时,这通常意味着系统无法找到或加载这编程个必要的动态链接库

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

Python中如何控制小数点精度与对齐方式

《Python中如何控制小数点精度与对齐方式》在Python编程中,数据输出格式化是一个常见的需求,尤其是在涉及到小数点精度和对齐方式时,下面小编就来为大家介绍一下如何在Python中实现这些功能吧... 目录一、控制小数点精度1. 使用 round() 函数2. 使用字符串格式化二、控制对齐方式1. 使用

TP-Link PDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务

《TP-LinkPDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务》近期,路由器制造巨头普联(TP-Link)在用户群体中引发了一系列重要变动,上个月,公司发出了一则通知,明确要求所... 路由器厂商普联(TP-Link)上个月发布公告要求所有用户必须完成实名认证后才能继续使用普联提供的 D