我使用 Spring boot 3.3.3,我想将我的对象 DTO 转换为仅包含值(不包含字段名称)的字符串行:
例如,我有一个
DTO
,名称为 PersonDto
:
@Data
public class PersonDto implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Gender gender;
private String firstName;
private String middleName;
private String lastName;
private Long salary;
private String email;
private LocalDate employDate;
private String photo;
private String resume;
}
我最后需要有这一行:
1,male,alex,J,karelin,2000,[email protected],2024-02-24
有图书馆可以做吗?非常感谢你
只需重写 toString 方法即可。将如下内容添加到您的班级中。
@Override
public String toString() {
return id + "," + gender + "," + firstName + ...etc;
}