BaseEntity.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2019-2020 Zheng Jie
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package me.zhengjie.base;
  17. import io.swagger.annotations.ApiModelProperty;
  18. import lombok.Getter;
  19. import lombok.Setter;
  20. import org.apache.commons.lang3.builder.ToStringBuilder;
  21. import org.hibernate.annotations.CreationTimestamp;
  22. import org.hibernate.annotations.UpdateTimestamp;
  23. import org.springframework.data.annotation.CreatedBy;
  24. import org.springframework.data.annotation.LastModifiedBy;
  25. import org.springframework.data.jpa.domain.support.AuditingEntityListener;
  26. import javax.persistence.Column;
  27. import javax.persistence.EntityListeners;
  28. import javax.persistence.MappedSuperclass;
  29. import java.io.Serializable;
  30. import java.lang.reflect.Field;
  31. import java.sql.Timestamp;
  32. /**
  33. * 通用字段, is_del 根据需求自行添加
  34. * @author Zheng Jie
  35. * @Date 2019年10月24日20:46:32
  36. */
  37. @Getter
  38. @Setter
  39. @MappedSuperclass
  40. @EntityListeners(AuditingEntityListener.class)
  41. public class BaseEntity implements Serializable {
  42. @CreatedBy
  43. @Column(name = "create_by", updatable = false)
  44. @ApiModelProperty(value = "创建人", hidden = true)
  45. private String createBy;
  46. @LastModifiedBy
  47. @Column(name = "update_by")
  48. @ApiModelProperty(value = "更新人", hidden = true)
  49. private String updateBy;
  50. @CreationTimestamp
  51. @Column(name = "create_time", updatable = false)
  52. @ApiModelProperty(value = "创建时间", hidden = true)
  53. private Timestamp createTime;
  54. @UpdateTimestamp
  55. @Column(name = "update_time")
  56. @ApiModelProperty(value = "更新时间", hidden = true)
  57. private Timestamp updateTime;
  58. /* 分组校验 */
  59. public @interface Create {}
  60. /* 分组校验 */
  61. public @interface Update {}
  62. @Override
  63. public String toString() {
  64. ToStringBuilder builder = new ToStringBuilder(this);
  65. Field[] fields = this.getClass().getDeclaredFields();
  66. try {
  67. for (Field f : fields) {
  68. f.setAccessible(true);
  69. builder.append(f.getName(), f.get(this)).append("\n");
  70. }
  71. } catch (Exception e) {
  72. builder.append("toString builder encounter an error");
  73. }
  74. return builder.toString();
  75. }
  76. }