BasePo.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.malk.base;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.excel.annotation.ExcelIgnore;
  4. import com.fasterxml.jackson.annotation.JsonFormat;
  5. import com.fasterxml.jackson.annotation.JsonIgnore;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. import org.springframework.data.annotation.CreatedDate;
  9. import org.springframework.data.annotation.LastModifiedDate;
  10. import org.springframework.data.jpa.domain.support.AuditingEntityListener;
  11. import javax.persistence.*;
  12. import java.util.Date;
  13. /**
  14. * 实体基类
  15. *
  16. * @MappedSuperclass 作为公共属性场景. 类注解, 标注后该类将不是一个完整的实体类,也不会映射到数据库表,但其的属性都将映射到其子类的数据库字段中
  17. * @Cloumn java为小驼峰命名,数据库为下划线命名,若有差异,通过name指定表字段. 若相同则可不用指定name [直接设置字段值即可,无效额外注解]
  18. * -
  19. * 功能使用自带
  20. * -
  21. * @JsonFormat(pattern = "yyyy-MM-dd HH:smm:ss", timezone = "GMT+8"),数据库 Date 类型序列化后转到前台的指定格式【jackjson】
  22. * @DateTimeFormat @DateTimeFormat(pattern = "yyyy-MM-dd"),使用和@jsonFormat差不多,前台传入的按照指定格式自动转为Date储存
  23. * @JsonIgnoreProperties 类注解,作用是json序列化时将bean中的一些属性忽略掉,序列化和反序列化都受影响。支持多个属性 [也用于双向绑定解决循环序列化]
  24. * @JsonIgnore 此注解用于属性或者方法上(最好是属性上),作用和上面的@JsonIgnoreProperties一样,屏蔽该字段在序列化和数据发挥会自动忽略
  25. * @JsonGetter 用于序列化, 还可指定返回属性名,@JsonSetter 用于反序列化。注意@JsonGetter比@JsonProperty的优先级高,同时存在属性忽略会失效
  26. * @Transient ORM框架将忽略该属性,不入库。如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则ORM框架默认其注解为@Basic
  27. * @Temporal & @JsonFormat: fixme: 指定时区, new Date 会默认当前系统时区, 不添加 json 时区注解, 会出现序列化后的对象时间不是 GMT 时区 [方法2见BaseDto, jsonFormatDateTime]
  28. */
  29. @MappedSuperclass
  30. @Data
  31. @NoArgsConstructor
  32. @EntityListeners(AuditingEntityListener.class)
  33. public abstract class BasePo extends BaseDto {
  34. // 若是实体若不直接在 com.malk 下, 可声明继承id, 避免编辑器提示 [不加也不影响编译以及运行] [ppExt: 现有表如u8, 不继承 BaseDto, 避免默认id与时间字段匹配异常]
  35. @ExcelIgnore
  36. @Id
  37. @JsonIgnore
  38. @GeneratedValue(strategy = GenerationType.IDENTITY)
  39. public Long id;
  40. @ExcelIgnore
  41. @CreatedDate
  42. @Temporal(TemporalType.TIMESTAMP)
  43. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  44. private Date createTime;
  45. @ExcelIgnore
  46. @LastModifiedDate
  47. @Temporal(TemporalType.TIMESTAMP)
  48. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  49. private Date updateTime;
  50. public void upsert(BasePo po_old) {
  51. if (ObjectUtil.isNotNull(po_old)) {
  52. this.id = po_old.id;
  53. this.setCreateTime(po_old.getCreateTime());
  54. }
  55. }
  56. }