DTO/VO/Entity等数据类型转换
在web开发中,前端传递数据给后端时采用DTO类型,而存入数据库时则采用entity类型,这样的操作存在类型转换,如果我们一个一个的设值也就太麻烦了,我们可以采用spring提供的jar包来解决这样的问题。
导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
创建实体
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class User {
private String userName;
private String passWord;
private boolean sex;
private String isDelete;
private int version;
private String phone;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private LocalDateTime regisTime;
}
创建DTO
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class UserDTO {
private String userName;
private String passWord;
}
测试
UserDTO dto = new UserDTO().setUserName("jx").setPassWord("1234");
User user = new User();
BeanUtils.copyProperties(dto,user);
System.out.println(user.toString());
|