MapStruct使用之接入Spring

接入Spring,使用Spring的依赖注入,能使MapStruct变得更加强大。 创建需要映射的POJO @Data @Builder public class X { private String firstName; private Integer type; } @Data @Builder public class Y { private String firstName; private String typeName; } 创建Spring的service @Service public class TypeService { public String convert(Integer type){ String typeName = "未知"; if (type == null) { return typeName; } switch (type) { case 1: typeName = "飞机"; break; case 2: typeName = "大炮"; break; case 3: typeName = "坦克"; break; case 4: typeName = "潜艇"; break; default: break; } return typeName; } } 编写映射接口 在 @Mapper 注解中,增加 componentModel属性,指定为 spring 。 然后使用 @Autowired 注入TypeService。 ...

2023-08-10 · 1 分钟 · 435 字

MapStruct使用之万能自定义方法处理

上面的情况都是比较常见、简单的情况,还有更复杂的情况。 比如当字段是null时,可以使用上面的方法处理,但是字段类型String值为"“时,那么该字段不是null就无法适用了。 亦或者,当字段为0或者某些特定值时,需要进行特殊的默认值处理。 ...

2023-08-10 · 3 分钟 · 1223 字

MapStruct使用之默认值

当源对象字段值为null时给定默认值 通常的情况时这样,源对象中的字段如果不为null,那么直接给目标对象中的对应字段进行赋值。 但是如果源对象中的字段是null,那么给目标对象中的对应字段赋默认值。 ...

2023-08-10 · 2 分钟 · 814 字

MapStruct使用之基础

基础映射 创建需要映射的POJO 咱们使用两个基础对象做示范。 注意:代码中使用了lombok @Data @Builder public class X { private Long id; private String name; private String description; } @Data @Builder public class Y { private Long id; private String name; private String description; } 编写映射接口 @Mapper public interface XYMapper { XYMapper INSTANCE = Mappers.getMapper(XYMapper.class); Y source2Destination(X source); } 生成的接口实现 public class XYMapperImpl implements XYMapper { @Override public Y source2Destination(X source) { if ( source == null ) { return null; } Y.YBuilder y = Y.builder(); y.id( source.getId() ); y.name( source.getName() ); y.description( source.getDescription() ); return y.build(); } } 测试验证 public class Test { public static void main(String[] args) { X x = X.builder().id(1L).name("哈利路亚").description("哈哈").build(); System.out.println(x); Y y = XYMapper.INSTANCE.source2Destination(x); System.out.println(y); } } X(id=1, name=哈利路亚, description=哈哈) Y(id=1, name=哈利路亚, description=哈哈) 字段名不同的映射 创建需要映射的POJO @Data @Builder public class X { private Long id; private String name; private String description; } @Data @Builder public class Y { private Long targetId; private String placeName; private String desc; } 编写映射接口 使用 @Mapping 注解配置映射的字段名称。 ...

2023-08-10 · 4 分钟 · 1777 字

如何写出缓存友好的代码(二)

基于缓存的两个局部性,可以充分利用这两个局部性写出缓存友好的代码。 0 时间局部性 时间局部性最常见的例子就是循环中访问同一变量了。 比如下面求min(inclusive)到max(exclusive)之间的整数之和: ...

2020-10-03 · 1 分钟 · 468 字

如何写出缓存友好的代码(一)

本周三(2020-09-23)我进行了disruptor的分享,其中disruptor的一个优化就是缓存友好。 我就上网搜了资料,分享下在实际工作中,如何写出缓存友好的代码。 ...

2020-09-25 · 1 分钟 · 399 字

arthas使用技巧七:ognl增强

在官方github中的issue-482中介绍了使用tt命令获取执行现场,然后用ognl获取spring的ApplicationContext从而为所欲为的示例。 ...

2020-07-16 · 2 分钟 · 655 字

arthas使用技巧六:常见错误

0、无法连接jvm 错误信息如下: com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded at sun.tools.attach.LinuxVirtualMachine.<init>(LinuxVirtualMachine.java:106) at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:78) at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:250) at com.taobao.arthas.core.Arthas.attachAgent(Arthas.java:85) at com.taobao.arthas.core.Arthas.<init>(Arthas.java:28) at com.taobao.arthas.core.Arthas.main(Arthas.java:123) [ERROR] attach fail, targetPid: 6958 排查思路如下: 目标pid是否正确,可以通过jps或ps进行确认 观察pid的执行用户是否和执行arthas的用户一致,比如常见的跑jvm的是tomcat用户,但是登录用户并不是 ...

2020-04-28 · 2 分钟 · 637 字

arthas使用技巧五:重新定义七点半

重新定义七点半,不知道你有没有听过这个梗。 我不知道这个梗最初是来源于哪里,我第一次见是在老罗的发布会,因为老罗的发布会总会有各种各样的问题,说是七点半开,从来没有准时过,所以说,“重新定义了七点半”。 ...

2020-04-28 · 2 分钟 · 765 字

arthas使用技巧四:ognl魔法变变变

本篇让我来体验一下ognl的强大魔法。 有这么一个场景,你的接口中有这么一个方法,清除所有的东东,让数据走上正轨。但是这个方法,你没有在后台管理系统调用,导致你现在无法调用这个方法来清除数据。 ...

2019-11-04 · 2 分钟 · 762 字