一. 实体类简述
实体类其实就是俗称的POJO,这种类一般不实现特殊框架下的接口,在程序中仅作为数据容器用来持久化存储数据用的
POJO(Plain Old Java Objects)简单的Java对象
它的一般格式就是
public class A{
private String id;
public String getId(){
return this.id;
}
public void setId(String id){
this.id = id;
}
}
其实这样写的意义就在于封装,id作为类A的成员变量,也称属性,一般情况下拥有读和写的能力,我们将id设为private,则外部无法对其直接进行操作,同时通过set方法提供了外部更改其value的方法,又通过get方法使外界能读取该成员变量的值。(必须有set,get方法)
二.序列化的必要性
Java中,一切都是对象,在分布式环境中经常需要将Object从这一端网络或设备传递到另一端。这就需要有一种可以在两端传输数据的协议。Java序列化机制就是为了解决这个问题而产生。
java对象序列化不仅保留一个对象的数据,而且递归保存对象引用的每个对象的数据。
Java 串行化技术可以使你将一个对象的状态写入一个Byte 流里,并且可以从其它地方把该Byte 流里的数据读出来,重新构造一个相同的对象。这种机制允许你将对象通过网络进行传播,并可以随时把对象持久化到数据库、文件等系统里。Java的串行化机制是RMI、EJB等技术的技术基础。用途:利用对象的串行化实现保存应用程序的当前工作状态,下次再启动的时候将自动地恢复到上次执行的状态。
序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。
对于一个存在于Java虚拟机中的对象来说,其内部的状态只保持在内存中。JVM停止之后,这些状态就丢失了。在很多情况下,对象的内部状态是需要被持久 化下来的。提到持久化,最直接的做法是保存到文件系统或是数据库之中。这种做法一般涉及到自定义存储格式以及繁琐的数据转换。对象关系映射(Object-relational mapping)是一种典型的用关系数据库来持久化对象的方式,也存在很多直接存储对象的对象数据库。 对象序列化机制(object serialization)是Java语言内建的一种对象持久化方式,可以很容易的在JVM中的活动对象和字节数组(流)之间进行转换。除了可以很简单 的实现持久化之外,序列化机制的另外一个重要用途是在远程方法调用中,用来对开发人员屏蔽底层实现细节。
三.我们应该这样用实体类
1.作为table的entity
public class Category {
private Integer id;
@JSONField(name="nameCN")
private String cateNameCN;
@JSONField(name="nameEN")
private String cateNameEN;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCateNameCN() {
return cateNameCN;
}
public void setCateNameCN(String cateNameCN) {
this.cateNameCN = cateNameCN;
}
public String getCateNameEN() {
return cateNameEN;
}
public void setCateNameEN(String cateNameEN) {
this.cateNameEN = cateNameEN;
}
public Category() {
super();
}
public Category(Integer id, String cateNameCN, String cateNameEN) {
super();
this.id = id;
this.cateNameCN = cateNameCN;
this.cateNameEN = cateNameEN;
}
public static Category fromResultSet(ResultSet rs) throws SQLException {
return new Category(rs.getInt("Id"), rs.getString("cateNameCN"), rs.getString("cateNameEN"));
}
2.request和response中用于接收参数的entity
public class PlatformRequestHeader {
@JSONField(name = "uid")
private String userId;
@JSONField(name = "clid")
private String clientId;
@JSONField(name = "cid")
private Integer companyId;
@JSONField(name = "uip")
private String userIp;
@JSONField(name = "rid")
private String requestId;
public PlatformRequestHeader() {
this.userId = "";
this.clientId = "";
this.companyId = -1;
this.userIp = "";
this.requestId = "";
}
public PlatformRequestHeader(String userId, String clientId, Integer companyId, String userIp, String requestId) {
this.userId = userId;
this.clientId = clientId;
this.companyId = companyId;
this.userIp = userIp;
this.requestId = requestId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public Integer getCompanyId() {
return companyId;
}
public void setCompanyId(Integer companyId) {
this.companyId = companyId;
}
public String getUserIp() {
return userIp;
}
public void setUserIp(String userIp) {
this.userIp = userIp;
}
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
@Override
public String toString() {
return "{\"uid\":\"" + userId + "\",\"clid\":\"" + clientId + "\",\"cid\":" + companyId + ",\"uip\":\"" + userIp
+ "\",\"rid\":\"" + requestId + "\"}";
}
}
public class ResponseModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@JSONField(name = "ers")
private List<ErrorInfo> errors;
@JSONField(name = "err")
private ErrorInfo errorInfo;
@JSONField(name = "data")
private Object data;
public ErrorInfo getErrorInfo() {
return errorInfo;
}
public void setErrorInfo(ErrorInfo errorInfo) {
this.errorInfo = errorInfo;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public List<ErrorInfo> getErrors() {
return errors;
}
public void setErrors(List<ErrorInfo> errors) {
this.errors = errors;
}
public ResponseModel() {
}
public ResponseModel(List<ErrorInfo> errors, Object object) {
this.errors = errors;
this.data = object;
}
public ResponseModel(ErrorInfo error, Object object) {
this.errorInfo=error;
this.data = object;
}
3.logic里面的工具entity-----类似于map(不过里面只有一组值)
public class Tuple implements Serializable {
private static final long serialVersionUID = -8719929625763890308L;
public static <A, B> Tuple2<A, B> tuple(A item1, B item2) {
return new Tuple2<A, B>(item1, item2);
}
public static <A, B, C> Tuple3<A, B, C> tuple(A item1, B item2,
C item3) {
return new Tuple3<A, B, C>(item1, item2, item3);
}
public static final class Tuple2<A, B> extends Tuple implements
Serializable {
/**
* gen by eclipse
*/
private static final long serialVersionUID = 7263645006696591635L;
private A item1;
private B item2;
public Tuple2() {
}
public Tuple2(A item1, B item2) {
this.item1 = item1;
this.item2 = item2;
}
public A getItem1() {
return item1;
}
public void setItem1(A item1) {
this.item1 = item1;
}
public B getItem2() {
return item2;
}
public void setItem2(B item2) {
this.item2 = item2;
}
}
public static final class Tuple3<A, B, C> extends Tuple implements
Serializable {
/**
* gen by eclipse
*/
private static final long serialVersionUID = 482545776907024160L;
private A item1;
private B item2;
private C item3;
public Tuple3(A item1, B item2, C item3) {
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
}
public A getItem1() {
return item1;
}
public void setItem1(A item1) {
this.item1 = item1;
}
public B getItem2() {
return item2;
}
public void setItem2(B item2) {
this.item2 = item2;
}
public C getItem3() {
return item3;
}
public void setItem3(C item3) {
this.item3 = item3;
}
}
public static final class Tuple4<A, B, C, D> extends Tuple implements
Serializable {
/**
* gen by eclipse
*/
private static final long serialVersionUID = 482545776907024160L;
private A item1;
private B item2;
private C item3;
private D item4;
public Tuple4(A item1, B item2, C item3, D item4) {
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
this.item4 = item4;
}
public A getItem1() {
return item1;
}
public void setItem1(A item1) {
this.item1 = item1;
}
public B getItem2() {
return item2;
}
public void setItem2(B item2) {
this.item2 = item2;
}
public C getItem3() {
return item3;
}
public void setItem3(C item3) {
this.item3 = item3;
}
public D getItem4() {
return item4;
}
public void setItem4(D item4) {
this.item4 = item4;
}
}
}
备注:实体类就是有set和get方法的简单JAVA对象,定义的时候一定要记住要序列化哦!!!