//使用 UserInputDTO userInputDTO = new UserInputDTO(); userInputDTO.setUsername("username"); userInputDTO.setAge(20); userInputDTO.setBirthday(LocalDate.of(2000, 1, 1)); userInputDTO.setSex(Boolean.TRUE); userInputDTO.setDesc("this is my desc");
User user = new User(); user.setUsername(userInputDTO.getUsername()); user.setAge(userInputDTO.getAge()); user.setBirthday(userInputDTO.getBirthday()); user.setSex(userInputDTO.getSex()); user.setDesc(userInputDTO.getDesc());
而实际上,Spring框架自带一个工具类,可以实现上面的功能,避免编写重复的样板代码:
1 2 3 4 5 6 7 8 9
UserInputDTO userInputDTO = new UserInputDTO(); userInputDTO.setUsername("username"); userInputDTO.setAge(20); userInputDTO.setBirthday(LocalDate.of(2000, 1, 1)); userInputDTO.setSex(Boolean.TRUE); userInputDTO.setDesc("this is my desc");
User user = new User(); BeanUtils.copyProperties(userInputDTO, user);