当前位置: 首页 > news >正文

简化java代码:mapstruct + 策略模式

目录

目的

准备

注意

相同类型-属性名不同

实体类 

映射

使用

验证-查看实现类

测试

不同类型(策略模式)

 实体类

映射

工具类

使用:对象拷贝

验证-查看实现类

测试

使用:集合拷贝

测试

策略模式说明

准备-依赖


目的

简化 BeanUtils.copyProperties 属性对拷代码,自动生成空判断,结合策略模式自定义转换

集合对拷:一行代码就能完成以前 先new,再 for循环,再add的多行代码,而且也不用判空

准备

这里我将开发中经常用到的全部依赖都列举出来(若下载不下来依赖,将settings.xml替换成我博客存档的那一份),此处为了不影响观看,放在文章末尾处。

注意

写好转换之后,maven 记得 clean install,查看一下生成的实现类有没有问题

实现类在  target 里面,若target没有,执行下列操作

相同类型-属性名不同

将 下面 Doctor 中的  i1  s1  分别转给 DoctorDto 中的  i2  s2

实体类 

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Doctor {private int id;private String name;String s1;int i1;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DoctorDto {private int id;private String name;private String s2;private int i2;
}

映射

@Mapper(componentModel = "spring")
public interface BeanConvert {BeanConvert INSTANCE = Mappers.getMapper(BeanConvert.class);/*** 不一致的属性映射*/@Mappings({@Mapping(target = "i2", source = "i1"),@Mapping(target = "s2", source = "s1")})DoctorDto toDto(Doctor doctor);
}

使用

    @GetMapping("/test")public DoctorDto get() {Doctor doctor = new Doctor();doctor.setId(1);doctor.setName("张三");doctor.setI1(1);doctor.setS1("1");return BeanConvert.INSTANCE.toDto(doctor);}

验证-查看实现类

clean install  重启

测试

可以看到 i1,s1中的值已经过来了

不同类型(策略模式)

 实体类

将下面 Sku2 中的  Long date  Integer code  分别转给 SkuDTO2 中的  Date date  String value

 一个是时间戳转换,一个是枚举转换

@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Sku2 {Long skuId;String skuCode;String skuPrice;List<String> nameList;Long date;Integer code;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class SkuDTO2 {Long skuId;String skuCode;String skuPrice;List<String> nameList;Date date;String value;
}

映射

uses 定义了两个策略,一个负责转换时间戳,一个负责转换枚举

@Mapper(componentModel = "spring", uses = {TimeConvertStrategy.class, EnumConvertStrategy.class})
public interface BeanConvert {/*** 策略模式测试*/@Mapping(target = "value", source = "code")SkuDTO2 domain2Dto(Sku2 sku2);}
@Component
public class EnumConvertStrategy {public String convert(Integer code) {return MyEnum.getByCode(code);}}
@Component
public class TimeConvertStrategy {public Date date2TimeStamp(Long timeStamp) {if (timeStamp == null) {return null;}return new Date(timeStamp);}}
public enum MyEnum {A(1,"哈哈"),B(2,"呵呵");private final Integer code;private final String msg;MyEnum(int code, String msg) {this.code = code;this.msg = msg;}public Integer getCode() {return code;}public String getMsg() {return msg;}public static String getByCode(Integer code) {if (code == null) {return null;}for (MyEnum value : MyEnum.values()) {if (value.getCode().equals(code)) {return value.getMsg();}}return null;}}

工具类

这里使用工具类,主要是辅助容器中的bean初始化的,包括我们定义的策略的初始化

/*** ApplicationContextAware 接口可以让 Bean 获取到 ApplicationContext 对象* 通过这个对象,可以获取 Spring 容器中的其他 Bean实例 或一些组件*/
@Component
public class ConvertSupport implements ApplicationContextAware {private static BeanConvert beanConvert;/*** 启动的时候,直接获取到 beanConvert 的实例*/@Overridepublic void setApplicationContext(ApplicationContext context) throws BeansException {// 这里如果没有执行,说明没有注入容器,但是我有了@Component注解,说明其未生效,主启动类@ComponentScan指定一下if (beanConvert == null) {beanConvert = context.getBean(BeanConvert.class);System.out.println("执行过这里");}}/*** 列表转换*/public static <I, O> List<O> copyList(List<I> inputList, BiFunction<BeanConvert, I, O> function) {if (CollectionUtils.isEmpty(inputList)) {return Collections.emptyList();}List<O> resultList = new ArrayList<>(inputList.size());for (I input : inputList) {resultList.add(function.apply(beanConvert, input));}return resultList;}/*** bean to bean*/public static <I, O> O castBean(I input, BiFunction<BeanConvert, I, O> function) {return function.apply(beanConvert, input);}}

使用:对象拷贝

    @GetMapping("/test2")public SkuDTO2 get2() {Sku2 sku2 = new Sku2();sku2.setSkuId(1L);sku2.setSkuCode("2");sku2.setNameList(Lists.newArrayList("测", "试"));sku2.setDate(new Date().getTime());sku2.setCode(1);return ConvertSupport.castBean(sku2, BeanConvert::domain2Dto);}

验证-查看实现类

clean install  重启

测试

使用:集合拷贝

    @GetMapping("/test3")public List<DoctorDto> get3() {List<Doctor> list = Lists.newArrayList(new Doctor(1, "张三", "1", 1),new Doctor(2, "李四", "2", 2),new Doctor(3, "王五", "3", 3));return ConvertSupport.copyList(list, BeanConvert::toDto);}

此后,一行代码就能完成以前 先new,再 for循环,再add的多行代码,而且也不用判空

测试

策略模式说明

上面使用的是用来处理不同类型,平常去除 if else 的使用方式:

先定义两个 策略,和上面类似,spring管理后,通过策略内部判断决定怎么走,方法再抽象出一层

/*** 策略1*/
@Component
public class Situation1 implements Situation {private static final String value = "用户传过来的值为1";/*** 判断进入哪个策略(if)*/@Overridepublic Boolean judge(String val) {return value.equals(val);}/*** 逻辑处理*/@Overridepublic int logic(int a, int b) {return a + b;}
}
/*** 策略2*/
@Component
public class Situation2 implements Situation {private static final String value = "用户传过来的值为2";/*** 判断进入哪个策略(if)*/@Overridepublic Boolean judge(String val) {return value.equals(val);}/*** 逻辑处理*/@Overridepublic int logic(int a, int b) {return a - b;}
}
public interface Situation {/*** 判断进入哪个策略(if)*/Boolean judge(String val);/*** 逻辑处理*/int logic(int a, int b);
}
@RestController
public class StrategyTest {@Autowiredprivate ApplicationContext applicationContext;@GetMapping("/test/st")public void test() {String value = "用户传过来的值为2";Map<String, Situation> beans = applicationContext.getBeansOfType(Situation.class);beans.forEach((k, Strategy) -> {// 判断用户传过来的值,从而决定进入哪个策略if (Strategy.judge(value)) {int num = Strategy.logic(8, 2);System.out.println(num);}});}
}

准备-依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.mytest</groupId><artifactId>springboot-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-demo</name><description>springboot-demo</description><properties><org.mapstruct.version>1.5.0.RC1</org.mapstruct.version><org.projectlombok.version>1.18.22</org.projectlombok.version><lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--HuTool为我们提供的一些便捷工具。--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>4.1.14</version></dependency><!--Valid--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId><version>2.3.2.RELEASE</version></dependency><!--自定义注解--><dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId><version>2.0.1.Final</version></dependency><!-- 定时任务 --><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><exclusions><exclusion><groupId>com.mchange</groupId><artifactId>c3p0</artifactId></exclusion></exclusions></dependency><!-- 解析客户端操作系统、浏览器等 --><dependency><groupId>eu.bitwalker</groupId><artifactId>UserAgentUtils</artifactId><version>1.21</version></dependency><!-- pagehelper 分页插件 注意 pagehelper 和 spring-boot-starter-parent 版本,容易出现循环依赖 一般通过加@Lazy解决,这里通过版本号解决--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.1</version></dependency><!--HttpUtils需要的所有依赖--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><!--mybatisplus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency><!--DateTime 需要的依赖,由于没有版本号管理,如果不写version,上面有一处会爆红--><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>2.9.4</version></dependency><!--代码生成器--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.4.1</version></dependency><!--velocity模板引擎--><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.3</version></dependency><!-- easyexcel依赖 --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.0.5</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.3.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.3.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.3.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><!--Lists.partition 需要的依赖--><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>25.1-jre</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><exclusion><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId></exclusion></exclusions></dependency><!--        &lt;!&ndash;jedis,redis客户端&ndash;&gt;--><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency><!--        &lt;!&ndash;使用redisson作为所有分布式锁,分布式对象等功能框架,也可以使用springboot的方式,就不用自己@Configuration了&ndash;&gt;--><dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.13.3</version></dependency><!--mysql驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.17</version></dependency><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>${org.mapstruct.version}</version></dependency><!-- 特别注意 mapstruct 和 lombok 的顺序,顺序出问题,实现类就不映射了 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${org.projectlombok.version}</version><scope>provided</scope></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.10</version></dependency><!--CollectionUtils依赖--><dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.2.2</version></dependency><dependency><groupId>com.google.code.google-collections</groupId><artifactId>google-collect</artifactId><version>snapshot-20080530</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<!--        <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></dependency>--><dependency><groupId>com.alibaba</groupId><artifactId>transmittable-thread-local</artifactId><version>2.11.5</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>21.0</version></dependency><dependency><groupId>javax.persistence</groupId><artifactId>persistence-api</artifactId><version>1.0</version></dependency><!--注意:3.0.0 版本   http://localhost:8081/swagger-ui.html   页面可能登陆不上去--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency></dependencies><!-- 特别注意 mapstruct 和 lombok 的顺序,顺序出问题,实现类就不映射了 --><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${org.projectlombok.version}</version></path><!-- This is needed when using Lombok 1.18.16 and above --><path><groupId>org.projectlombok</groupId><artifactId>lombok-mapstruct-binding</artifactId><version>${lombok-mapstruct-binding.version}</version></path><!-- Mapstruct should follow the lombok path(s) --><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>${org.mapstruct.version}</version></path></annotationProcessorPaths></configuration></plugin></plugins><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include><include>**/*.yml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include><include>**/*.yml</include></includes><filtering>false</filtering></resource></resources></build></project>

相关文章:

简化java代码:mapstruct + 策略模式

目录 目的 准备 注意 相同类型-属性名不同 实体类 映射 使用 验证-查看实现类 测试 不同类型(策略模式) 实体类 映射 工具类 使用&#xff1a;对象拷贝 验证-查看实现类 测试 使用&#xff1a;集合拷贝 测试 策略模式说明 准备-依赖 目的 简化 BeanUtils.…...

【Java】SpringMVC路径写法

1、多级路径 ✅类路径和方法路径都可以写成多级 ✅其中&#xff0c;类路径写在方法路径前面 ✅与Servlet不同&#xff0c;SpringMVC中写不写“/”都可以 RequestMapping("/hello/t1") RestController public class HelloSpring {RequestMapping( value "world…...

数据结构之生成树及最小生成树

数据结构之生成树及最小生成树 1、生成树概念2、最小生成树 数据结构是程序设计的重要基础&#xff0c;它所讨论的内容和技术对从事软件项目的开发有重要作用。学习数据结构要达到的目标是学会从问题出发&#xff0c;分析和研究计算机加工的数据的特性&#xff0c;以便为应用所…...

【java面试】常见问题(超详细)

目录 一、java常见问题JDK和JRE的区别是什么&#xff1f;Java中的String类是可变的还是不可变的&#xff1f;Java中的equals方法和hashCode方法有什么关系&#xff1f;Java中什么是重载【Overloading】&#xff1f;什么是覆盖【Overriding】&#xff1f;它们有什么区别&#xf…...

Labview for循环精讲

本文详细介绍Labview中For循环的使用方法&#xff0c;从所有细节让你透彻的看明白For循环是如何使用的&#xff0c;如果有帮助的话记得点赞加关注~ 1. For循环结构 从最简单的地方讲起&#xff0c;一个常用的for循环结构是由for循环结构框图、循环次数、循环计数(i)三部分组成…...

【STM32】STM32学习笔记-W25Q64简介(37)

00. 目录 文章目录 00. 目录01. SPI简介02. W25Q64简介03. 硬件电路04. W25Q64框图05. Flash操作注意事项06. 预留07. 附录 01. SPI简介 在大容量产品和互联型产品上&#xff0c;SPI接口可以配置为支持SPI协议或者支持I 2 S音频协议。SPI接口默认工作在SPI方式&#xff0c;可以…...

clickhouse数据库 使用http 方式交付查询sql

今天使用clickhouse 的HTTP 方式进行查询语句 clickhouse 服务 搭建在192.168.0.111 上面 那么我们如何快速的去查询呢 如下 我们可以使用curl 功能 或者直接在浏览器上输入对应的查询命令 如下&#xff1a; http://192.168.0.111:8123/userdefault&password123456&…...

深度学习-循环神经网络-RNN实现股价预测-LSTM自动生成文本

序列模型(Sequence Model) 基于文本内容及其前后信息进行预测 基于目标不同时刻状态进行预测 基于数据历史信息进行预测 序列模型:输入或者输出中包含有序列数据的模型 突出数据的前后序列关系 两大特点: 输入(输出)元素之间是具有顺序关系。不同的顺序,得到的结果应…...

案例分享 | 助力数字化转型:嘉为科技项目管理平台上线

嘉为科技项目管理平台&#xff08;一期&#xff09;基于易趋&#xff08;EasyTrack&#xff09;进行实施&#xff0c;通过近一年的开发及试运行&#xff0c;现已成功交付上线、推广使用&#xff0c;取得了良好的应用效果。 1.关于广州嘉为科技有限公司&#xff08;以下简称嘉为…...

深入理解 MySQL 中的 HAVING 关键字和聚合函数

深入理解 MySQL 中的 HAVING 关键字和聚合函数 在处理数据库查询时&#xff0c;尤其是涉及到大量数据分析和报表生成的场合&#xff0c;了解如何有效使用 SQL 语句中的 HAVING 关键字和聚合函数变得尤为重要。 什么是 HAVING 关键字&#xff1f; HAVING 关键字在 SQL 语句中…...

GPT4.5人工智能即将来临,ChatGPT的正面影响和负面影响(好处和坏处),利弊分析

ChatGPT来了&#xff0c;对我们影响大不大&#xff1f; 近年来&#xff0c;人工智能技术的飞速进步催生了ChatGPT——一种强大的人工智能语言模型。其杰出的生成能力使其能够与人类进行自然、流畅的交流&#xff0c;从而在教育、医疗和娱乐等多个领域展现出巨大的应用潜力。然…...

条款47:请使用traits classes表现类型信息

1.前言 STL主要由“用以表现容器&#xff0c;迭代器和算法”的template构成&#xff0c;但也覆盖若干工具性templates&#xff0c;其中一个名为advance&#xff0c;用来将某个迭代器移动某个给定距离&#xff1a; tempalte<typename IterT,typename DistT>//将迭代器向…...

蓝桥杯省赛无忧 课件49 DFS-剪枝

01 数字王国之军训排队 02 特殊的三角形 03 特殊的多边形...

Linux中查看端口被哪个进程占用、进程调用的配置文件、目录等

1.查看被占用的端口的进程&#xff0c;netstat/ss -antulp | grep :端口号 2.通过上面的命令就可以列出&#xff0c;这个端口被哪些应用程序所占用&#xff0c;然后找到对应的进程PID https://img-blog.csdnimg.cn/c375eb2bed754426b373907acaa7346e.png 3.根据PID查询进程。…...

大模型面试题总结

文章目录 一、大模型(LLMs)基础面二、大模型(LLMs)进阶面三、大模型(LLMs)微调面四、大模型(LLMs)langchain面1. 基于LLM+向量库的文档对话 基础面2. 基于LLM+向量库的文档对话 优化面3. LangChain的概念面试问题4.LangChain的一些模块提问5.LangChain的业务提问6.Lang…...

Authorization Failed You can close this page and return to the IDE

一.问题描述 注册JetBrains成功&#xff0c;并且通过了学生认证&#xff0c;但在activate pycharm时&#xff0c;却显示Authorization Failed You can close this page and return to the IDE如上图 二.原因&#xff1a; 可能是因为之前使用了破解版pycharm 三.解决方法&am…...

【时间序列篇】基于LSTM的序列分类-Pytorch实现 part2 自有数据集构建

系列文章目录 【时间序列篇】基于LSTM的序列分类-Pytorch实现 part1 案例复现 【时间序列篇】基于LSTM的序列分类-Pytorch实现 part2 自有数据集构建 【时间序列篇】基于LSTM的序列分类-Pytorch实现 part3 化为己用 在一个人体姿态估计的任务中&#xff0c;需要用深度学习模型…...

《设计模式的艺术》笔记 - 策略模式

介绍 策略模式定义一系列算法类&#xff0c;将每一个算法封装起来&#xff0c;并让它们可以相互替换。策略模式让算法独立于使用它的客户而变化&#xff0c;也称为政策模式。策略模式是一种对象行为模式。 实现 myclass.h // // Created by yuwp on 2024/1/12. //#ifndef DES…...

【Elasticsearch篇】详解使用RestClient操作索引库的相关操作

文章目录 &#x1f354;什么是Elasticsearch&#x1f33a;什么是RestClient&#x1f386;代码操作⭐初始化RestClient⭐使用RestClient操作索引库⭐使用RestClient删除索引库⭐使用RestClient判断索引库是否存在 &#x1f354;什么是Elasticsearch Elasticsearch是一个开源的分…...

ES数据处理方法

由于日志数据存在ES项目里&#xff0c;需要从ES中获取日志进行分析&#xff0c;使用SQL数据进行处理&#xff0c;如下&#xff1a; select traceid-- STRING COMMENT 流程id, ,appnum -- BIGINT COMMENT 迭代号, ,appversion --STRING COMMENT APP版本, ,appc…...

SkyWalking 10.2.0 SWCK 配置过程

SkyWalking 10.2.0 & SWCK 配置过程 skywalking oap-server & ui 使用Docker安装在K8S集群以外&#xff0c;K8S集群中的微服务使用initContainer按命名空间将skywalking-java-agent注入到业务容器中。 SWCK有整套的解决方案&#xff0c;全安装在K8S群集中。 具体可参…...

【WiFi帧结构】

文章目录 帧结构MAC头部管理帧 帧结构 Wi-Fi的帧分为三部分组成&#xff1a;MAC头部frame bodyFCS&#xff0c;其中MAC是固定格式的&#xff0c;frame body是可变长度。 MAC头部有frame control&#xff0c;duration&#xff0c;address1&#xff0c;address2&#xff0c;addre…...

模型参数、模型存储精度、参数与显存

模型参数量衡量单位 M&#xff1a;百万&#xff08;Million&#xff09; B&#xff1a;十亿&#xff08;Billion&#xff09; 1 B 1000 M 1B 1000M 1B1000M 参数存储精度 模型参数是固定的&#xff0c;但是一个参数所表示多少字节不一定&#xff0c;需要看这个参数以什么…...

SciencePlots——绘制论文中的图片

文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了&#xff1a;一行…...

前端倒计时误差!

提示:记录工作中遇到的需求及解决办法 文章目录 前言一、误差从何而来?二、五大解决方案1. 动态校准法(基础版)2. Web Worker 计时3. 服务器时间同步4. Performance API 高精度计时5. 页面可见性API优化三、生产环境最佳实践四、终极解决方案架构前言 前几天听说公司某个项…...

江苏艾立泰跨国资源接力:废料变黄金的绿色供应链革命

在华东塑料包装行业面临限塑令深度调整的背景下&#xff0c;江苏艾立泰以一场跨国资源接力的创新实践&#xff0c;重新定义了绿色供应链的边界。 跨国回收网络&#xff1a;废料变黄金的全球棋局 艾立泰在欧洲、东南亚建立再生塑料回收点&#xff0c;将海外废弃包装箱通过标准…...

MODBUS TCP转CANopen 技术赋能高效协同作业

在现代工业自动化领域&#xff0c;MODBUS TCP和CANopen两种通讯协议因其稳定性和高效性被广泛应用于各种设备和系统中。而随着科技的不断进步&#xff0c;这两种通讯协议也正在被逐步融合&#xff0c;形成了一种新型的通讯方式——开疆智能MODBUS TCP转CANopen网关KJ-TCPC-CANP…...

【Go】3、Go语言进阶与依赖管理

前言 本系列文章参考自稀土掘金上的 【字节内部课】公开课&#xff0c;做自我学习总结整理。 Go语言并发编程 Go语言原生支持并发编程&#xff0c;它的核心机制是 Goroutine 协程、Channel 通道&#xff0c;并基于CSP&#xff08;Communicating Sequential Processes&#xff0…...

Neo4j 集群管理:原理、技术与最佳实践深度解析

Neo4j 的集群技术是其企业级高可用性、可扩展性和容错能力的核心。通过深入分析官方文档,本文将系统阐述其集群管理的核心原理、关键技术、实用技巧和行业最佳实践。 Neo4j 的 Causal Clustering 架构提供了一个强大而灵活的基石,用于构建高可用、可扩展且一致的图数据库服务…...

微服务商城-商品微服务

数据表 CREATE TABLE product (id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 商品id,cateid smallint(6) UNSIGNED NOT NULL DEFAULT 0 COMMENT 类别Id,name varchar(100) NOT NULL DEFAULT COMMENT 商品名称,subtitle varchar(200) NOT NULL DEFAULT COMMENT 商…...