BaseDto.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.malk.base;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.serializer.SerializerFeature;
  5. import com.fasterxml.jackson.annotation.JsonInclude;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. import lombok.SneakyThrows;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.springframework.beans.BeanUtils;
  11. import org.springframework.beans.BeanWrapper;
  12. import org.springframework.beans.BeanWrapperImpl;
  13. import java.beans.PropertyDescriptor;
  14. import java.util.HashMap;
  15. import java.util.HashSet;
  16. import java.util.Map;
  17. import java.util.Set;
  18. /**
  19. * 基础对象
  20. *
  21. * @JsonInclude(JsonInclude.Include.NON_NULL):类注解过滤null字段,包含入参和返回值【参数类搭配 @Data,才可以实例化返回值以及ToString输出】
  22. * -
  23. * lombok
  24. * @Data : 注在类上,提供类的get、set、equals、hashCode、canEqual、toString方法
  25. * @AllArgsConstructor : 注在类上,提供类的全参构造
  26. * @NoArgsConstructor : 注在类上,提供类的无参构造
  27. * @Setter : 注在属性上,提供 set 方法
  28. * @Getter : 注在属性上,提供 getDefault 方法
  29. * @EqualsAndHashCode : 注在类上,提供对应的 equals 和 hashCode 方法
  30. * @Log4j/@Slf4j : 注在类上,提供对应的 Logger 对象,变量名为 log
  31. * @Builder:为类生成相对略微复杂的构建器API。来初始化实例对象::类名.属性(值).属性(值).build()
  32. * @Singular:在使用@Singular注释注释一个集合字段(使用@Builder注释类),lombok会将该构建器节点视为一个集合,并生成两个adder方法而不是setter方法::点一次集合增加一个元素
  33. * @Builder.Default:在类中id和insertTime上都添加注解@Builder.Default,当在使用这个实体对象时,就不需要在为这两个字段进行初始化值
  34. */
  35. @Data
  36. @NoArgsConstructor
  37. @JsonInclude(JsonInclude.Include.NON_NULL)
  38. public abstract class BaseDto {
  39. /**
  40. * 对象拷贝: 若是复制一个对象, 建议使用 cloneParam 避免性能问题
  41. */
  42. @Deprecated
  43. @SneakyThrows
  44. public BaseDto copyParam() {
  45. BaseDto dto = this.getClass().newInstance();
  46. BeanUtils.copyProperties(this, dto);
  47. return dto;
  48. }
  49. /**
  50. * * todo: 4.11 继承Serializable后执行深拷贝, 不能类型转换, 不继承Serializable则返回
  51. * 对象拷贝: 复制一个新的对象, 避免条件被修改, 尤其并发下分页混乱情况
  52. */
  53. public BaseDto cloneParam() {
  54. return ObjectUtil.clone(this);
  55. }
  56. /**
  57. * 对象属性合并: jda之save接口会以传入数据为准,若传入为空或不传入,更新会置空。目前解决办法两种,通过注解实现JPQL/SQL,或者查询出数据,将未传入字段属性拷贝后再更新【性能消耗】
  58. */
  59. public void mergeParam(BaseDto modifyDo) {
  60. BeanUtils.copyProperties(this, modifyDo, getNotNullPropertyNames(modifyDo));
  61. }
  62. // 忽略有值的字段
  63. private static String[] getNotNullPropertyNames(Object target) {
  64. final BeanWrapper src = new BeanWrapperImpl(target);
  65. PropertyDescriptor[] pds = src.getPropertyDescriptors();
  66. Set<String> emptyNames = new HashSet();
  67. for (PropertyDescriptor pd : pds) {
  68. Object srcValue = src.getPropertyValue(pd.getName());
  69. if (srcValue != null) {
  70. // 此处判断可根据需求修改, 目前过滤不为null
  71. emptyNames.add(pd.getName());
  72. }
  73. }
  74. String[] result = new String[emptyNames.size()];
  75. return emptyNames.toArray(result);
  76. }
  77. /**
  78. * 传入映射的Map, 将实体属性和Map的key转换, 返回Map
  79. */
  80. public Map convertEntity(Map<String, String> reflect) {
  81. Map map = JSON.parseObject(JSON.toJSONString(this, SerializerFeature.WriteNullStringAsEmpty), Map.class);
  82. Map<String, String> formData = new HashMap();
  83. for (String key : reflect.keySet()) {
  84. String content = String.valueOf(map.get(key));
  85. // json序列化已经将空字符串过滤, 若转换还有null字符串, 可能是key为null或SerializerFeature未指定到类型, 如Date
  86. if (StringUtils.isNotBlank(content) && !content.equals("null")) formData.put(reflect.get(key), content);
  87. }
  88. return formData;
  89. }
  90. /**
  91. * Map时间格式化, 直接从数据库取值后Map会有市区差, 方法1见BasePo, @Temporal & @JsonFormat 注解
  92. * -
  93. * [单独时间格式化 [废弃]]
  94. * * JSON.parseArray(JSON.toJSONString(data), Map.class).stream().map(item -> {
  95. * * item.put("tStoreInTime", UtilDateTime.formatDateTime(new Date(UtilMap.getLong(item, "tStoreInTime"))));
  96. * * return item;
  97. * * });
  98. */
  99. public static final Object jsonFormatDateTime(Object data) {
  100. return JSON.parse(JSON.toJSONString(data, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat));
  101. }
  102. }