package com.jn.tpr.entity.basic; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.io.Serializable; import java.lang.reflect.Type; /** * 基础实体类,所有的实体都应继承当前的基础实体类 */ @SuppressWarnings("serial") public class Entity<TEntity> implements Serializable { /** * 对象复制 */ public TEntity copy(Entity entity) { Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); TEntity copy = (TEntity) gson.fromJson(gson.toJson(entity), this.getClass()); return copy; } /** * 转换为json字符串 */ public String toJson() { return new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(this); } public Entity() { } public TEntity toEntity(String json) { return (TEntity) new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(json, this.getClass()); } public TEntity toEntity(String json, Type type) { return (TEntity) new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(json, type); } @Override public String toString() { return toJson(); } }