package com.cn;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
public class t01 {
public static void main(String[] args) {
// ReflectionToStringBuilder
// 底层使用反射机制
User user = new User(1, "张三", "123456");
System.out.println(ReflectionToStringBuilder.toString(user)); // 默认
// com.cn.User@7229724f[id=1,name=张三,password=123456,classId=0,className=Def]
System.out.println(ReflectionToStringBuilder.toString(user, ToStringStyle.JSON_STYLE)); // JSON
// {"id":1,"name":"张三","password":"123456","classId":0,"className":"Def"}
System.out.println(ReflectionToStringBuilder.toString(user, ToStringStyle.SIMPLE_STYLE)); // 值
// 1,张三,123456,0,Def
System.out.println(ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE)); // 默认(换行显示)
/*
com.cn.User@7229724f[
id=1
name=张三
password=123456
classId=0
className=Def
]
*/
System.out.println(ReflectionToStringBuilder.toString(user, ToStringStyle.SHORT_PREFIX_STYLE)); // 默认(类名简称)
// User[id=1,name=张三,password=123456,classId=0,className=Def]
System.out.println(ReflectionToStringBuilder.toString(user, ToStringStyle.NO_CLASS_NAME_STYLE)); // 默认(无类名)
// [id=1,name=张三,password=123456,classId=0,className=Def]
System.out.println(ReflectionToStringBuilder.toString(user, ToStringStyle.NO_FIELD_NAMES_STYLE)); // 默认(无属性名)
// com.cn.User@7229724f[1,张三,123456,0,Def]
System.out.println(ReflectionToStringBuilder.toStringExclude(user, "classId", "className")); // 默认(排除属性)
// com.cn.User@7229724f[id=1,name=张三,password=123456]
}
}
@Getter
@Setter
class User extends BaseModel {
private Integer id;
private String name;
private String password;
public User(Integer id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
}
@Getter
@Setter
class BaseModel {
private Long classId = 0L;
private String className = "Def";
// private Date time = new Date();
}