ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
.append("id", this.id);
系统中一般都要打印日志的,因为所有实体的toString()方法 都用的是简单的"+",因为每"+" 一个就会 new 一个 String 对象,这样如果系统内存小的话会暴内存(前提系统实体比较多)。使用ToStringBuilder就可以避免暴内存这种问题的。
ToStringStyle参数说明:
DEFAULT_STYLE
com.entity.Person@182f0db[name=John Doe,age=33,smoker=false]
MULTI_LINE_STYLE
com.entity.Person@182f0db[
name=John Doe
age=33
smoker=false
]
NO_FIELD_NAMES_STYLE
com.entity.Person@182f0db[John Doe,33,false]
SHORT_PREFIX_STYLE (即截去了包名)
Person[name=John Doe,age=33,smoker=false]
SIMPLE_STYLE
John Doe,33,false