There is a better way to practice:
Create a parent class first
/*General fields for all domain classes*/ @Data public class BaseDomain implements Serializable { /*These values are common. During the INSERT/UPDATE operation, these values will be automatically updated. For the specific implementation, please refer to MyMetaObjecthandler*/ @TableField(fill = FieldFill.INSERT_UPDATE) public Integer yn; @TableField(fill = FieldFill.INSERT_UPDATE) public String creator; @TableField(fill = FieldFill.INSERT_UPDATE) public String updater; @TableField(fill = FieldFill.INSERT_UPDATE) public Date createTime; @TableField(fill = FieldFill.INSERT_UPDATE) public Date updateTime; }
Then our entity classes inherit this class
@TableName(value ="student") @Data @AllArgsConstructor @NoArgsConstructor public class Student extends BaseDomain { @TableId(type = ) private Integer id; private String studentName; private String studentPassword; private String teacheruuid; private String phone; private String address; @TableField(exist = false) private static final long serialVersionUID = 1L; @Override public boolean equals(Object that) { if (this == that) { return true; } if (that == null) { return false; } if (getClass() != ()) { return false; } Student other = (Student) that; return (() == null ? () == null : ().equals(())) && (() == null ? () == null : ().equals(())) && (() == null ? () == null : ().equals(())) && (() == null ? () == null : ().equals(())) && (() == null ? () == null : ().equals(())) && (() == null ? () == null : ().equals(())) && (() == null ? () == null : ().equals(())) && (() == null ? () == null : ().equals(())) && (() == null ? () == null : ().equals(())) && (() == null ? () == null : ().equals(())) && (() == null ? () == null : ().equals(())); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); result = prime * result + ((getStudentName() == null) ? 0 : getStudentName().hashCode()); result = prime * result + ((getStudentPassword() == null) ? 0 : getStudentPassword().hashCode()); result = prime * result + ((getTeacheruuid() == null) ? 0 : getTeacheruuid().hashCode()); result = prime * result + ((getPhone() == null) ? 0 : getPhone().hashCode()); result = prime * result + ((getAddress() == null) ? 0 : getAddress().hashCode()); result = prime * result + ((getYn() == null) ? 0 : getYn().hashCode()); result = prime * result + ((getCreator() == null) ? 0 : getCreator().hashCode()); result = prime * result + ((getUpdater() == null) ? 0 : getUpdater().hashCode()); result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode()); return result; } @Override public String toString() { StringBuilder sb = new StringBuilder(); (getClass().getSimpleName()); (" ["); ("Hash = ").append(hashCode()); (", , studentName=").append(studentName); (", studentPassword=").append(studentPassword); (", teacheruu, phone=").append(phone); (", address=").append(address); (", yn=").append(yn); (", creator=").append(creator); (", updater=").append(updater); (", createTime=").append(createTime); (", updateTime=").append(updateTime); (", serialVersionUID=").append(serialVersionUID); ("]"); return (); } }
Then write MetaObjectHandler
/*The function of this class is: * Added the INSERT_UPDATE set to the entity class to automatically fill the property in the new or updated operation*/ * @Component //Note that you need to put it in the container herepublic class MyMetaObjecthandler implements MetaObjectHandler { // There are several public attributes// public Integer yn; // public String creator; // public String updater; // public Date createTime; // public Date updateTime; @Override public void insertFill(MetaObject metaObject) { Date now = new Date(); /*If the object has no value when the insertion operation, the object has a default fill*/ if (("yn") == null) { setFieldValByName("yn", 1, metaObject); } if (("creator") == null) { /* Here you can combine the security context to get the operator. I wrote it here simply, so I directly use admin to assign values*/ setFieldValByName("creator", "admin", metaObject); } if (("updater") == null) { /* Here you can combine the security context to get the operator. I wrote it here simply, so I directly use admin to assign values*/ setFieldValByName("updater", "admin", metaObject); } if (("createTime") == null) { setFieldValByName("createTime", now, metaObject); } if (("updateTime") == null) { setFieldValByName("updateTime", now, metaObject); } } @Override public void updateFill(MetaObject metaObject) { /*The logic is different when updating. The prerequisite for updating is that there is an object, so some values of the object cannot be moved casually. * For example, yn, and the updater and update time can change*/ if (("updater") == null) { /* Here you can combine the security context to get the operator. I wrote it here simply, so I directly use admin to assign values*/ setFieldValByName("updater", "admin1231231", metaObject); } if (("updateTime") == null) { setFieldValByName("updateTime", new Date(), metaObject); } } }
This is fine. Of course, you can also do these operations on a specified class. For example, I just want to do these operations on objects of Student class when updating and inserting them? You can use metaObject to achieve it.
For example:
if ("".equals( ().getClass().getName()) ) { Date now = new Date(); /*If the object has no value when the insertion operation, the object has a default fill*/ if (("yn") == null) { setFieldValByName("yn", 1, metaObject); } if (("creator") == null) { /* Here you can combine the security context to get the operator. I wrote it here simply, so I directly use admin to assign values*/ setFieldValByName("creator", "admin", metaObject); } if (("updater") == null) { /* Here you can combine the security context to get the operator. I wrote it here simply, so I directly use admin to assign values*/ setFieldValByName("updater", "admin", metaObject); } if (("createTime") == null) { setFieldValByName("createTime", now, metaObject); } if (("updateTime") == null) { setFieldValByName("updateTime", now, metaObject); } }
After testing
()
()
()
All are in line with our expectations
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.